How to display linear regression model output using shiny for R?

I have a shiny chunk that takes a CSV user input from a file. The file is then used to create a linear regression model. However, when the model is returned, I only see the table column of the value "Class", and not the model itself. Does anyone know why that might be?

Upload CSV File:

ui <- fluidPage(

titlePanel("Upload Transaction Data Set"),

sidebarLayout(

sidebarPanel(

  fileInput("file1", "Choose CSV File",
            multiple = FALSE,
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv"))
),

mainPanel(
tableOutput("prediction")

  )
 )
)

Apply server code to take the CSV input, and then output the model:

server = function(input,output){
  df = reactive({
  req(input$file1)
  read.csv(file = input$file1$datapath)
  })

  #Perform Regression

  output$prediction = renderTable({
   req(df())
   set.seed(2)
   split <- sample.split(thedata, SplitRatio=0.7)

   df <- subset(thedata, split=TRUE)
   Actual <- subset(thedata, split=FALSE)

   model = lm(Class ~.,data = df())
   prediction <- predict(model, Actual)

   plot.new()
   #plot(Actual$Class,type = "l",lty= 1.8,col = "red")
   lines(prediction, type = "l", col = "blue")
   plot(prediction,type = "l",lty= 1.8,col = "blue")

   predict_df = data.frame(Class = prediction)
   output_df = cbind(predict_df)
   return(output_df)
  })
}
shinyApp(ui,server)

I think that the problem could be that the mainPanel is displaying a table, OR because I am creating the model in the server code.. Can't seem to work it out..

Your are just returning one column (Class) from the renderTable() function because your are not binding it with nothing, what else do you expect to see?

predict_df = data.frame(Class = prediction)
   output_df = cbind(predict_df)
   return(output_df)
1 Like

Hey, thanks I noticed this. I'm very new to R but am learning loads already.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.