R在一个被动响应中迭代多个输入
下面是该问题的一些可复制代码。但基本上我有一个应用程序,可以有许多用户输入文件,将类似的结构。我希望有一个reactive()函数,它将迭代所有输入并附加数据以返回。有关此问题,请参见代码第78行的函数ReadData()。我可以在所有文件上循环,因为我已经用一个递增的整数使它们的id相同,例如1输入id=File1,2输入id=File2等等R在一个被动响应中迭代多个输入,r,loops,shiny,reactive,R,Loops,Shiny,Reactive,下面是该问题的一些可复制代码。但基本上我有一个应用程序,可以有许多用户输入文件,将类似的结构。我希望有一个reactive()函数,它将迭代所有输入并附加数据以返回。有关此问题,请参见代码第78行的函数ReadData()。我可以在所有文件上循环,因为我已经用一个递增的整数使它们的id相同,例如1输入id=File1,2输入id=File2等等 library('shiny') library('shinythemes') ## adding the conditional statement
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)
library('shinny')
图书馆(“shinythemes”)
##添加条件语句
用户界面=
navbarPage(“页面标题”,
选项卡面板(“面板1”,
侧栏面板(
##加上名字,
##进行分析的调查次数
数值输入(“n_值”,“下一面板中的列数:”,1,最小值=1,最大值=3)
),
主面板(
标签$div(
h2(“主页”)
)
)
),
选项卡面板(“面板2”,
fixedPage(theme=“flatly”,
固定道(
第(2)列,“第一列”,
文件输入(“文件1”,“选择CSV文件”,多个=F),
操作按钮(“检查数据”、“验证输入”),
p(“单击按钮检查数据是否正确读取”)
),
条件面板(condition=“input.n\u values>=2”,
第(2)栏,“第二栏”,
文件输入(“文件2”,“选择CSV文件”,多个=F),
p(“单击按钮检查数据是否正确读取”)
)
),
条件面板(condition=“input.n\u values>=3”,
第(2)栏,“第二栏”,
文件输入(“文件3”,“选择CSV文件”,多个=F),
p(“单击按钮检查数据是否正确读取”)
)
)
),
固定道(
第(12)栏,
逐字输出(“errorText2”)
)
)
)
)
)
服务器=功能(输入、输出、会话){
##调用错误消息函数并打印
输出$errorText2您几乎就在那里了只要更改行文件如果您执行输入[[paste0(“文件”,i)]]
,它是否工作?
?再次感谢,我正在努力处理reactivevalues对象,有时输入像列表一样,有时我通常的列表代码不工作。
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)