0

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)
Kasia Kulma
  • 1,683
  • 1
  • 14
  • 39
  • What do you mean with 'the app breaks'? What is the error message? I copied your code and everything works as expected. – Wilmar van Ommeren Feb 11 '19 at 11:35
  • just edited my question by adding the error message. It will be returned only when you uncomment the following definition: `react_df <- reactive({ df %>% filter(country == input$country) })` and comment out the current definition shown in the original code – Kasia Kulma Feb 11 '19 at 11:43
  • It works even after uncomment/comment. It only starts as blank until the user selects a country, since `!is.null(input$table)` is `False` at that moment. Make sure all packages are updated. – A. Suliman Feb 11 '19 at 11:53
  • I see.. Thank for that. I'm working on the client's machine that uses internal CRAN with package versions going back to 2017... so not much I can here until the updates happen. Big thanks! – Kasia Kulma Feb 11 '19 at 11:55
  • One more thing, using the above example, how can I make sure that there's always a default `country` option in place before a user specify one? Thanks again for your help – Kasia Kulma Feb 11 '19 at 13:16

1 Answers1

0

As @ A.Suliman pointed out, the issue was caused but working on old R packages. The same code run with updated packages returned no error. Thanks for help!

Kasia Kulma
  • 1,683
  • 1
  • 14
  • 39