1

I have three tables (math, science, and literature results). I would like to have a "dynamic" table that updates some columns when clicking a checkboxGroupInput option. To do that, I'm using the bind_rows function, but I'm not being able to update the table the way I want.

The final output needs to be something like the gif below:
(1) When I click "math" it shows the math results
(2) when I click "literature" (sorry for the typo), it shows the literature results etc..

enter image description here

I added

if (input$overall_boxes == "sci") { table_science } else { FALSE } #when I click "sci" is shows the science results (and keep the math if math is selected)
if (input$overall_boxes == "lit") { table_litterature } else { FALSE }

on purpose to demonstrate what is the goal.

If you have another strategy to run that, feel comfortable changing the following code

library(shiny)
library(tidyverse)
table_math <- data.frame(age = c(5,10), test = "math", result = rnorm(100,10,2))
table_science <- data.frame(age = c(10,15), test = "science", result = rnorm(100,8,2))
table_litterature <- data.frame(age = c(5,15), test = "litterature", result = rnorm(100,5,2))

# Define UI for application that draws a histogram
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("overall_boxes", label = h3("This is a Checkbox group"), 
                         choices = list("Math" = "math", "Sciences" = "sci", "Litterature" = "lit"),
                         selected = "math")
    ),
    # Show a plot of the generated distribution
    mainPanel(
      dataTableOutput("main_results")
    )
  )
)


# Define server logic required to draw a histogram
server <- function(input, output) {
  
  #backend
  merge_results <- reactive({
    bind_rows( 
      if (input$overall_boxes == "math") { table_math } else { FALSE } #when I click "math" is shows the math results
    )
  })
  
  #real output
  output$main_results <- renderDataTable(
    merge_results()
    
  )
  
}

# Run the application 
shinyApp(ui = ui, server = server)
Luis
  • 1,248
  • 10
  • 25

1 Answers1

2

input$overall_boxes may contain multiple elements, so that you should use %in% instead of == in the if statement.

Try:

library(shiny)
library(tidyverse)
table_math <- data.frame(age = c(5,10), test = "math", result = rnorm(100,10,2))
table_science <- data.frame(age = c(10,15), test = "science", result = rnorm(100,8,2))
table_litterature <- data.frame(age = c(5,15), test = "litterature", result = rnorm(100,5,2))

# Define UI for application that draws a histogram
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("overall_boxes", label = h3("This is a Checkbox group"), 
                         choices = list("Math" = "math", "Sciences" = "sci", "Litterature" = "lit"),
                         selected = "math")
    ),
    # Show a plot of the generated distribution
    mainPanel(
      dataTableOutput("main_results")
    )
  )
)


# Define server logic required to draw a histogram
server <- function(input, output) {
  
  #backend
  merge_results <- reactive({

    bind_rows(
      if ("math" %in% input$overall_boxes) { table_math } else { table_math[F,] }, #when I click "math" is shows the math results
      if ("sci" %in% input$overall_boxes) { table_science } else { table_science[F,] } ,#when I click "sci" is shows the science results (and keep the math if math is selected)
      if ("lit" %in% input$overall_boxes) { table_litterature } else { table_litterature[F,]}
      )
  })
  
  #real output
  output$main_results <- renderDataTable(
    merge_results()
    
  )
  
}

# Run the application 
shinyApp(ui = ui, server = server)
Waldi
  • 31,868
  • 6
  • 18
  • 66
  • You made it look easy! Thank you so much! I didn't know of this `[F,]` argument. Is it an R base code ? – Luis Feb 01 '22 at 21:31
  • 2
    `[F,]` is just to return a table with 0 rows instead of NA so that `bind_rows` doesn't complain. Try : `mtcars[F,]` – Waldi Feb 01 '22 at 21:34
  • I saw in your profile that you're a fluent datatable user. would you mind providing some guidance on this subject here? https://stackoverflow.com/questions/70955804/implementing-ifelse-or-if-else-in-datatable-output-to-conditionally-change-the – Luis Feb 02 '22 at 12:41
  • Ohhh!! I see! My bad!! Thank you!! – Luis Feb 02 '22 at 12:48