简体   繁体   中英

Shiny observeEvent on different actions

I would like to do something as: there are actionLink a and actionLink b. And they can trigger the UpdateNumericInput() which assigns some weights. I can also manually change the weights on the numericInput() in UI. What I want to realize is when clicking on the actionLink a, the textoutput would be "click on a", and same thing happened with actionLink b as "click on b". I also want want the textoutput to show up as "manually changed the weights" when I actually edit the weights on UI. Is there any way to realize this?

#triggered by actionLink a
apply_a_weights = observeEvent(input$a_weight_link, {

v_a = get_a_weights()

v_a = setNames(as.double(unname(v_a)), names(v_a))

for (nm in names(v_a)) {
  updateNumericInput(session, nm, value = v_a[[nm]])
}
output$selected_weight <- renderUI({HTML("You have changed to <B>a weight</B>")}) }, priority = 1)

# triggered by actionLink b
apply_b_weights = observeEvent(input$b_weight_link, {

v_b = df_dmd$b_weights
names(v_b) = df_dmd$input_id

for (nm in names(v_b)) {
  updateNumericInput(session, nm, value = v_b[[nm]])
}
output$selected_weight <- renderUI({HTML("You have changed to <B>b weight</B>")})}, priority = 1)

I also tried to put this part in the server.R as the default textoutput:

output$selected_weight <- renderUI({HTML("<B>manually</B> changed the weights")})

But those codes doesn't work. When I change the weights on my own, the "manually changed the weights" doesn't show up.

Many Thanks

You don't provide a reproducible code so it's hard to guess. Possibly, the problem is the duplicated output$selected_weight .

Define a reactiveVal to store the text do be displayed, for example at the beginning of server :

Text <- reactiveVal()

Then in your two observeEvent , remove output$selected_weight , and do:

#triggered by actionLink a
apply_a_weights = observeEvent(input$a_weight_link, {

v_a = get_a_weights()

v_a = setNames(as.double(unname(v_a)), names(v_a))

for (nm in names(v_a)) {
  updateNumericInput(session, nm, value = v_a[[nm]])
}

Text("You have changed to <B>a weight</B>") 

}, priority = 1)

# triggered by actionLink b
apply_b_weights = observeEvent(input$b_weight_link, {

v_b = df_dmd$b_weights
names(v_b) = df_dmd$input_id

for (nm in names(v_b)) {
  updateNumericInput(session, nm, value = v_b[[nm]])
}

Text("You have changed to <B>b weight</B>") 

}, priority = 1)

Finally, outside the observeEvent s, do

output$selected_weight <- renderUI({HTML(Text()})

Again, your don't provide a minimal reproducible code, so I'm not sure my answer provides what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM