【问题标题】:Upload Data onto Shiny App dynamically [duplicate]将数据动态上传到 Shiny App [重复]
【发布时间】:2018-02-14 18:42:53
【问题描述】:

我想知道是否可以让用户在使用应用程序时从本地硬盘上传数据(可能是 .CSV 格式)到 Shiny 应用程序中,然后 Shiny 会动态执行分析。

目前,对于此类分析,我将 RData/CSV 格式的数据保存在 WWW 文件夹中,然后 Shiny 从那里获取数据 - 但这并不是真正动态的。

任何这样的想法都将受到高度赞赏。

【问题讨论】:

标签: r shiny


【解决方案1】:

是的,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)
}

【讨论】:

  • 谢谢,这符合我的目的。一个快速的问题:上传文件后是否有任何选项可以控制“上传完成”语句。我宁愿没有任何这样的声明。谢谢,
  • “上传完成”消息不可自定义,但如果您愿意,可以使用 css 隐藏整个上传栏,或者如果您想保留上传栏但更改消息,请查看 shinyjs。
猜你喜欢
  • 2018-01-02
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
相关资源
最近更新 更多