【发布时间】:2018-02-14 18:42:53
【问题描述】:
我想知道是否可以让用户在使用应用程序时从本地硬盘上传数据(可能是 .CSV 格式)到 Shiny 应用程序中,然后 Shiny 会动态执行分析。
目前,对于此类分析,我将 RData/CSV 格式的数据保存在 WWW 文件夹中,然后 Shiny 从那里获取数据 - 但这并不是真正动态的。
任何这样的想法都将受到高度赞赏。
【问题讨论】:
我想知道是否可以让用户在使用应用程序时从本地硬盘上传数据(可能是 .CSV 格式)到 Shiny 应用程序中,然后 Shiny 会动态执行分析。
目前,对于此类分析,我将 RData/CSV 格式的数据保存在 WWW 文件夹中,然后 Shiny 从那里获取数据 - 但这并不是真正动态的。
任何这样的想法都将受到高度赞赏。
【问题讨论】:
是的,Shiny 有一个名为fileInput 的输入,可让用户上传数据。来自文档here:
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = input$header)
})
}
shinyApp(ui, server)
}
【讨论】: