r - 数据框的列名称中的单词之间的空格会导致 Shiny 的应用程序出现问题

标签 r shiny

我想通过每次将其他变量作为输入来显示摘要并可视化回归模型。当代码工作时,我更改了 iris 的列名称通过在数据集之间创建空格来获取数据集,现在我收到错误,因为 Error in parse: <text>:1:3: unexpected symbol

     library(shiny)
        library(shinydashboard)
        library(shinydashboardPlus)
        library(shinyWidgets)
        library(dplyr)
        library(ggiraph)
        library(ggiraphExtra)
        library(plyr)
        shinyApp(
          ui = dashboardPagePlus(
            header = dashboardHeaderPlus(title = "Social Media Metrics", titleWidth = 320
                                         
            ),
            sidebar = dashboardSidebar(width = 320,
                                       
                                       uiOutput("value"),
                                       uiOutput("value2")
                                       
            ),
            body = dashboardBody(
              verbatimTextOutput("plot"),
              ggiraphOutput("plot2"),
              plotOutput("plot3")
            )
            
            
          ),
          server = function(input, output) {
            
            colnames(iris)[1:4]<-c("f f","s s","d d","f f f")
            
            output$value<-renderUI({
              
              pickerInput(
                inputId = "val"
                ,
                label = "DEPENDENT" 
                ,
                choices = colnames(iris)[-5] #all rows of selected column
                ,
                multiple = F, options = list(`actions-box` = TRUE)
                
              )
              
              
            })
            output$value2<-renderUI({
              
              pickerInput(
                inputId = "val2"
                ,
                label = "INDEPENDENT" 
                ,
                choices = colnames(iris)[-5] #all rows of selected column
                ,
                multiple =T, options = list(`actions-box` = TRUE)
                
              )
            })
            
            
            
            model <- eventReactive(c(input$val,input$val2),{
              req(c(input$val,input$val2))
              lm(as.formula(paste(input$val," ~ ",paste(input$val2,collapse="+"))),data=iris)
            })
            
            output$plot <- renderPrint({
              summary(model())
            })
            output$plot2 <- renderggiraph({
              ggPredict(model(),se=TRUE,interactive=TRUE)
            })
            output$plot3<-renderPlot({
ggplot(iris,aes(y=input$val,x=input$val2))+geom_point()+geom_smooth(method="lm")
})

            
            
            
          }
        )

最佳答案

如果模型变量包含空格,则应使用反引号来定义它们:

lm(as.formula(paste0("`",input$val,"` ~ ",paste0("`",input$val2,"`",collapse="+"))),data=iris)

这使得模型能够工作。
但是,这不能与 ggpredict 一起正常工作,因为反引号未保存在模型本身中: enter image description here

查看ggpredict的源代码表明,在不修改函数代码的情况下似乎很难克服这个困难,例如,请参见导致上述错误的第15行:

temp = paste0("aes(y=", yname, ",x=", xname)

关于r - 数据框的列名称中的单词之间的空格会导致 Shiny 的应用程序出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64105434/

相关文章:

r - 与ggplot2不同数量的离群值

r - 并行计算 15 行(通过矢量化)并用它们创建 df

javascript - 自动关闭日期范围输入 Shiny

html - 在 R 中定位 Shiny 的小部件,例如 box 和 selectInput

r - 从一列制作两列,涵盖所有组合

r - 使用 "apply"将函数应用于矩阵,其中参数是特定于列的

r - 将字母映射到 R 中的数字

r - 在 Shiny 的应用程序中使用 session 参数

r - 如何更改 Shiny 条的颜色

r - 具有多个观察员的 Shiny 传单 map