从 Shiny a App 下载数据框作为 txt 文件

Downloading a data frame from Shiny a App as a txt file

这是我第一次出现堆栈溢出。对于我的问题的通用标题,我深表歉意,但我想不出任何其他不会太麻烦的标题。我正在努力将 shiny 中生成的数据框结果下载为 txt 文件。

下面是我做过的和我想做的例子。

这是我在 Shiny 中所做的:

##################################
## Load R packages ###############
##################################
library(dplyr)
library(tictoc)
library(lubridate)
library(shiny)
library(shinythemes)
library(zoo)

##################################
## create data for the example ###
##################################

df <- data.frame(date = as.Date((Sys.Date()-9):Sys.Date())
                 ,id = sample.int(10)
                 ,amount = rnorm(10, -1000, 300))
mapping <- data.frame(id = 1:10
                      ,Company = c("XP", "Itau", "Brad", "Paddy", "XYZ", "Tuco", "Sant", "Rolex", "ABC", "TaG")
                      ,Country = c("BR", "BR", "BR", "IR", "ES", "IT", "AG", "SW", "NO", "CY"))

##################################
############ Define UI ###########
##################################

ui <- fluidPage(theme = shinytheme("cerulean"), 
                navbarPage(
                  "PoG",
                  tabPanel("Filter",
                           sidebarLayout(
                             sidebarPanel(
                               dateRangeInput("dates",tags$h3("Date Range:") ,
                                              start = as.Date(Sys.Date()-10),
                                              end = as.Date(Sys.Date()) )

                             ), # sidebarPanel
                             mainPanel(
                               h1("Filtered values"),

                               h4("Summary Table"),
                               DT::dataTableOutput("Final")

                             ) # mainPanel
                          ) # sidebar Panel
                      )# sidebar Layout
                ) # navbarPage
                , downloadButton("export_data", "Download")
                ) # fluidPage


##################################
#### Server Function UI ##########
##################################  

Server <- function(input, output, session) {

  output$Final <- DT::renderDataTable({

    df %>% filter(date >= input$dates[1] & date <= input$dates[2])
  })

  }

##################################
#### Create Shiny object #########
##################################  

shinyApp(ui = ui, server = Server)

这是我希望在按下下载按钮后发生的事情:

 Records <- 1:dim(df %>% filter(date >= input$dates[1] & date <= input$dates[2]))[1]

    Tst <- data.frame(test = rep("", length(Records) * 9))
    Tst$test <-as.character(Tst$test)

for (i in 1:length(Records)){

      position <- which(mapping$id %in% df$id[i])
      Entity <- as.character(mapping$Company[position])
      Country <- as.character(mapping$Country[position])
      DealDate <- format(df$date[i], "%d/%m/%Y")
      Dealer <- "SvcUser"
      Comments <- paste0(Entity," ", Country)


      Tst$test[i + (i-1) * 8]   <- "@ActualDeal"
      Tst$test[i+1 + (i-1) * 8] <- paste0("** RECORD ",Records[i])
      Tst$test[i+2 + (i-1) * 8] <- "$NEW"
      Tst$test[i+3 + (i-1) * 8] <- paste0("Entity=",Entity)
      Tst$test[i+4 + (i-1) * 8] <- paste0("DealDate=",DealDate)
      Tst$test[i+5 + (i-1) * 8] <- paste0("Dealer = ", Dealer)
      Tst$test[i+6 + (i-1) * 8] <- paste0("Comments=",Comments)
      Tst$test[i+7 + (i-1) * 8] <- "$INSERT"
      Tst$test[i+8 + (i-1) * 8] <-  ""           


    }


output$export_data <- downloadHandler(
      filename = "test.qxt"
      , content = function(){
        write.table(Tst, file = "test.txt", row.names = F, sep = "\t", col.names = F, quote = F)
      }


      )

当按下应用程序中的下载按钮时,我很难将 object Tst 保存为文本文件。

非常感谢任何帮助。

我的应用程序中有类似的情况。我只是将它链接到一个输出,它对我有用:

output$MyOutput <- renderPrint({
  req(input$export_data) # Ensure that export_data value is available

  # Replace the next instructions with your own code
  Data <- fromJSON(file = input$export_data$datapath)
  # ... more code to correctly transform my data 
  print(Data)
})

我通过执行以下操作解决了这个问题:

第一:我创建了两个函数

函数 1:

FilterData <- function(x1, x2){

  Result <- df %>% filter(date >= x1 & date <= x2)

}

函数 2:

PrintTable <- function(x1, x2){

  Records <- 1:dim(df %>% filter(date >= x1 & date <= x2))[1]

    Tst <- data.frame(test = rep("", length(Records) * 9))
    Tst$test <-as.character(Tst$test)
    for (i in 1:length(Records)){

      position <- which(mapping$id %in% df$id[i])
      Entity <- as.character(mapping$Company[position])
      Country <- as.character(mapping$Country[position])
      DealDate <- format(df$date[i], "%d/%m/%Y")
      Dealer <- "SvcUser"
      Comments <- paste0(Entity," ", Country)


      Tst$test[i + (i-1) * 8]   <- "@ActualDeal"
      Tst$test[i+1 + (i-1) * 8] <- paste0("** RECORD ",Records[i])
      Tst$test[i+2 + (i-1) * 8] <- "$NEW"
      Tst$test[i+3 + (i-1) * 8] <- paste0("Entity=",Entity)
      Tst$test[i+4 + (i-1) * 8] <- paste0("DealDate=",DealDate)
      Tst$test[i+5 + (i-1) * 8] <- paste0("Dealer = ", Dealer)
      Tst$test[i+6 + (i-1) * 8] <- paste0("Comments=",Comments)
      Tst$test[i+7 + (i-1) * 8] <- "$INSERT"
      Tst$test[i+8 + (i-1) * 8] <-  ""           


    }

    return(Tst)
}   

然后我把Server函数改成如下:

Server <- function(input, output, session) {

  x1 <- reactive(input$dates[1])
  x2 <- reactive(input$dates[2])

  output$Final <- DT::renderDataTable({
    FilterData(x1(), x2())

  })



    data <- reactive({

      Test <- PrintTable(x1(), x2())

      return(Test)

      })




  output$export_data <- downloadHandler(
    filename = function() {
      paste("test.txt")  },

    content = function(file) {
      write.table(data(), file, row.names = F, sep = "\t", col.names = F, quote = F)
    }
  )




}

任务完成!