【问题标题】:How to upload data (as a reactive object for processing in multiple places) and download data in the same App?如何在同一个App中上传数据(作为多处处理的反应对象)和下载数据?
【发布时间】:2021-12-29 23:37:19
【问题描述】:

请参阅最底部的最终工作 MWE 代码,其中包含 gss 提出的建议!

在运行以下 MWE 代码时,如果用户单击侧边栏面板顶部的“浏览”按钮并访问 csv 文件,则该文件中的数据将被上传并以表格形式显示在主面板中。但是,我还希望将 csv 数据作为矩阵或向量反映在该侧边栏面板中呈现的矩阵中。如底部的图像所示。关于如何做到这一点的任何想法?

在下面的 MWE 代码中,您可以看到我的注释掉的 observeEvent() 由“浏览”触发,我尝试这样做,包括使用 shinyMatrix 包的 updateMatrixInput() 函数。它不起作用。

最终我计划取消表格渲染,因为两次查看相同的数据是没有意义的。但现在为了测试,我正在尝试并行运行它们。

library(shiny)
library(shinyMatrix)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Optionally choose input file (csv)", accept = ".csv"),
      matrixInput("matrix1", 
                  value = matrix(c(5), ncol = 1, dimnames = list("Matrix 1 input",NULL)),
                  cols =  list(names = FALSE),
                  class = "numeric"),
      downloadButton("download")
    ),
    mainPanel(
      tableOutput("contents"),
    )
  )
)

server <- function(input, output, session) {
  
  output$contents <- renderTable({
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    req(file)
    validate(need(ext == "csv", "Please upload a csv file"))
    data <- read.csv(file$datapath, header = TRUE)
    data
  })
  
  # observeEvent(input$file1,{
  #   tmpMat <- matrix(c(data), ncol=1)
  #   updateMatrixInput(session, inputId = "matrix1", value = tmpMat)
  # })
  
  output$download <- downloadHandler(
    filename = function() {
      paste("Inputs","csv",sep=".")
    },
    content = function(file) {
      write.csv(input$matrix1[1,1], file,row.names=FALSE)
    }
  )
 
}

shinyApp(ui, server)

基于 gss 解决方案的最终工作 MWE 代码:

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Optionally choose input file (csv)", accept = ".csv"),
      matrixInput("matrix1", 
                  value = matrix(c(5), 
                          ncol = 1, 
                          dimnames = list("Matrix 1 input",NULL)
                  ),
                  cols =  list(names = FALSE),
                  class = "numeric"
      ),
      downloadButton("download")
    ),
    mainPanel(
      tableOutput("contents"),
    )
  )
)

server <- function(input, output, session) {
  
  input_file <- reactive({
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    req(file)
    validate(need(ext == "csv", "Please upload a csv file"))
    read.csv(file$datapath, header = TRUE)
  })
  
  output$contents <- renderTable({
    input_file()
  })
  
  observeEvent(input$file1,{
    updateMatrixInput(session, 
                      inputId = "matrix1", 
                      value = matrix(as.matrix(input_file()),
                                     ncol=1,
                                     dimnames = list("Matrix 1 input",NULL)
                              )
    )
                      
  })
  
  output$download <- downloadHandler(
    filename = function() {
      paste("Inputs","csv",sep=".")
    },
    content = function(file) {
      write.csv(input$matrix1[1,1], file,row.names=FALSE)
    }
  )
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny upload shiny-reactivity


    【解决方案1】:

    你需要改变这个:

    output$contents <- renderTable({
        file <- input$file1
        ext <- tools::file_ext(file$datapath)
        req(file)
        validate(need(ext == "csv", "Please upload a csv file"))
        data <- read.csv(file$datapath, header = TRUE)
        data
      })
    

    你需要的是有一个响应式,以后可以在不同的地方使用,所以只需:

    input_file <- reactive({
        file <- input$file1
        ext <- tools::file_ext(file$datapath)
        req(file)
        validate(need(ext == "csv", "Please upload a csv file"))
        data <- read.csv(file$datapath, header = TRUE)
        data
    })
    
    output$contents <- renderTable({
        input_file()
      })
    

    您现在也可以在其他地方使用input_file()

    【讨论】:

    • 嗨 gss!这样可行!但是,当我包含已编辑帖子(顶部)中显示的更改后的 observeEvent() 时,在运行应用程序并上传 csv 文件时,将返回更新后的矩阵,并在数字周围加上括号(例如 [5000])。知道为什么它被括号括起来以及如何删除括号吗?我以前从未见过这种情况。可能是我的一些错误没有对 csv 上传进行子集化。
    • 我不知道,“为什么”,但我找到了如何改变它 - 使用 tmpMat &lt;- as.matrix(input_file(), ncol = 1)tmpMat &lt;- as.matrix(input_file()),即不要使用 matrix,而是使用 as.matrix 并执行不要使用c()。您也可以尝试其他组合,但我看到提到的这两个不显示括号。
    猜你喜欢
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    相关资源
    最近更新 更多