Create a dynamic dataframe based on user’s inputs in R Shiny UI using multiple linear regression model


#1

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

Now I want to run this model against a test dataset (which has exactly same column names as mtcars 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)

#2

You have a line that refers to modelq is a function.

pred1<-predict(modelq(),filedata())

With the () after modelq, you're indicating to R that modelq() is a function.

I think if you get rid of those suffix parenthasis you'll be good to go.


#3

now, its not giving any error message, but also not giving any output, once I hit predict button


#4

Its also a similar problem that i am facing here..could i enquire if you were able to solve it