我正在尝试为项目构建一个闪亮的应用程序,我想我已经完成了,一切似乎都正常工作但我在运行应用程序时在控制台中不断收到此错误:
Error in grep(input$predict, columns) : invalid 'pattern' argument
Error in parse(text = x) : <text>:2:0: unexpected end of input
1: Ozone ~
^
Error in parse(text = x) : <text>:2:0: unexpected end of input
1: Ozone ~
^
这是我的ui.R代码:
library(shiny)
# Define UI for airquality data application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Air Quality Relationships & Predictions"),
sidebarPanel(
selectInput("outcome", "Outcome:", list("O Zone" = "Ozone",
"Solar Radiation" = "Solar.R",
"Wind" = "Wind",
"Temperature" = "Temp")),
uiOutput("predictor"),
uiOutput("slider"),
h5(textOutput("guess"))
),
mainPanel(
plotOutput("ioPlot")
)
))
这是server.R的代码:
library(shiny)
library(datasets)
#import dataset
airData <- airquality
#make list of columns for dropdowns
columns <- list("O Zone" = "Ozone", "Solar Radiation" = "Solar.R", "Wind" = "Wind", "Temperature" = "Temp")
# Define server logic
shinyServer(function(input, output, session) {
#set dropdown so it only shows 3 remaining columns, won't compare a variable to itself
output$predictor = renderUI({
outc <- input$outcome
selectInput("predict", "Predictor:", columns[-grep(outc,columns)])
})
#setup the slider for the current selections, default to mean, set max and min to the observed max and min
output$slider = renderUI({
slLabel = paste("Adjust to predict ", names(columns[grep(input$outcome,columns)]), "using this value for ", names(columns[grep(input$predict,columns)]), ":")
slVal = round(mean(airData[,input$predict], na.rm=TRUE),0)
slMin = round(min(airData[,input$predict], na.rm=TRUE),0)
slMax = round(max(airData[,input$predict], na.rm=TRUE),0)
sliderInput('slide', slLabel, value = slVal, min = slMin, max = slMax, step=1,)
})
#create formula for plot
formulaText <- reactive({
paste(input$outcome, " ~ ", input$predict)
})
#create text for prediction
output$guess <- renderText({
fit <- lm(as.formula(formulaText()), data=airData)
slp <- fit$coeff[2]
yint <- fit$coeff[1]
predout <- round(yint + slp * input$slide,2)
paste("Predicted outcome for ", names(columns[grep(input$outcome,columns)]), ": ", predout)
})
#create plot and fitted line
output$ioPlot <- renderPlot({
fit <- lm(as.formula(formulaText()), data=airData)
plot(as.formula(formulaText()),
data = airData, xlab=names(columns[grep(input$predict,columns)]), ylab=names(columns[grep(input$outcome,columns)]))
abline(fit, col="red")
title(main= paste(names(columns[grep(input$outcome,columns)]), " vs. ", names(columns[grep(input$predict,columns)])))
})
})
答案 0 :(得分:0)
我认为问题是由于您将columns
分组为predict
selectInput
试试这个
output$predictor = renderUI({
selectInput("predict", "Predictor:", columns[columns!=input$outcome])
})
除了为columns
选择的内容外,list
input$outcome
应该为您提供所有内容。
修改强>
我刚刚注意到你也多次使用grep
来为输出粘贴一些文本。相反,你应该使用如下语句:
xlab=names(columns[columns==input$predict])
也就是说,您不需要grep
,您可以直接将columns
分组。