Please see code below. I get the error: Warning: Error in UseMethod: no applicable method for 'predict' applied to an object of class "c('reactiveExpr', 'reactive', 'function')"
library(shiny)
library(ggplot2)
library(caret)
# Define server logic required to draw a histogram
function(input, output, session) {
output$data <- renderTable({
mtcars[, c(input$variable), drop = FALSE]
}, rownames = TRUE)
output$data2 <- renderTable({
mtcars[, c(input$variable2), drop = FALSE]
}, rownames = TRUE)
output$summary <- renderText({
paste("SUMMARY STATISTICS")
})
output$sum <- renderTable({
summary(mtcars)
})
model <- reactive({
lm(mtcars[, c(input$variable)] ~ mtcars[ , c(input$variable2)], data = mtcars)
})
output$reg <- renderText({
paste("REGRESSION COEFFICIENTS AND RESIDUALS")
})
output$coef <- renderTable({
as.data.frame(model()$coefficients)
})
output$res <- renderTable({
as.data.frame(model()$residuals)
})
output$num <- renderText({
req(input$obs)
})
input_value <- reactive({
input$obs
})
predic <- reactive({
predict(model, newdata = data.frame(input_value))
})
output$p <- renderText({
predic()
})
# output$plot <- renderPlot({
# ggplot(mtcars, aes(mtcars[,c(input$variable)], mtcars[, c(input$variable2)]))
# })
# output$data3 <- renderTable({
# brushedPoints(mtcars, input$plot_brush, xvar = input$variable, yvar = input$variable2)
# })
}
I found the solution to the orevious error. instead of model in the predict function I have to use model() because it ia a reactive function. Please see below. But this time I get a warning :" Warning: 'newdata' had 1 row but variables found have 32 rows". So this script asks input from the user (just one number) to make a prediction with that number, however the function returns many values, not just 1. How can I fix this?
library(shiny)
library(ggplot2)
library(caret)
library(shinyPredict)
# Define server logic required to draw a histogram
function(input, output, session) {
output$data <- renderTable({
mtcars[, c(input$variable), drop = FALSE]
}, rownames = TRUE)
output$data2 <- renderTable({
mtcars[, c(input$variable2), drop = FALSE]
}, rownames = TRUE)
output$summary <- renderText({
paste("SUMMARY STATISTICS")
})
output$sum <- renderTable({
summary(mtcars)
})
model <- reactive({
lm(mtcars[, c(input$variable)] ~ mtcars[ , c(input$variable2)], data = mtcars)
})
output$reg <- renderText({
paste("REGRESSION COEFFICIENTS AND RESIDUALS")
})
output$coef <- renderTable({
as.data.frame(model()$coefficients)
})
output$res <- renderTable({
as.data.frame(model()$residuals)
})
output$num <- renderText({
req(input$obs)
})
input_value <- reactive({
req(input$obs)
})
predic <- reactive({
predict(model(), newdata = data.frame(input_value()))
})
output$p <- renderText({
predic()
})
# output$plot <- renderPlot({
# ggplot(mtcars, aes(mtcars[,c(input$variable)], mtcars[, c(input$variable2)]))
# })
# output$data3 <- renderTable({
# brushedPoints(mtcars, input$plot_brush, xvar = input$variable, yvar = input$variable2)
# })
}
I realized that if I want to predict for one value, I just need to output the coefficients to build the equation
system
Closed
May 19, 2025, 5:51pm
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.