是该问题的一些可重现的代码。但基本上我有一个应用程序可以有许多用户输入文件将具有类似的结构。我想有一个reactive()函数,它将迭代所有输入并附加数据返回。请参阅问题代码第78行的函数ReadData()。我可以循环遍历所有文件,因为我已经使用增量整数表示id相同,例如1输入id = File1,2输入id = File2等。
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)
提前感谢任何建议
答案 0 :(得分:1)
您几乎就是将File <- get(input_name, input)
行更改为File <- input[[input_name]]
。
这是我的工作版
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)