Reputation: 711
below is some reproducible code for the issue. But basically I have an app that can have many user input files that will be of similar structure. I want to have a reactive() function that will iterate over all the inputs and append the data to return. See the function ReadData() at line 78 of the code for the issue. I can loop over all the files because I have made there id's the same with an incremental integer, e.g. 1 input id = File1, 2 input id = File2 etc.
library('shiny')
library('shinythemes')
## adding the conditional statements
ui =
navbarPage("Page Title",
tabPanel("Panel 1",
sidebarPanel(
## Add Name,
## Number of surveys analysising
numericInput("n_values", "Number of columns in next panel:", 1, min = 1, max = 3)
),
mainPanel(
tags$div(
h2("Home Page")
)
)
),
tabPanel("Panel 2",
fixedPage(theme = "flatly",
fixedRow(
column(2,"First Column",
fileInput("File1", "Choose a CSV files", multiple = F),
actionButton("CheckData", "Validate Input"),
p("Click the button to check the data was read in correctly")
),
conditionalPanel(condition = "input.n_values >= 2",
column(2,"Second Column",
fileInput("File2", "Choose a CSV files", multiple = F),
p("Click the button to check the data was read in correctly")
)
),
conditionalPanel(condition = "input.n_values >= 3",
column(2,"Second Column",
fileInput("File3", "Choose a CSV files", multiple = F),
p("Click the button to check the data was read in correctly")
)
)
),
fixedRow(
column(12,
verbatimTextOutput("errorText2")
)
)
)
)
)
server = function(input, output,session) {
## Call the error message function and print
output$errorText2 <- renderText({
validate(
if (input$n_values == 1) {
need(!is.null(input$File1)
, 'You need to input the files before we can validate the data. Please select all the necessary files.')
} else if (input$n_values == 2) {
need(!is.null(input$File1) & !is.null(input$File2)
, 'You need to input the files before we can validate the data. Please select all the necessary files.')
} else if (input$n_values == 3) {
need(!is.null(input$File1) & !is.null(input$File2) & !is.null(input$File3)
, 'You need to input the files before we can validate the data. Please select all the necessary files.')
}
)
validate(ReadDataAndCheck())
})
## read in an user defined number of inputs and append them together
ReadData <- reactive({
print("enter: CheckData")
temp_df = NULL;
for (i in 1:input$n_values) {
input_name = Paste("File", i)
print(names(input))
print(input_name)
File <- get(input_name, input) ## get an error
## File <- eval(expr = text(x = paste0("input$",input_name))) ## also tried but get error cannot find object 'input'
if (!is.null(File)) {
this_df = read.csv(File$datapath, header = T,stringsAsFactors = F);
if (i == 1) {
temp_df = this_df
} else {
temp_df = rbind(temp_df, this_df);
}
}
}
if (!is.null(temp_df)) {
return(temp_df)
}
})
ReadDataAndCheck <- eventReactive(input$CheckData, {
print("enter: Validating data")
Data = ReadData();
if(is.null(Data)) {
return("error couldn't read data")
}
return("successfully read in data")
})
}
shinyApp(ui, server)
Thanks in advance for any suggestions
Upvotes: 1
Views: 2169
Reputation: 5003
You are almost there just change the line File <- get(input_name, input)
to File <- input[[input_name]]
.
This is a working version for me
library('shiny')
library('shinythemes')
## adding the conditional statements
ui =
navbarPage("Page Title",
tabPanel("Panel 1",
sidebarPanel(
## Add Name,
## Number of surveys analysising
numericInput("n_values", "Number of columns in next panel:", 1, min = 1, max = 3)
),
mainPanel(
tags$div(
h2("Home Page")
)
)
),
tabPanel("Panel 2",
fixedPage(theme = "flatly",
fixedRow(
column(2,"First Column",
fileInput("File1", "Choose a CSV files", multiple = F),
actionButton("CheckData", "Validate Input"),
p("Click the button to check the data was read in correctly")
),
conditionalPanel(condition = "input.n_values >= 2",
column(2,"Second Column",
fileInput("File2", "Choose a CSV files", multiple = F),
p("Click the button to check the data was read in correctly")
)
),
conditionalPanel(condition = "input.n_values >= 3",
column(2,"Second Column",
fileInput("File3", "Choose a CSV files", multiple = F),
p("Click the button to check the data was read in correctly")
)
)
),
fixedRow(
column(12,
verbatimTextOutput("errorText2")
)
)
)
)
)
server = function(input, output,session) {
## Call the error message function and print
output$errorText2 <- renderText({
validate(
if (input$n_values == 1) {
need(!is.null(input$File1)
, 'You need to input the files before we can validate the data. Please select all the necessary files.')
} else if (input$n_values == 2) {
need(!is.null(input$File1) & !is.null(input$File2)
, 'You need to input the files before we can validate the data. Please select all the necessary files.')
} else if (input$n_values == 3) {
need(!is.null(input$File1) & !is.null(input$File2) & !is.null(input$File3)
, 'You need to input the files before we can validate the data. Please select all the necessary files.')
}
)
validate(ReadDataAndCheck())
})
## read in an user defined number of inputs and append them together
ReadData <- reactive({
print("enter: CheckData")
temp_df = NULL;
for (i in 1:input$n_values) {
input_name = paste0("File", i)
print(names(input))
print(input_name)
File <- input[[input_name]] ## get an error
## File <- eval(expr = text(x = paste0("input$",input_name))) ## also tried but get error cannot find object 'input'
if (!is.null(File)) {
this_df = read.csv(File$datapath, header = T,stringsAsFactors = F);
if (i == 1) {
temp_df = this_df
} else {
temp_df = rbind(temp_df, this_df);
}
}
}
if (!is.null(temp_df)) {
return(temp_df)
}
})
ReadDataAndCheck <- eventReactive(input$CheckData, {
print("enter: Validating data")
Data = ReadData();
if(is.null(Data)) {
return("error couldn't read data")
}
return("successfully read in data")
})
}
shinyApp(ui, server)
Upvotes: 1