使用 Shiny 和 checkboxGroupInput 通过 bind_rows 或 rbind 更新 table 并使用 if else 语句

Using Shiny and checkboxGroupInput to update a table via bind_rows or rbind and using if else statements

我有三个 table(数学、科学和文学成绩)。我想要一个“动态”table,它在单击 checkboxGroupInput 选项时更新某些列。为此,我正在使用 bind_rows 函数,但无法按照我想要的方式更新 table。

最终输出需要类似于下面的 gif:
(1) 当我点击“数学”时它显示数学结果
(2) 当我点击“文献”时(抱歉打错了),它显示的是文献结果 等..

我加了

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 }

故意展示目标是什么。

如果您有其他策略 运行 那,感觉很舒服table 更改以下代码

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)

input$overall_boxes可能包含多个元素,因此您应该在if语句中使用%in%而不是==

尝试:

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)