0

I am attempting to run a regression that allows users to determine regression inputs, and then provide an output that is the regression summary. For whatever reason, the output is not coming out correct, and I have looked everyone on the internet to find a solution. I am hoping somebody can help.

For clarification, this is in shiny.

Here is my server code:

shinyServer(
function(input,output,session) {
mod <- eventReactive(input$analysis,{
  response <- data[,2]
  explan1 <- data[,input$Explan1]
  explan2 <- data[,input$Explan2]
  explan3 <- data[,input$Explan3]
  mod1 <- lm(response~explan1+explan2+explan3)
}      )


output$modelSummary <- renderPrint({
  (summary(mod()$mod1))
})

output$ColumnNames <- renderPrint({
  as.data.frame(colnames(data))
})
}
)
summary(model)

And my ui code

shinyUI(
  fluidPage(
    titlePanel("What does it take for a Hockey Team to Win?"),
    titlePanel("Please select the column numbers for three variables to regress on"),
    sidebarLayout(

      sidebarPanel(
        verbatimTextOutput("ColumnNames"),
        numericInput("Explan1","Explanatory Variable 1",3,min = 3, max = 13),
        numericInput("Explan2","Explanatory Variable 2",4,min = 3,max = 13),
        numericInput("Explan3","Explanatory Variable 3",5,min = 3, max = 13)
      ),
      mainPanel(
        actionButton("analysis","Analyze!"),
        verbatimTextOutput("modelSummary")

      )
      )
      )
      )

When I run the app, select the input columns (which are by number rather than name. I hope to fix this later) and click analyze, I get the following output:

Length Class Mode 0 NULL NULL

I haven't been able to find much relevant information on this output. I hope you all can help.

Thank you in advance.

Alex Dometrius
  • 812
  • 7
  • 20

1 Answers1

0

You're just calling the reactive incorrectly, it should be: summary(mod()) instead of summary(mod()$mod1). Reactives behave very much like functions the way that they return objects.

Here is a fully reproducible example, with an example on how to use a formula instead of individually selecting the columns:

col_names <- names(mtcars)

ui <- fluidPage(
  sidebarPanel(
    verbatimTextOutput("ColumnNames"),
    selectInput("Response", "Response Variable", choices = col_names, selected = "mpg"),
    selectInput("Explan1","Explanatory Variable 1", choices = col_names, selected = "cyl"),
    selectInput("Explan2","Explanatory Variable 2", choices = col_names, selected = "disp"),
    selectInput("Explan3","Explanatory Variable 3", choices = col_names, selected = "wt")
  ),
  mainPanel(
    actionButton("analysis","Analyze!"),
    verbatimTextOutput("modelFormula"),
    verbatimTextOutput("modelSummary")

  )
)

server <- function(input, output, session) {

  myformula <- reactive({
    expln <- paste(c(input$Explan1, input$Explan2, input$Explan3), collapse = "+")
    as.formula(paste(input$Response, " ~ ", expln))
  })

  mod <- eventReactive(input$analysis, {
    lm(myformula(), data = mtcars)
  })

  output$modelFormula <- renderPrint({
    myformula()
  })

  output$modelSummary <- renderPrint({
    summary(mod())
  })
}

shinyApp(ui, server)

Screenshot:

enter image description here

mlegge
  • 6,763
  • 3
  • 40
  • 67
  • Thank you so much, who knew it was that easy. Additionally, I initially had the function set up as a selectInput, but for whatever reason wasn't getting the results correctly. This little fix worked. Cheers! – Brandon Mauriello Apr 24 '18 at 12:11
  • Thanks @BrandonMauriello, if its answered your question its customary to click the green checkmark beneath the voting buttons to indicate that to others. – mlegge Apr 24 '18 at 12:27