运行 用于创建预测模型和相应图的闪亮应用时出错
Error while running a shiny app to create a predictive model and a corresponding plot
我是 shiny 的新手,刚刚使用 R 中可用的 ToothGrowth 数据构建了这个 shiny 应用程序。数据摘要如下:
str(ToothGrowth)
'data.frame': 60 obs. of 3 variables:
$ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
$ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
$ dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
我的ui.R文件如下:
library(shiny)
# Define UI for application
shinyUI(fluidPage(
# Application title
titlePanel("Tooth length prediction"),
# Sidebar with options selectors
sidebarLayout(
sidebarPanel(
h3(helpText("Select:")),
selectInput("dos", label = h4("Dose Value"), choices = list("0.5", "1" ,"2")),
selectInput("sup", label = h4("Supplement Type"),
choices = list("OJ" = "OJ", "VC" = "VC"))
),
# Show a plot and regression line
mainPanel(
plotOutput("distPlot"),
h4("Predicted tooth value is:"),
h3(textOutput("result"))
)
)
))
我的server.R文件如下:
library(shiny)
library(ggplot2)
library(dplyr)
head(ToothGrowth)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# build linear regression model
ToothGrowth$dose <- as.numeric(ToothGrowth$dose)
fit <- lm( len~dose + supp , ToothGrowth)
# predicts the length
pred <- predict(fit, newdata = data.frame(dose = input$dos,
supp = input$sup
))
# Draw the plot using ggplot2
plot <- ggplot(data=ToothGrowth, aes(x=dose, y = len))+
geom_point(aes(color = supp), alpha = 0.3)+
geom_smooth(method = "lm")
plot
})
output$result <- renderText({
# Renders the text for the prediction below the graph
fit <- lm( len~dose + supp , ToothGrowth)
pred <- predict(fit, newdata = data.frame(dose = input$dos,
supp = input$sup
))
})
})
当我 运行 应用程序时,出现此错误:
Error in : variable 'dose' was fitted with type "numeric" but type "character" was supplied
我没有看到 ggplot 创建的任何渲染图,也没有看到线性模型的预测输出结果,如下所示:
如果有人能告诉我我做错了什么,我将不胜感激。
非常感谢!
此致
在服务器部分添加as.numeric()
到dose
:
将 input$dos
替换为 as.numeric(input$dos)
library(shiny)
library(ggplot2)
library(dplyr)
head(ToothGrowth)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# build linear regression model
ToothGrowth$dose <- as.numeric(ToothGrowth$dose)
fit <- lm( len~dose + supp , ToothGrowth)
# predicts the length
pred <- predict(fit, newdata = data.frame(dose = as.numeric(input$dos),
supp = input$sup
))
# Draw the plot using ggplot2
plot <- ggplot(data=ToothGrowth, aes(x=dose, y = len))+
geom_point(aes(color = supp), alpha = 0.3)+
geom_smooth(method = "lm")
plot
})
output$result <- renderText({
# Renders the text for the prediction below the graph
fit <- lm( len~dose + supp , ToothGrowth)
pred <- predict(fit, newdata = data.frame(dose = as.numeric(input$dos),
supp = input$sup
))
})
})
我是 shiny 的新手,刚刚使用 R 中可用的 ToothGrowth 数据构建了这个 shiny 应用程序。数据摘要如下:
str(ToothGrowth)
'data.frame': 60 obs. of 3 variables:
$ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
$ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
$ dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
我的ui.R文件如下:
library(shiny)
# Define UI for application
shinyUI(fluidPage(
# Application title
titlePanel("Tooth length prediction"),
# Sidebar with options selectors
sidebarLayout(
sidebarPanel(
h3(helpText("Select:")),
selectInput("dos", label = h4("Dose Value"), choices = list("0.5", "1" ,"2")),
selectInput("sup", label = h4("Supplement Type"),
choices = list("OJ" = "OJ", "VC" = "VC"))
),
# Show a plot and regression line
mainPanel(
plotOutput("distPlot"),
h4("Predicted tooth value is:"),
h3(textOutput("result"))
)
)
))
我的server.R文件如下:
library(shiny)
library(ggplot2)
library(dplyr)
head(ToothGrowth)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# build linear regression model
ToothGrowth$dose <- as.numeric(ToothGrowth$dose)
fit <- lm( len~dose + supp , ToothGrowth)
# predicts the length
pred <- predict(fit, newdata = data.frame(dose = input$dos,
supp = input$sup
))
# Draw the plot using ggplot2
plot <- ggplot(data=ToothGrowth, aes(x=dose, y = len))+
geom_point(aes(color = supp), alpha = 0.3)+
geom_smooth(method = "lm")
plot
})
output$result <- renderText({
# Renders the text for the prediction below the graph
fit <- lm( len~dose + supp , ToothGrowth)
pred <- predict(fit, newdata = data.frame(dose = input$dos,
supp = input$sup
))
})
})
当我 运行 应用程序时,出现此错误:
Error in : variable 'dose' was fitted with type "numeric" but type "character" was supplied
我没有看到 ggplot 创建的任何渲染图,也没有看到线性模型的预测输出结果,如下所示:
如果有人能告诉我我做错了什么,我将不胜感激。
非常感谢!
此致
在服务器部分添加as.numeric()
到dose
:
将 input$dos
替换为 as.numeric(input$dos)
library(shiny)
library(ggplot2)
library(dplyr)
head(ToothGrowth)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# build linear regression model
ToothGrowth$dose <- as.numeric(ToothGrowth$dose)
fit <- lm( len~dose + supp , ToothGrowth)
# predicts the length
pred <- predict(fit, newdata = data.frame(dose = as.numeric(input$dos),
supp = input$sup
))
# Draw the plot using ggplot2
plot <- ggplot(data=ToothGrowth, aes(x=dose, y = len))+
geom_point(aes(color = supp), alpha = 0.3)+
geom_smooth(method = "lm")
plot
})
output$result <- renderText({
# Renders the text for the prediction below the graph
fit <- lm( len~dose + supp , ToothGrowth)
pred <- predict(fit, newdata = data.frame(dose = as.numeric(input$dos),
supp = input$sup
))
})
})