Greetings!
I am using flexdashboard with shiny, and I am trying to create a program that:
A) Asks the user to upload a CSV file
B) Reads the file and on separate a chunk asks for "dependent" and "independent variables"
C) Displays two linear regression models, one of the actual results, another of the predicted.
Can shiny do this? Here's my code so far:
---
title: "Benchmarking Algorithms for Credit Card Fraud Detection"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
social: ["menu"]
runtime: shiny
options: (shiny.maxRequestSize = 500*1024^2)
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(MASS)
library(data.table)
library(datasets)
library(plotly)
library(dplyr)
thedata <- readxl::read_xlsx("data/transactionDataAlteredXLSX.xlsx")
Column {.tabset}
Step 1 - Upload File
# Reference = https://shiny.rstudio.com/articles/upload.html
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")),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
radioButtons("quote", "Quote",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"'),
tags$hr(),
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output, session) {
output$contents <- renderTable({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
if(input$disp == "head") {
return(head(df))
}
else {
return(df)
}
})
data <- reactive({
req(input$file1)
inFile <- input$file1
if (is.null(infile)){
return(NULL)
}
read.csv(infile$datapath)
}
)
}
shinyApp(ui, server)
Linear Regression Model
output$dependent <- renderUI({
df <- file1()
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 <- file1()
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,file1())
}
)
#model
output$regTab <- renderPrint({
if(!is.null(input$independents)){
pred12()
} else {
print(data.frame(Warning="Please select Model Parameters."))
}
})
server.R
library(shiny)
shinyServer(function(input,output) {
output$input_file <- renderable({
file_to_read = input$file
if(is.null(file_to_read)){
return()
}
read.table(file_to_read$datapath, sep = input$sep, header=input$header)
})
})
The code runs with no errors, displays upload chunk/tab, after I upload the CSV file the table appears, but nothing appears on the linear regression chunk/tab models for input after that.
Any ideas?