我想通过每次将其他变量作为输入来显示摘要并可视化回归模型。当代码工作时,我更改了 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 一起正常工作,因为反引号未保存在模型本身中:
查看ggpredict
的源代码表明,在不修改函数代码的情况下似乎很难克服这个困难,例如,请参见导致上述错误的第15行:
temp = paste0("aes(y=", yname, ",x=", xname)
关于r - 数据框的列名称中的单词之间的空格会导致 Shiny 的应用程序出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64105434/