I have a Shiny app that shows an editable rhandsontable
and displays the table output in a plot. All is well as long as I send a "complete" reactive data frame to rhandsontable
but when I want to subset what's displayed with selectInput
values, the app breaks with the following error message:
Listening on http://127.0.0.1:5362
Warning: Error in matrix: 'data' must be of a vector type, was 'NULL'
Stack trace (innermost first): 60: matrix 59: 58: do.call 57: hot_to_r 56: observerFunc [P:/Mango Engagement/00-CLAIMS/00-shiny-SPRINT3/20190211-shiny-broken-reactive-values-editable-table-plot.R#57] 1: runApp
ERROR: [on_request_read] connection reset by peer
Any ideas on how to go about it? In the script below you can reproduce the problem by commenting/uncommenting definitions of react_df()
.
library(shiny)
library(rhandsontable)
library(dplyr)
library(ggplot2)
#### based on the SO code found here: https://stackoverflow.com/a/52607643/6327771
df <- data.frame(country = c(rep('US', 5), rep('UK', 5)),
date = rep(as.Date(c('2018-01-01', '2018-02-01', '2018-03-01', '2018-04-01', '2018-05-01')), 2),
val = rep(sample(6:8, size = 5, replace = T), 2))
ui <- fluidPage(
selectInput("country", "Choose the Country",
choices = df %>% unique() %>% pull(country),
selected = 'US',
multiple = FALSE),
rHandsontableOutput('table'), # editable table with bar heights
plotOutput('plot')#, # bar plot that takes values from the drop-down menu and the editable table
)
server <- function(input,output,session)({
#### REACTIVE INPUT: SUBSETTED VERSUS COMPLETE DATA ####
## subsetted version of react_df()
# react_df <- reactive({
# df %>%
# filter(country == input$country)
# })
## complete version of react_df()
react_df <- reactive({df})
values <- reactiveValues(data = NULL)
observe({
values$data <-react_df()
})
#observe the table
observe({
if(!is.null(input$table))
values$data <- hot_to_r(input$table)
})
# output the table
output$table <- renderRHandsontable({
req(values$data)
rhandsontable(values$data)
})
# update the plot
output$plot <- renderPlot({
req(values$data)
ggplot(values$data, aes(date, val)) +
geom_line() +
geom_point()
})
})
shinyApp(ui = ui, server = server)