【问题标题】:Error when selecting variables (with `varSelectInput`) and using ggplot with Shiny (Error: `arg` must a symbol)选择变量(使用 `varSelectInput`)和使用带有 Shiny 的 ggplot 时出错(错误:`arg` 必须是符号)
【发布时间】:2022-01-16 15:22:16
【问题描述】:

我正在尝试制作一个使用此功能的 Shiny 应用程序(用 R markdown 制作):

ls_vs <- function(variable) {
dataset %>%
  filter({{variable}} != 254.948530) %>% 
  filter({{variable}} != 121.738080) %>%
  ggplot(mapping = aes(y = life_satisfaction, x = {{variable}})) +
  geom_point(aes(color = region, shape = freedom_status), size = 2) +
  geom_smooth(color = "cyan") +
  labs(y = "Life Satisfaction", shape = "Freedom Status", color = "Continent")
}

ls_vs(economic_freedom)

我用这个函数得到了想要的结果:

现在这是我将其集成到闪亮应用中的尝试:

用户界面部分:

tabPanel("Factor Comparision", 
               
               sidebarPanel(
                 varSelectInput(inputId = "variable", label = "Select Factor to Compare", data = dataset),
                 
                mainPanel(                                   
                  h1("Test"),           
                  plotOutput("ls_vs"),
                 )   
               )),
      

这里是服务器部分:

#Factor Comparisons
    output$ls_vs <- renderPlot({
      dataset%>%
        filter({{input$variable}} != 254.948530) %>% 
        filter({{input$variable}} != 121.738080) %>%
        ggplot(mapping = aes(y = life_satisfaction, x = {{input$variable}})) +
        geom_point(aes(color = region, shape = freedom_status), size = 2) +
        geom_smooth(color = "cyan") +
        labs(y = "Life Satisfaction", shape = "Freedom Status", color = "Continent")
    })

在尝试运行应用程序时,我收到了错误:

错误:arg 必须是符号

我是否在 UI 区域或服务器区域做错了什么?如何在 Shiny 中正确使用 varSelectInput 来制作交互式 ggplot,可以在其中更改变量以更改绘图?

非常感谢!

【问题讨论】:

    标签: r ggplot2 shiny shiny-server


    【解决方案1】:

    dplyr::sym() 中使用“bang bang”运算符也应该可以工作。

    #Factor Comparisons
    output$ls_vs <- renderPlot({
      dataset%>%
        filter(!!sym(input$variable) != 254.948530) %>% 
        filter(!!sym(input$variable) != 121.738080) %>%
        ggplot(mapping = aes(y = life_satisfaction, x = !!sym(input$variable))) +
        geom_point(aes(color = region, shape = freedom_status), size = 2) +
        geom_smooth(color = "cyan") +
        labs(y = "Life Satisfaction", shape = "Freedom Status", color = "Continent")
    })
    

    【讨论】:

      【解决方案2】:

      markdown 中的代码与 Shiny 中的代码之间的主要区别在于 Shiny 传递变量的字符串值 ("economic_freedom"),而 markdown 中的函数已编写为使用裸列名称 (economic_freedom)。

      更改函数以使用可以使用.data 而不是{{}} 完成的字符串。

      library(dplyr)
      library(ggplot2)
      
      output$ls_vs <- renderPlot({
        dataset%>%
          filter(.data[[input$variable]] != 254.948530) %>% 
          filter(.data[[input$variable]] != 121.738080) %>%
          ggplot(mapping = aes(y = life_satisfaction, x = .data[[input$variable]])) +
          geom_point(aes(color = region, shape = freedom_status), size = 2) +
          geom_smooth(color = "cyan") +
          labs(y = "Life Satisfaction", shape = "Freedom Status", color = "Continent")
      })
      

      【讨论】:

      • 非常感谢,效果很好。我有一个快速跟进的问题,您能否提供一些关于从 varSelectInput 中删除某些选项的指导。我的应用程序显示包含所有变量的下拉菜单,但我只想选择一组特定的变量显示在下拉菜单中,并希望重命名它们,以便它们尽可能不显示下划线。再次感谢您。
      • 您可以在 varSelectInput 中传递数据帧的子集,即如果您想包含前 5 列,则仅传递 data 而不是传递 dataset[1:5]。您也可以使用selectInput 代替varSelectInput。在selectInput 中,您可以手动传递值,例如 c('A', 'B', 'C')` 或列名称的子集,例如 names(dataset)[1:5]
      猜你喜欢
      • 1970-01-01
      • 2015-09-04
      • 2021-03-19
      • 2012-06-07
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多