Reputation: 397
I am creating a shiny app that allows you to upload an excel file with some data (dependent and independent variables) for the purpose of running a linear regression.
I am using read.xlsx to load the excel file which will output a table in the first tab. In the second tab i want to have a widget which allows you to select the dependent variables and another input widget that asks you to choose the independent variables. These variables will be a function of the loaded data column headers. So assume the loaded data looks like this:
A B C D E F G
22000 0.605 0.352 1.125 103.5 162 7107.263017
22000 0.495 0.352 1.375 126.5 162 4569.734496
22000 0.495 0.352 1.375 103.5 198 4649.524562
18000 0.495 0.352 1.125 126.5 198 4495.776272
Where G is the independent variable. I would like the column headers to be used as the basis for the variable selection on my second tab. The issue I am facing is linking the loaded excel files headers(column names) to be my variable selector as I define them as my selected variables in my UI as:
selectizeInput('inv',"In", choices = var, multiple = TRUE),
selectizeInput('out',"Out", choices = pred, multiple = FALSE)
So var and pred are defined under server and hence generate an error :object 'var' not found.
How can I workaround this. This is the code so far:
ui <- fluidPage(
titlePanel("Hi"),
sidebarLayout(position = "left",
sidebarPanel(
conditionalPanel(condition = "input.tabs1==1",
tags$style(type='text/css', ".well { max-width: 20em; }"),
# Tags:
tags$head(
tags$style(type="text/css", "select[multiple] { width: 100%; height:10em}"),
tags$style(type="text/css", "select { width: 100%}"),
tags$style(type="text/css", "input { width: 19em; max-width:100%}")
),
# Select filetype:
selectInput("readFunction", "Function to read data:", c(
# Base R:
"read.table",
"read.csv",
"read.csv2",
"read.delim",
"read.delim2",
"readWorksheet",
"read_excel",
"read.xlsx"
)),
# Argument selecter:
htmlOutput("ArgSelect"),
# Argument field:
htmlOutput("ArgText"),
# Upload data:
fileInput("file", "Upload data-file:"),
# Variable selection:
htmlOutput("varselect"),
br(),
textInput("name","Dataset name:","Data")),
conditionalPanel(condition = "input.tabs1==2",
#fileInput('file', 'Choose file to upload.'),
selectizeInput('invar',"Select Regression Input Variables", choices = varnames, multiple = TRUE),
selectizeInput('outvar',"Select Regression Output Variable", choices = predictors, multiple = FALSE)
),
mainPanel(
tabsetPanel(id="tabs1",
tabPanel("Data File",value = 1,tableOutput("table")),
tabPanel("LM Plot", value=2, plotOutput("PlotLM")))
)
)
))
server<-function(input, output) {
options(shiny.maxRequestSize=30*1024^2)
### Argument names:
ArgNames <- reactive({
Names <- names(formals(input$readFunction)[-1])
Names <- Names[Names!="..."]
return(Names)
})
# Argument selector:
output$ArgSelect <- renderUI({
if (length(ArgNames())==0) return(NULL)
selectInput("arg","Argument:",ArgNames())
})
## Arg text field:
output$ArgText <- renderUI({
fun__arg <- paste0(input$readFunction,"__",input$arg)
if (is.null(input$arg)) return(NULL)
Defaults <- formals(input$readFunction)
if (is.null(input[[fun__arg]]))
{
textInput(fun__arg, label = "Enter value:", value = deparse(Defaults[[input$arg]]))
} else {
textInput(fun__arg, label = "Enter value:", value = input[[fun__arg]])
}
})
### Data import:
Dataset <- reactive({
if (is.null(input$file)) {
# User has not uploaded a file yet
return(data.frame())
}
args <- grep(paste0("^",input$readFunction,"__"), names(input), value = TRUE)
argList <- list()
for (i in seq_along(args))
{
argList[[i]] <- eval(parse(text=input[[args[i]]]))
}
names(argList) <- gsub(paste0("^",input$readFunction,"__"),"",args)
argList <- argList[names(argList) %in% ArgNames()]
Dataset <- as.data.frame(do.call(input$readFunction,c(list(input$file$datapath),argList)))
return(Dataset)
})
# Select variables:
output$varselect <- renderUI({
if (identical(Dataset(), '') || identical(Dataset(),data.frame())) return(NULL)
# Variable selection:
selectInput("vars", "Variables to use:",
names(Dataset()), names(Dataset()), multiple =TRUE)
})
# Show table:
output$table <- renderTable({
if (is.null(input$vars) || length(input$vars)==0) return(NULL)
return(Dataset()[,input$vars,drop=FALSE])
})
Upvotes: 0
Views: 1478
Reputation: 151
You might try to use uiOutput
and renderUI
to do that.
in the server:
varnames <- reactive({
colnames(Dataset())
})
output$selectize1 <- renderUI({
selectizeInput('invar',"Select Regression Input Variables", choices = varnames(), multiple = TRUE)
})
in the ui :
uiOutput('selectize1')
Upvotes: 2