andrew
andrew

Reputation: 49

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

I have a select input box where the user can select from three choices. Having made the selection I want a table to appear whereby they can enter some additional values associated with those choices.

I then want to display a summary table which contains a summary of the values that have been entered.

When I run the code below I get the following error:

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)

Upvotes: 0

Views: 427

Answers (1)

MKR
MKR

Reputation: 1700

I haven't fully understood what you are aiming to do, but the error is produced because of the line:

SectionList <- input$section

this line has to go to a reactive environment. If you change this to, e.g.

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

the error disappears. Hope this helps!

Upvotes: 1

Related Questions