【发布时间】:2019-07-04 20:48:22
【问题描述】:
我有一个 Shiny 应用程序,它显示可编辑的 rhandsontable 并在图中显示表格输出。只要我向rhandsontable 发送一个“完整的”反应数据帧,一切都很好,但是当我想对selectInput 值显示的内容进行子集化时,应用程序会中断并显示以下错误消息:
警告:矩阵中的错误:“数据”必须是向量类型,为“NULL”
堆栈跟踪(最内层优先): 60:矩阵 59: 58:打电话 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:运行应用程序
错误:[on_request_read] 连接被对等方重置
关于如何去做的任何想法?在下面的脚本中,您可以通过注释/取消注释 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)
【问题讨论】:
-
“应用中断”是什么意思?错误信息是什么?我复制了您的代码,一切正常。
-
刚刚通过添加错误消息编辑了我的问题。只有当您取消注释以下定义时才会返回它:
react_df <- reactive({ df %>% filter(country == input$country) })并注释掉原始代码中显示的当前定义 -
即使在取消注释/注释后也能正常工作。在用户选择一个国家之前,它只会以空白开始,因为此时
!is.null(input$table)是False。确保所有软件包都已更新。 -
我明白了.. 谢谢。我正在使用内部 CRAN 的客户端机器上工作,其包版本可以追溯到 2017 年......所以在更新发生之前我可以在这里做的不多。非常感谢!
-
还有一件事,使用上面的示例,我如何确保在用户指定之前始终有一个默认的
country选项?再次感谢您的帮助
标签: r shiny rhandsontable