如何在 Shiny 中显示依赖于 selectInput 选择的 rhandsontable

How to display an rhandsontable in Shiny that is dependent on selectInput choices

我有一个 select 输入框,用户可以在其中 select 从三个选项中选择。制作 selection 后,我希望出现 table,他们可以借此输入与这些选择相关的一些附加值。

然后我想显示摘要 table,其中包含已输入值的摘要。

当我 运行 下面的代码时,我得到以下错误:

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

library(shiny)
library(rhandsontable)
library(dplyr)


ui <- fluidPage(
  mainPanel(
    selectInput("section", "Section", choices = c("A","B","C"), multiple = TRUE),
    rHandsontableOutput('table')
  )
)

server = function(input, output, session) {

  SectionList <- input$section

  Section <- eventReactive(input$section, {
    section_table <- data.frame(Section = SectionList, input1 = 0, input2 = 0)
    return(section_table)
  })

  output$table <- renderRHandsontable({
    if(!is.null(input$table)){
      DF = hot_to_r(input$table)
    } else {
      DF = data.frame(Section = "A", input1 = 0, input2 = 0)
    }
    rhandsontable(DF) %>%
      hot_col(col = "input1") %>%
      hot_col(col = "input2")
  })

  results_summary <- eventReactive(input$table, {
    DF = output$table
    summary <- DF %>% group_by(Section) %>%
      summarise(input1 = mean(input1),
                input2 = mean(input2))
  })

  output$results <- renderTable({
    results_summary()
  })

}
shinyApp(ui, server)

我还没有完全理解你的目的,但错误是由于以下行而产生的:

SectionList <- input$section

此行必须转到响应式环境。如果您将其更改为,例如

Section <- eventReactive(input$section, { SectionList <- input$section section_table <- data.frame(Section = SectionList, input1 = 0, input2 = 0) return(section_table) })

错误消失。希望这对您有所帮助!