Home > Back-end >  Multiple Linear regression using modules in R Shiny
Multiple Linear regression using modules in R Shiny

Time:10-06

I am trying to use R Shiny Modules to do a linear regression. The idea is as follows:

  1. I have a module which takes a data.table as an input to the function.
  2. The module UI then asks for the X and Y variables for the linear regression
  3. and upon click of a button should perform the linear regression and print the summary output.

I tried the below code, but is throwing up an error which I am unable to fix.

Kindly help.

Code


Linear.Regression.UI <- function(id, data.tibble){
  ns <- NS(id)
  tagList(
    actionButton(ns("ClickforRegression"), label = "Click Here to Do Regression"),
    
    selectInput(inputId = ns("Regression.Y.Input"),
                label = "Select Regression Dependent Variable",
                choices = names(data.tibble),
                ),
    selectInput(inputId = ns("Regression.X.Input"),
                label = "Select Regression Independent Variables",
                choices= names(data.tibble),
                multiple=TRUE),
    
    verbatimTextOutput("Linear.Model.Output.Summary")
  )#end of tagList

}#end of Linear.Regression.UI


Linear.Regression.Server <- function(id){
  moduleServer(id, function(input, output, session){
    ns <- session$ns
    
    observeEvent(eventExpr = input$ClickforRegression,
                 linear.model <-  lm(reformulate(input$Regression.X.Input, input$Regression.Y.Input), data = data.tibble)
                 )#end of observeEvent
    
    
    output$Linear.Model.Output.Summary <- renderPrint(summary(linear.model()))
    
    
  })#end of moduleServer
  
}


Regression.App <- function(data.tibble){
  ui <- fluidPage(
    Linear.Regression.UI("Data", data.tibble = iris)
  )
  
  server <- function(input, output, session)
  {
    Linear.Regression.Server("Data")
  }
  
  shinyApp(ui, server)
}


Regression.App()

Error

Listening on http://127.0.0.1:3883
Warning: Error in is.data.frame: object 'data.tibble' not found
  [No stack trace available]

CodePudding user response:

The code also needed a ns fix but the main fix was passing the data to the server:

Linear.Regression.UI <- function(id, data.tibble){
  ns <- NS(id)
  tagList(
    actionButton(ns("ClickforRegression"), label = "Click Here to Do Regression"),
    
    selectInput(inputId = ns("Regression.Y.Input"),
                label = "Select Regression Dependent Variable",
                choices = names(data.tibble),
    ),
    selectInput(inputId = ns("Regression.X.Input"),
                label = "Select Regression Independent Variables",
                choices= names(data.tibble),
                multiple=TRUE),
    
    verbatimTextOutput(ns("Linear.Model.Output.Summary"))
  )#end of tagList
  
}#end of Linear.Regression.UI


Linear.Regression.Server <- function(id, data.tibble ){
  moduleServer(id, function(input, output, session){
    ns <- session$ns
    
    linear.model<- reactiveVal()
    observeEvent(eventExpr = input$ClickforRegression,{ message("sdf");
      linear.model(lm(reformulate(input$Regression.X.Input, input$Regression.Y.Input), data = data.tibble))}
    )#end of observeEvent
    
    
    output$Linear.Model.Output.Summary <- renderPrint(summary(linear.model()))
    
    
  })#end of moduleServer
  
}


Regression.App <- function(data.tibble){
  ui <- fluidPage(
    Linear.Regression.UI("Data", data.tibble = iris)
  )
  
  server <- function(input, output, session)
  {
    Linear.Regression.Server("Data", data.tibble = iris)
  }
  
  shinyApp(ui, server)
}


Regression.App()
  • Related