【问题标题】:Altering dataframe with R shiny用 R 闪亮改变数据框
【发布时间】:2018-09-26 22:57:58
【问题描述】:

我是一般编码的新手,目前正在为我的应用程序制作 R 闪亮的应用程序。 它的目的是

  1. 上传csv文件
  2. 有多个复选框。如果勾选,数据会走相应的脚本。
  3. 导出新数据

我已经观看了教程,但我目前在反应方面遇到了一些困难。 我也尝试浏览其他问题,但由于我不熟悉编码,我发现很难从他们的示例中挑选出我需要的内容。

我目前完成了正确的导入和导出功能,并为正文编写了脚本。但是,我不确定如何将这个“主体”合并到服务器端。

这是“body”之一,没有考虑到Shiny:

file1 <- file1[ grep("REVERSE", file1[,c(1)], fixed = FALSE, invert = TRUE),]

Ui 在某处

...  fileInput('file1'
....  checkboxInput(inputId = "rmDecoy",
                      label = "Remove Decoy Entry",
                      value = TRUE
        ),
....  mainPanel(
        tableOutput('contents')

虽然这是我目前写的服务器端,只有导出功能:

server <- function(input, output) {
  getData <- reactive({
    inFile <- input$file1
    if (is.null(input$file1))
      return(NULL)
    read.csv(inFile$datapath, header=input$header, sep=input$sep, 
             quote=input$quote)
  })

  output$contents <- renderTable(
    getData()
  )

  output$downloadData <- downloadHandler(
    filename = function() { 
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      write.csv(getData(), file)
    })
}

当我使用output$rmDecoy 时它有点工作,但是当我将它与下载数据功能一起使用时,它就停止了。

因此,我的问题是

  1. 我的理解是,您并没有尝试直接更改输入。相反,您正在渲染新表、更改它并导出它。我了解 R Shiny 的原理吗?
  2. 如何将上述脚本合并到服务器中?

感谢您的帮助。

【问题讨论】:

    标签: r shiny shiny-server shiny-reactivity


    【解决方案1】:

    一个稍微精简的工作示例如下所示。请注意,我用简单的前两行替换了您的数据操作步骤file1 &lt;- file1[ grep("REVERSE", file1[,c(1)], fixed = FALSE, invert = TRUE),]。如果您在其他地方从不需要应用程序中未处理的数据,您也可以将此步骤移至 getData 并仅使用一个 reactive

    希望这会有所帮助!

    library(shiny)
    
    ui <- fluidPage(
      fileInput('file1','file1'),
      tableOutput('table_to_show'),
      downloadButton('downloadData', label = "Download")
    )          
    
    server <- function(input, output) {
      getData <- reactive({
        inFile <- input$file1
        if (is.null(input$file1))
          return(NULL)
        read.csv(inFile$datapath)
      })
    
      contents <- reactive({
        dat<- getData()
        print(dat)
        # manipulations to uploaded data here.
        dat <- dat[1:2,]
      })
    
      output$table_to_show <- renderTable(
      contents()
      )
    
      output$downloadData <- downloadHandler(
        filename = function() { 
          paste("data-", Sys.Date(), ".csv", sep="")
        },
        content = function(file) {
          write.csv(contents(), file)
        })
    }
    shinyApp(ui,server)
    

    【讨论】:

    • 哇!非常感谢您的惊人回答!由于没有通知,我不知道我的问题得到了回答,很抱歉没有很快注意到。只是一个简单的问题,我的应用程序将有多个复选框,这些复选框具有多个独立更改数据框的功能。如果是这样,其他函数会位于代码中的什么位置?谢谢!
    • 所有操作最好放在# manipulations to uploaded data here.。如果解决了您的问题,请考虑accepting the answer,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-06-21
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2017-11-15
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多