Vit
Vit

Reputation: 77

Shiny click on button and show new plot

I have the following app: If you click next, you will see a plot - at the moment with trivial information - and have to choose two radiobutton options: yes or no. Then you can click on the next button and evaluate the next plot. The problem is, that you have to first click on the button to show the first plot. Also you see in the print statement a missmatch. The first radio button choice is printed in the second line instad of the first print statement.

Can you help me to show an initial plot?

ui <- fluidPage(

  actionButton("buttonNext", "Next"),

  radioButtons("radio", "Your Decision:",
               choices = c("No Decision" = 'NoDec', "Yes" = 'yes', "No" = 'no'),
               selected = 'NoDec'),

  plotOutput("TimeSeriesPlot")
)


server <- function(input,output,session) {

  observeEvent(input$buttonNext, {
  })

  clickNext <- eventReactive(input$buttonNext, {
    updateRadioButtons(session,'radio',selected = -1)
    randomNumber <- input$buttonNext
    print(c(input$buttonNext,randomNumber,input$radio))
    return(randomNumber)
  })

  output$TimeSeriesPlot <- renderPlot({ 

    i <- clickNext()
    plot(i)

  })

}
shinyApp(server = server, ui = ui)

Upvotes: 1

Views: 4844

Answers (2)

Vit
Vit

Reputation: 77

Thank you a lot!

How is it possible to save the data the user is inserting:

print(c(input$buttonNext,randomNumber,input_radio))

This should not be just printed, but it should be available to me later. My plan is, that people evaluate my plots and then I see for each user the buttons he clicked for each plot.

Upvotes: 0

Florian
Florian

Reputation: 25425

You could use a simple reactive, and isolate the statement where you call the radio button's value. This way, the reactive won't take a dependency on the radiobuttons. Also, it is considered bad practice to use a reactive for it's side-effects, better to update the radio buttons from a separate observer:

ui <- fluidPage(

  actionButton("buttonNext", "Next"),

  radioButtons("radio", "Your Decision:",
               choices = c("No Decision" = 'NoDec', "Yes" = 'yes', "No" = 'no'),
               selected = 'NoDec'),

  plotOutput("TimeSeriesPlot")
)


server <- function(input,output,session) {

  clickNext <- reactive({
    isolate(input_radio <- input$radio)
    randomNumber <- input$buttonNext
    print(c(input$buttonNext,randomNumber,input_radio))
    return(randomNumber)
  })

  observeEvent(input$buttonNext,
               {
                 updateRadioButtons(session,'radio',selected = -1)
               })

  output$TimeSeriesPlot <- renderPlot({ 
    i <- clickNext()
    plot(i)
  })

}
shinyApp(server = server, ui = ui)

Hope this helps.

Upvotes: 1

Related Questions