简体   繁体   中英

observe event different actions on 2nd & 3rd event

i try to use an actionButton with observeEvent. My question is: can i define different actions with a second click on the button? something like

`observeEvent(input$verlauf,{
if (input$verlauf == 0){input$verlauf <- 1 & renderTable(BDI2())} 
else if (input$verlauf == 1){input$verlauf <- 2 & renderTable(BDI2())} 
else if (input$verlauf == 2){input$verlauf <- 3 & renderTable(BDI3())} 
else if (input$verlauf == 3){input$verlauf <- 4 & renderTable(BDI4())} 
else if (input$verlauf == 4){input$verlauf <- 5 & renderTable(BDI5())} 
else if (input$verlauf == 5){input$verlauf <- 6 & renderTable(BDI6())} 
else {}})`

on every click on the actionButton there should be a different action. How can i do this in my R / shiny code? it wont work

Thank you very much :)

derlu

You can't increment the value of input$verlauf , but you can make a reactiveValues object and increment that value. See below for an example.

library(shiny)

shinyApp(
  ui = fluidPage(
    actionButton(inputId = "button",
                 label = "I am a Button. Click me!"),
    uiOutput("result")
  ),

  server = shinyServer(function(input, output, session){

    # MAKE A REACTIVE VALUES OBJECT HERE.  THIS BEHAVES LIKE A LIST, BUT 
    # CAN TRIGGER REACTIVE COMPONENTS
    Button <- reactiveValues(
      Click = 0
    )

    # Update the Button$Click object on each click of input$button
    observeEvent(input$button,
                 {
                   Button$Click <- input$button %% 8
                 })

    # Take an element of the vector.  This is changed every time
    # Button$Click changes.
    output$result <- 
      renderUI({
        p(c("It's raining tacos.",
            "From out of the sky.",
            "Tacos.",
            "No need to ask why.",
            "Just open your mouth",
            "and close your eyes...",
            "it's raining tacos.")[Button$Click])
      })
  })
)

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