Search code examples
rshinyselectinput

Is it possible to use eventReactive for more than 1 checkboxInput in Shiny?


Some days ago I was answered in this post. The solution was perfect in that moment, but I realised that I forgot to ask how I can do that with more than 1 checkboxInput. Since... I have tried a lot of things and that solution doesn't fit me with 2 checkboxInput. Maybe it can be done with the same solution changing some things, but, as I am new using shiny, I cannot find a way to do it.

The difference between the code from the previous post and this one, is that I have added a conditionalPanel and two checkboxInputs instead of 1.

image 1

Since the condition here is that if the user selects the condition (play), I thought that the solution was writing eventReactive(input$play,{}). However, none of the checkboxInputs work.

On the other hand, if you write eventReactive(input$change_log2,{}) one of the checkboxInputs (the logaritm) works. But if you select the other (srqt) it won't do nothing.

I have seen that an alternative way could be using observe or observeEvent but I cannot save the results in a variable, so... I need eventReactive...

I am a bit lost.

Someone could help me? Eventually I will add more checkboxInputs... so I need a way which I could use more than 2 checkboxInputs.

Here it is the code:

library(shiny)

# Define UI 
ui <- fluidPage(
  
  # Application title
  titlePanel("My app"),
  
  sidebarLayout(
    sidebarPanel(
      uiOutput("selected_sample_one"),
      uiOutput("selected_sample_two"),
      
      checkboxInput("play", strong("I want to play my data"), value = FALSE),
      
      conditionalPanel(
        condition = "input.play == 1",
        checkboxInput("change_log2", "Log2 transformation", value = FALSE),
        checkboxInput("run_sqrt", "sqrt option", value = FALSE))
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("plot")
    )
  )
)

# Define server 
server <- function(input, output,session) {
  
  data <- reactive({
    numbers <- c(5,345,55,10)
    df<-data.frame(t(numbers))
    names(df) <- c("S1", "S2", "S3", "S4")
    
    return(df)
  })
  
  data1 <- eventReactive(input$play,{
    df <- data()
    if(input$change_log2 == TRUE){
      df <- log2(df)
    }
    
    if(input$run_sqrt == TRUE){
      df <- sqrt(df)
    }
  
    return(df)
  })
  
  samples_names <- reactive({
    req(data())
    samples <- colnames(data())
    return(samples)
  })
  
  output$selected_sample_one <- renderUI({
    selectizeInput(inputId = "sample_one_axis", "Select the 1st sample", choices=samples_names(), options=list(maxOptions = length(samples_names())))
  })
  
  # With this function you can select which sample do you want to plot in the y-axis.
  output$selected_sample_two <- renderUI({
    selectizeInput(inputId = "sample_two_axis", "Select the 2nd sample", choices=samples_names(), selected=samples_names()[2], options=list(maxOptions = length(samples_names())))
  })
  
  output$plot <- renderPlot({
    req(input$sample_one_axis,input$sample_two_axis,data1())
    barplot(c(data1()[,input$sample_one_axis], data1()[,input$sample_two_axis]))
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

Thanks very much in advance,

Regards


Solution

  • Thanks to @Limey, the solution for my problem was to replace data1 <- eventReactive with data1 <- reactive.

    Thanks very much.