Home > Back-end >  Using Shiny inputs in lm()
Using Shiny inputs in lm()

Time:10-27

I have the following shiny input panel:

inputPanel(selectInput(inputId = "engagement_state", label = "Choose Engagement State:",
                   choices = states.of.engagement2, selected = states.of.engagement2[1]), 
       selectInput(inputId = "product", label = "Choose a Product:",
                   choices = unique.products, selected = unique.products[1]),
       selectInput(inputId = "model_inputs", label = "Choose Input Variables:",
                   choices = model.variables, selected = model.variables, multiple = TRUE))

Using these inputs, I want to create a model. The dependent variable is input$engagement_state and the independent variables are the variables included input$model_inputs as well as another column that is added into the data shown below

renderDataTable({
  aggregated.engagement <- data[get(variable.product) != input$product, .(agg_engagement = mean(get(input$engagement_state), na.rm=T)), by=id][, agg_engagement := ifelse(agg_engagement == "NaN", 0, agg_engagement)]
  
  model.data <- merge(data[get(variable.product) == input$product], aggregated.engagement, by='id')
  inputs <- c(input$model_inputs, input$product, "aggregated.engagement")
  
  model <- lm(input$engagement_state ~ inputs, model.data)
 
})

aggregated.engagement is based on the product that is selected in the inputs, and I then aggregated that variable with the rest of the inputs. I understand why this isn't working in lm() since it isn't pulling variables but I am not sure how to set the model parameters to be set to what is being pulled from the input and what is being calculated from that data.

Once the model is created, I will be using some of the outputs to create a table.

CodePudding user response:

I can't say for sure without your data, but I think the issue is that you need to make a formula object from your inputs. Here is an example:

library(tidyverse)
library(shiny)


ui <- fluidPage(
  selectInput(inputId = "var.x1", 
              label = strong("X-Variable1"),
              choices = colnames(mtcars),
              selected = "cyl"),
  selectInput(inputId = "var.x2", 
              label = strong("X-Variable2"),
              choices = colnames(mtcars),
              selected = "hp"),
  selectInput(inputId = "var.y", 
              label = strong("y-Variable"),
              choices = colnames(mtcars),
              selected = "mpg"),
   verbatimTextOutput('lm')
)

server <- function(input, output, session) {
  
  output$lm <- renderPrint(expr = {
    inputs <- paste(c(input$var.x1, input$var.x2), collapse = "   ")
    forms <- as.formula(paste0(input$var.y, "~",  inputs))
    model <- lm(forms, data = mtcars)
    return(summary(model))
  })
}

shinyApp(ui, server)

CodePudding user response:

If i understood correctly what you want is:

renderDataTable({
  aggregated.engagement <- data[get(variable.product) != input$product, .(agg_engagement = mean(get(input$engagement_state), na.rm=T)), by=id][, agg_engagement := ifelse(agg_engagement == "NaN", 0, agg_engagement)]
  
  model.data <- merge(data[get(variable.product) == input$product], aggregated.engagement, by='id')
  inputs <- c(input$model_inputs, input$product, "aggregated.engagement")
  model_formula <- as.formula( paste(input$engagement_state, "~", paste(inputs, collapse = " ")))
  model <- lm(model_formula,  model.data)
 
})
  • Related