Dynamic model to predict values based multiple linear regression model in Rshiny

1,013 views
Skip to first unread message

verma....@gmail.com

unread,
May 20, 2018, 7:02:03 AM5/20/18
to Shiny - Web Framework for R

I am running a multiple linear regression model using an available training dataset mtcars.

Now I want to run this model against a test dataset. So I am trying to fetch test dataset manually from user on Shiny UI. User should have an option to select the dependant and independant variables, based on the independant and dependant variable selection, it should give the predicted value of the test dataset

Currently, its is giving an error "could not find function "modelq"

modelq is the linear regression model which I have built(based on mtcars dataset) to predict the values of the test dataset Code


library(shiny)
library(datasets)
library(caret)
library(shiny)
library(curl)

library(shiny)

library(shiny)

ui <- fluidPage(
  titlePanel("My first predictive model"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),
      tags$hr(),
      uiOutput("dependent"),
      uiOutput("independents"),
      tags$hr(),
      actionButton("action", "Predict!")
    ),
    mainPanel(
      verbatimTextOutput("regTab")
    )
  )
)

server <- function(input, output, session) {
  filedata <- reactive({
    infile <- input$file1
    if (is.null(infile)){
      return(NULL)      
    }
    read.csv(infile$datapath)
  })

  output$dependent <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("dependent","Select ONE variable as dependent variable from:",items)
  })
  output$independents <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    checkboxGroupInput('independents','Select the regressors', choices = names(df))
  })
  #regression formula
  pred12 <- eventReactive(input$action, {
   modelq <- lm(as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+"))),data=mtcars)
  pred1<-predict(modelq(),filedata())
  }
)
  #model
  output$regTab <- renderPrint({
    if(!is.null(input$independents)){
      pred12()
    } else {
      print(data.frame(Warning="Please select Model Parameters."))
    }
  })
}

shinyApp(ui, server)

pralhad kalkundre

unread,
May 26, 2020, 1:23:16 AM5/26/20
to Shiny - Web Framework for R
Hello !!

I was wondering whether you are able to resolve the below error.

Facing the same issue... 

For Me

unread,
Feb 6, 2021, 11:27:05 AM2/6/21
to Shiny - Web Framework for R
Hi,

Were you able to get this model to work?
If so, can you please help with the final code!

Reply all
Reply to author
Forward
0 new messages