【问题标题】:Update on File Upload Shiny R文件上传 Shiny R 的更新
【发布时间】:2015-03-31 21:01:05
【问题描述】:

我正在尝试向我的 Shiny 应用程序添加文件上传功能。理想情况下,我希望用户能够上传 csv 文件,然后从下拉菜单中选择他们想要使用的变量。 当应用程序启动时,我最初预加载了一个数据集,下拉列表显示了该数据集的正确变量名称。但是,当我尝试上传不同的 csv 文件时,我似乎无法让下拉列表更新以显示上传的 csv 中包含的变量。这是我的代码:

server.R

library(shiny)
library(ggplot2)

inFile <- mtcars

shinyServer(function(input, output) {

  tableData <- reactive({
    inFile <<- input$file1

    if (!is.null(inFile)){
        read.csv(inFile$datapath, header=input$header, sep=input$sep,
                                                   quote=input$quote)
    }
    else {
        inFile <<- mtcars
    }
  })

  #for x values
  output$opt.x <- renderUI({
         selectInput("xcolumn", "X column to Plot", names(inFile))
  })
})

ui.R

library(shiny)

shinyUI(fluidPage(
     titlePanel("App"),
        sidebarLayout(
            sidebarPanel(
                fileInput('file1', 'Choose CSV File',
                     accept = c(
                            '.csv',
                            '.tsv')
                ),
                uiOutput("opt.x")
             ),
    mainPanel()
))

我也尝试将 output$opt.x 赋值放在读取文件的响应函数中,但这似乎没有帮助。

【问题讨论】:

  • 实际上我误读了这个问题,我打算的赏金是一种使用闪亮重新上传相同文件的方法。

标签: r csv shiny


【解决方案1】:

您好,不要将变量传递给全局环境,而是使用闪亮的反应功能,您的服务器应该是:

library(shiny)
library(ggplot2)

# Test data
# write.csv(x = iris, file = "iris.csv", row.names = FALSE)
# write.csv(x = data.frame(V1 = 1:10, V2 = rnorm(10)), file = "test.csv", row.names = FALSE)

shinyServer(function(input, output) {

  tableData <- reactive({
    inFile <- input$file1

    if (!is.null(inFile)){
      read.csv(inFile$datapath)
    } else {
      mtcars
    }
  })

  #for x values
  output$opt.x <- renderUI({
    selectInput("xcolumn", "X column to Plot", names(tableData()))
  })

 output$your_data <- renderTable({
   head(tableData())
 })
})

并将您的主面板替换为:

mainPanel(
  tableOutput("your_data")
)

我删除了 read.csv 中的参数,因为这些输入未在 UI 中定义。

编辑

试试这个应用程序,它不是一个闪亮的解决方案,而是一个 jquery/js 解决方案(区别在于 ui)(请参阅this answer):

library("shiny")
ui <- fluidPage(
  titlePanel("App"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept = c('.csv','.tsv')
      ),
      tags$script(
        '$( "#file1" ).on( "click", function() {
          this.value = null; 
        });'
      ),
      uiOutput("opt.x")
    ),
    mainPanel(
      tableOutput("your_data")
    )
    )
  )
server <- function(input, output) {

  tableData <- reactive({
    inFile <- input$file1
    if (!is.null(inFile)){
      read.csv(inFile$datapath)
    } else {
      mtcars
    }
  })
  #for x values
  output$opt.x <- renderUI({
    selectInput("xcolumn", "X column to Plot", names(tableData()))
  })

  output$your_data <- renderTable({
    head(tableData())
  })
}
shinyApp(ui = ui, server = server)

我使用的测试:

write.csv(x = data.frame(V1 = 1:10, V2 = rnorm(10)), file = "test.csv", row.names = FALSE)
write.csv(x = data.frame(V11 = 11:20, V12 = rnorm(10), ABC = letters[1:10]), file = "test.csv", row.names = FALSE)

【讨论】:

  • 我提供了赏金,但是,我仍然遇到问题。你如何重新上传相同的文件?我没有看到 Shiny 允许您多次上传同一个文件(即使该文件是在本地修改的)
  • 对于中等大小的文件,我得到“超出最大上传大小”。什么是限制?可以修改吗?
  • options(shiny.maxRequestSize=30*1024^2) (30MB)(见groups.google.com/forum/#!msg/shiny-discuss/rU3vwGMZexQ/…
  • 很好的答案 Victor,我也可以使用 tags$script 修改不同的 UI 项目。类似于“this.value = null”,我可以使用“itemID.value = null”
  • 谢谢!什么是 itemID ?你可能应该问一个新问题。
猜你喜欢
  • 2020-07-29
  • 1970-01-01
  • 2021-08-19
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
相关资源
最近更新 更多