[英]Shiny app error in console
I am trying to build a shiny app for a project and I think I am all done, everything seems to be working properly but I keep getting this error in my console when I run the app: 我正在尝试为一个项目构建一个闪亮的应用程序,但我想我已经完成了一切,一切似乎都正常运行,但是运行该应用程序时,我的控制台中始终出现此错误:
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 ~
^
Here is my code for ui.R: 这是我的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")
)
))
And here is the code for server.R: 这是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)])))
})
})
I think the issue is due to the way you are subsetting columns
to make the predict
selectInput
我认为这个问题是由于您进行子集的方式
columns
,使predict
selectInput
Try this instead 试试这个
output$predictor = renderUI({
selectInput("predict", "Predictor:", columns[columns!=input$outcome])
})
That should give you everything from the columns
list
except for whatever was chosen for input$outcome
. 这应该为您提供
columns
list
所有内容,但为input$outcome
选择的内容除外。
Edit I just noticed you are also using a grep
many times in order to paste some text for an output. 编辑我刚刚注意到您还多次使用
grep
以便为输出粘贴一些文本。 Instead you should use statements like: 相反,您应该使用如下语句:
xlab=names(columns[columns==input$predict])
That is, you don't need grep
you can just subset columns
directly. 也就是说,您不需要
grep
您可以直接对columns
子集化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.