我一直在尝试在Shiny中制作一些代码,允许将多个文件上传并组合成单个数据帧,但我能想出的唯一方法是使用reactive()命令来完成这项工作。然而,我需要我的数据框是可subsettable的,因为我有很多进一步的计算要做,而一个响应式数据框不断地给我"闭包"类型的对象是不可subsettable的。错误。
有什么办法我可以:
a)在不使用reactive()的情况下将多个文件读入静态数据框架(即与我为单个文件所做的方式相同),或b)将一个响应数据帧转换为一个静态数据帧?
我正在使用fileInput(…在UI中多个= TRUE)命令。
这是我服务器代码的相关部分(适用于单个文件上传但没有多个):
server <- function(input, output) {
output$contents <- renderTable({
req(input$file1)
tryCatch(
{
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
}
)
#create a subset of all the rows where pred > threshold
rows_above <- df[rowSums(df[6] > input$predthr) > 0, ]
#......my code goes on to do more analysis, subsetting and graphing
return(rows_above)
})
}
我已经尝试过这个而不是read.csv,但如果我尝试任何类型的子集,它会给我一个错误,我不知道如何将其转换为静态数据帧:
df<-reactive({
rbindlist(lapply(input$file1$datapath, fread),
use.names = TRUE, fill = TRUE)
})
我想我已经找到解决问题的办法了。根据这个答案的信息:如何在shiny中加载csv文件文件夹,我已经这样做了,这似乎适用于在多个文件中加载:
server <- function(input, output) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
inFile <- input$file1
if (is.null(inFile)) {
return(NULL)
} else {
df <- inFile %>%
rowwise() %>%
do({
read.csv(.$datapath)
})
}
#create a subset of all the rows where pred > threshold
rows_above <- df[(df[6] > input$predthr) > 0, ]
#......my code goes on to do more analysis, subsetting and graphing
return(rows_above)
})
}
我还必须从我的子集行中删除rowsum,但幸运的是,这似乎对我的数据没有任何影响。
谢谢你指出'df'不是一个很好的变量名——以后我会记住的。