使用$运算符和过滤器时,Rshiny输出功能中断

时间:2018-11-21 22:32:32

标签: r shiny

尽管下面有一个不同的(尽管结构相似)数据对象,但我仍在尝试根据自己拥有的(工作)的上一个应用程序非常紧密地构建一个闪亮的应用程序。更改所有变量和对象名称后,出现以下错误:

  

警告:filter_impl中的错误:评估错误:$运算符无效   用于原子向量。

我在网上搜索中找到了几个看似相关的页面:

$ operator is invalid for atomic vectors :: R shiny

https://github.com/rstudio/shiny/issues/1823

不幸的是,$运算符错误似乎是一条非常普通的错误消息,而且这两个错误似乎都没有专门解决我的问题。经过一番修补后,我发现我可以在应用程序中渲染图和表,前提是我不尝试对任何输入字段进行过滤。

例如,以下输出可以正常工作,包括允许我打开和关闭整个图以及使用名为filter1的文本输入更改图形标题的开关。

output$emoPlot <- renderPlotly({

if(input$prefilterswitch == "OFF"){
  df <- dtm_EMO %>% 
    clusterer(input$clusters)

  plot <- df %>% 
    left_join(EMO_ALL, by = "Work_Order") %>% 
    ggplot(aes(x = date, y = as.factor(cluster), col = MILL, shape = as.factor(PART), text = Equipment_Description_Line_1, text2 = Work_Order))+
    geom_point()+
    guides(col = "none")+
    ggtitle(label = input$filter1)
  ggplotly(plot, tooltip = c("x", "y", "col", "text", "text2", "size"))

}

})

但是,如果我添加试图对这些输入之一进行过滤的行,就像这样:

output$emoPlot <- renderPlotly({


if(input$prefilterswitch == "OFF"){
  df <- dtm_EMO %>% 
    filter(str_detect(combined, input$prefilterswitch %>% tolower()) ==T) %>%
    clusterer(input$clusters)

  plot <- df %>% 
    left_join(EMO_ALL, by = "Work_Order") %>% 
    ggplot(aes(x = date, y = as.factor(cluster), col = MILL, shape = as.factor(PART), text = Equipment_Description_Line_1, text2 = Work_Order))+
    geom_point()+
    guides(col = "none")+
    ggtitle(label = input$filter1)
  ggplotly(plot, tooltip = c("x", "y", "col", "text", "text2", "size"))

}

})

然后我得到我的错误,并且没有剧情:

  

警告:filter_impl中的错误:评估错误:$运算符无效   用于原子向量。

我也尝试过使用renderTable来租借表格输出,并遇到同样的问题。我可以使用输入,但不能使用filter()之类的功能,并且mutate()也存在相同的问题。

我怀疑问题可能与输入有关,但是所有开关和文本字段都在应用程序中找到了,它们似乎起作用了,只是与这些功能无关。

这与我能够缩小的范围差不多。这有点令人沮丧,因为应用多个过滤器的功能对于应用程序的用途非常重要。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我很确定问题是在过滤器中使用了非标准评估。在

 filter(str_detect(combined, input$prefilterswitch %>% tolower()))

您有输入$ prefilterswitch ...,这通常在过滤器中将是无效的,因为它不只是从组合中给出列名(即使我们实际上知道是这样。我认为可能有一个自动检入过滤器如果存在$,则会引发错误。我通常的解决方案是在开始管道之前创建对象,例如

 prefilterswitch <- input$prefilterswitch

,然后在filter语句中引用它。同样,使用布尔值时,您不需要==T。 或者,您可以进入rlang。