繁体   English   中英

R Studio Shiny 应用程序错误:未找到 object“变量(克拉)”

[英]R Studio Shiny App Error: object 'Variable(carat)' not found

我正在尝试开发一个 shiny 应用程序,以使用 R 上的“钻石”数据来预测基于克拉的钻石价格。 除了返回错误的 plot 部分外,一切正常:未找到 object “克拉”。 该代码似乎在 shiny 之外工作,所以我猜问题在于将数据集读入闪亮服务器。 我是 shiny 的新手,我在这里探索了类似的问题,但我想知道我做错了什么。 我尝试在此查询中附加错误图像和预期的 output。 谢谢!

library(shiny)
library(ggplot2) ## Added as the diamonds data is under this package
data("diamonds") ## load data into the code

diamonds <- as.data.frame(diamonds) ## Added to explicitly read the data into the code
carat <- diamonds$carat ## Added to explicitly create the variable carat as a workaround the issue

# Define UI for application that predicts the price of diamonds from its carat and plots linear models using the diamonds data on R
shinyUI(fluidPage(
        titlePanel("Predict Price of Diamond from its Carat"),
        sidebarLayout(
                sidebarPanel(
                        sliderInput("sliderCarat", "What is the Carat of the diamond?", min = 0.2, max = 5.01, value = 3),
                        checkboxInput("showModel1", "Show/Hide Model 1", value = TRUE),
                        checkboxInput("showModel2", "Show/Hide Model 2", value = TRUE),
                        checkboxInput("showModel3", "Show/Hide Model 3", value = TRUE)
                ),
                
                mainPanel(
                        plotOutput("plot1"),
                        h3("Predicted Price from Model 1:"),
                        textOutput("pred1"),
                        h3("Predicted Price from Model 2:"),
                        textOutput("pred2"),
                        h3("Predicted Price from Model 3:"),
                        textOutput("pred3")
                )
        )
))



shinyServer(function(input, output) {
        
        ## Model Generation: server.R Part 1
        
        model1 <- lm(price ~ carat, data = diamonds)
        model2 <- lm(price ~ carat + cut + color, data = diamonds)
        model3 <- lm(price ~ carat + depth + table, data = diamonds)
        
        model1pred <- reactive({
                caratInput <- input$sliderCarat
                predict(model1, newdata = data.frame(caratInput))
        })
        
        model2pred <- reactive({
                caratInput <- input$sliderCarat
                predict(model2, newdata = data.frame(caratInput))
        })
        
        model3pred <- reactive({
                caratInput <- input$sliderCarat
                predict(model3, newdata = data.frame(caratInput))
        })
        
        ## Price Prediction: server.R Part 2
        
        output$plot1 <- renderPlot({
                
                caratInput <- input$sliderCarat
                
                plot(diamonds$carat, diamonds$price, xlab = "Weight of the Diamond in Carat",
                     ylab = "Price in USD", bty = "n", pch = 16, xlim = c(0.2, 5.1), ylim = c(326, 18823))
                if(input$showModel1){
                        abline(model1, col = "red", lwd = 2)
                }
                if(input$showModel2){
                        abline(model2, col = "blue", lwd = 2)
                }
                if(input$showModel3){
                        abline(model3, col = "green", lwd = 2)
                }
                
                legend(25, 250, c("Model 1 Prediction", "Model 2 Prediction", "Model 3 Prediction"), pch = 16,
                       col = c("red", "blue", "green"), bty = "n", cex = 1.2)
                points(caratInput, model1pred(), col = "red", pch = 16, cex = 2)
                points(caratInput, model2pred(), col = "blue", pch = 16, cex = 2)
                points(caratInput, model3pred(), col = "green", pch = 16, cex = 2)
        })
        
        output$pred1 <- renderText({
                model1pred()
        })
        
        output$pred2 <- renderText({
                model2pred()
        })
        
        output$pred3 <- renderText({
                model3pred()
        })
})

shinyApp(ui = ui, server = server)

结果:

在此处输入图像描述

预期结果:

在此处输入图像描述

问题是如何为预测定义新数据。 由于您在 model 的公式中指定了carat ,因此新的 data.frame 也需要包含此列。

尝试

model1pred <- reactive({
                caratInput <- input$sliderCarat
                predict(model1, newdata = data.frame(carat = caratInput))
        })

(以及其他预测)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM