【发布时间】:2020-03-22 03:44:06
【问题描述】:
我是 Shiny 的新手(使用 R 工作了几个月),我正在尝试弄清楚处理多个上传文件的“闪亮”方式是什么。
我的目标是有一个用户可以上传多个文件的界面。必须从这些文件中生成大量图表形式的报告。根据我在 Shiny 教程中学到的知识,在服务器端,所有对象都是相互隔离的(这意味着它们无法读取,除非您显式调用另一个函数)。
正如您在下面的代码中看到的,这意味着我必须为每个绘图复制处理代码。这似乎效率低下。处理这个问题的“闪亮”方式是什么?
另外,我遗漏了一些对于示例来说并非绝对必要的代码。本质上,我需要做更多的处理,我不想为每个情节复制所有代码。
我特意询问的是服务器端代码。我想读入包含不同内容的多个文件。服务器端代码中的操作只是占位符,我实际上不想绑定任何东西,但我把它放在那里以保持代码简单。我希望能够对导入的数据框做任何我想做的事情。
library(shiny)
# Define UI for application
ui <- fluidPage(
# Sidebar with file input
sidebarLayout(
sidebarPanel(
fileInput("people", NULL, multiple = FALSE, accept = ".csv",
buttonLabel = "Browse...", placeholder = "people file"),
fileInput("info", NULL, multiple = FALSE, accept = ".csv",
buttonLabel = "Browse...", placeholder = "info file"),
),
# Show the results of the data processing
mainPanel(
imageOutput("plot"),
tableOutput("base_data")
)
)
)
# Define server logic required to process the data
server <- function(input, output) {
output$base_data <- renderTable({
if(is.null(input$people) | is.null(input$info)) {
} else {
people_file <- input$people
info_file <- input$info
people <- read.csv(people_file$datapath, stringsAsFactors = F, encoding = "UTF-8-BOM")
info <- read.csv(info_file$datapath, stringsAsFactors = F, fileEncoding = "UTF-8-BOM")
rbind(people, info)
}
})
output$plot <- renderImage({
if(is.null(input$people) | is.null(input$info)) {
outfile <- tempfile(fileext='.png')
png(outfile, width = 1200, height = 800, res = 200)
dev.off()
list(src = outfile, width = 1200, height = 800)
} else {
people_file <- input$people
info_file <- input$info
people <- read.csv(people_file$datapath, stringsAsFactors = F, encoding = "UTF-8-BOM")
info <- read.csv(info_file$datapath, stringsAsFactors = F, fileEncoding = "UTF-8-BOM")
outfile <- tempfile(fileext='.png')
png(outfile, width = 1200, height = 800, res = 200)
plot(nrow(people), nrow(info), type="b")
dev.off()
list(src = outfile, width = 1200, height = 800, alt = "questions"))
}
}, deleteFile = TRUE)
}
# Run the application
shinyApp(ui = ui, server = server)
这是我想要的伪代码示例:
[[ui]]
fileInput("people")
fileInput("info")
show(plot)
show(plot2)
show(df)
[[serverside]]
files <- source(input) {
people <- read.csv(input$people, stringsAsFactors = F, encoding = "UTF-8-BOM")
info <- read.csv(input$info, stringsAsFactors = F, encoding = "UTF-8-BOM")
}
contents <- plot(output) {
some_function(files$people, files$info)
plot(contents)
}
contents2 <- plot(output) {
some_other_function(files$people, files$info)
plot2 <- plot(contents2)
}
df <- table(output) {
cbind(files$people, files$info)
}
这是我现在拥有的伪代码,效率不高。
[[ui]]
fileInput("people")
fileInput("info")
show(plot)
show(plot2)
show(df)
[[serverside]]
contents <- plot(input, output) {
people <- read.csv(input$people, stringsAsFactors = F, encoding = "UTF-8-BOM")
info <- read.csv(input$info, stringsAsFactors = F, encoding = "UTF-8-BOM")
contents <- some_function(people, info)
plot(contents)
}
contents2 <- plot(input, output) {
people <- read.csv(input$people, stringsAsFactors = F, encoding = "UTF-8-BOM")
info <- read.csv(input$info, stringsAsFactors = F, encoding = "UTF-8-BOM")
contents <- some_other_function(people,info)
plot(contents)
}
df <- table(input, output) {
people <- read.csv(input$people, stringsAsFactors = F, encoding = "UTF-8-BOM")
info <- read.csv(input$info, stringsAsFactors = F, encoding = "UTF-8-BOM")
cbind(people, info)
}
【问题讨论】:
-
你好,恩佐利马。即使您删除了很多不必要的代码,实际上仍然有很多代码与实际问题并不真正相关,据我所知,这就是如何生成多个输出(例如,表格和绘图) 从单个对象。肯定有“闪亮”的方法可以做到这一点,但用一个更简单的可重现示例来证明这一点要容易得多。
-
我把它剪掉了。这样更好吗?