【问题标题】:Running scripts on uploaded csv file in shiny以闪亮的方式在上传的 csv 文件上运行脚本
【发布时间】:2017-01-14 00:57:16
【问题描述】:

我正在尝试构建一个应用程序,该应用程序从用户那里获取一个 csv 文件,将其上传,然后用户填写一些文本框,这些文本框稍后将填充数据框中的特定列,单击“GO”按钮,一些脚本在后台运行,我们有一个可供下载的数据框。问题是整个反应式架构使得逐步建立算法变得困难。你能帮我建立这样做的框架吗?理想情况下,如下所示

shinyUI(fluidPage(
           titlePanel("Uploading Files"),
           fileInput('file1', 'Choose file to upload',
                         accept = c('text/csv',
                         'text/comma-separated-values',
                         'text/tab-separated-values',
                         'text/plain','.csv','.tsv')),        
           dateInput('date',"Select when the file was uploaded",
                      value = NULL,
                      format = 'yyyy-mm-dd'),
           textInput('text1','Type what will be in column 6'),
           textInput('text2','Type what will be in column 7'),
           actionButton('go','go'),
           tableOutput('readytable')

现在有了前面,我想: 1. 从用户的 csv 加载数据框 2. 等待用户填写其他输入框 3. 单击“开始”后,在数据框上运行一堆函数用户插入的输入,例如df$column6 <- input$text1,之后我留下了一个准备好再次写入csv文件的数据框。提前感谢任何链接/建议

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用反应变量来控制闪亮的反应。这是您的问题的示例。请注意,下载按钮在 RStudio 查看器上不起作用,因此如果您想使用下载按钮,请在浏览器中启动应用程序。

    library(shiny)
    runApp(list(
      ui = shinyUI(pageWithSidebar(
      headerPanel('Uploading Files'),
      sidebarPanel( 
               fileInput('file1', 'Choose file to upload',
                             accept = c('text/csv',
                             'text/comma-separated-values',
                             'text/tab-separated-values',
                             'text/plain','.csv','.tsv')),
        uiOutput('buttonsUI'), br(),
        uiOutput('downloadUI')
      ),
      mainPanel(
        tableOutput('readytable')
      )
    )), 
      server = shinyServer(function(input, output) {
        # variables to control the sequence of processes 
        controlVar <- reactiveValues(fileReady = FALSE, tableReady = FALSE)
        # to keep the data upload
        dat <- NULL
        # handle the file reading
        observeEvent(input$file1, {
          controlVar$fileReady <- FALSE
          if (is.null(input$file1))
            return()
          inFile <- input$file1
          dat <<- read.csv(inFile$datapath)
          if(!is.data.frame(dat))
            return()
          controlVar$fileReady <- TRUE
        })
        # show buttons only when file is uploaded
        output$buttonsUI <- renderUI({
          if (controlVar$fileReady)
            div(
              dateInput('date','Select when the file was uploaded',
                          value = NULL,
                          format = 'yyyy-mm-dd'),
               textInput('text1','Type what will be in column 6'),
               textInput('text2','Type what will be in column 7'),
               actionButton('go','go')
    
            )
        })
        # show a download button only if data is ready
        output$downloadUI <- renderUI({
          if (controlVar$tableReady)
                downloadButton('downloadData', 'Download')
        })
        # add columns to dat and run some script on it
        observeEvent(input$go, {
          controlVar$tableReady <- FALSE
          if (!is.null(input$text1))
            dat$column6 <<- input$text1
          if (!is.null(input$text2))
            dat$column7 <<- input$text2
          # simulate running a cool script on dat
          Sys.sleep(2)
          controlVar$tableReady <- TRUE  
        })
        # render table after uploading file or running the script
        output$readytable <- renderTable({
          input$go
          if (controlVar$fileReady || controlVar$tableReady)
             dat
        })
        # handle the download button
        output$downloadData <- downloadHandler(
          filename = function() { 'newData.csv' },
          content = function(file) {
            write.csv(dat, file)
          }
        )
      })
    ))
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多