r - 如何从按日期排序的基于目录的数据库中导出 .csv?

标签 r database export shiny

Shiny 的新用户,我想我已经完成了大部分设置。我的代码从基于目录的数据库中检索某些数据。输出是一个 csv 文件,我希望能够直接从 shiny 下载该文件。

下面的代码是非常基本的:

  1. 它采用输入 日期并分隔 D/M/Y 并创建搜索词
  2. 还创建了代表不同文件的某些关键字以供搜索
  3. 它搜索特定文件夹并检索 CSV。文件
  4. 然后从该文件中仅检索标记为输入之一的数据
  5. 此数据应该可以 CSV 或 XLS 格式通过 Shiny 应用程序下载/输出

我的输入是:

inputId = "NaVDate" = 这是日期(用于搜索和检索 .csv)

inputId = "FundID" = 这是在第 4 步中用来对数据进行排序的 ID

输出是:

Forwards_Fund

这些已经包含在下面的代码中:

UI.R

library(shiny)


ui <- fluidPage(

  # Date input by user
  dateInput(inputId = "NaVDate", label = "Select NaV Date", 
            format = "yyyy-mm-dd", startview = "month", weekstart = 0),
  # 
  textInput(inputId = "FundID", label = "Enter Fund ID (Numeric Only)", value = "", width = NULL, placeholder = NULL),

   downloadButton("downloadData", "Download")


)

SERVER.R

server <- function(input, output) {

  # extract inputed month / year / day and vectorize ( inputed in YYYY-MM-DD format)


  timestrip   <- reactive({unlist(strsplit(as.character(input$NaVDate), split = ""))}) 


  year   <- paste(timestrip[1], timestrip[2], timestrip[3], timestrip[4], sep = "")
  month  <- paste(timestrip[6], timestrip[7], sep = "")
  day    <- paste(timestrip[9], timestrip[10], sep = "")

  Date      = paste(year, month, day, sep = "")


  # create directory search criteria for the required files - eg.("XCP4P487FOFFET_20160315.CSV")

  Forwards  = paste0("*FOFFET_", Date, ".CSV$")


  # create main directory file path

  file_path <- paste0("C:/Users/dell Optiplex/Desktop/Equilibrium Weekly Macro/", year,"/", month) 


  # Search through file_path for criteria and create individual .csv filepath

  Forwards_CSV_path  = list.files(file_path, 
                                  pattern = Forwards, full.names=TRUE, 
                                  ignore.case=TRUE)


  # Read Data from CSV files

  Forwards_data = do.call(rbind, lapply(Forwards_CSV_path, function(x) read.csv(x, sep = ";", stringsAsFactors = FALSE)))


  # Extract data from the inputed fund code (input$FundID) 

  Forwards_Fund  <- reactive({
    Forwards_data[which(Forwards_data$Fund.Code == input$FundID),]
  })

  ## download above data from shiny app

output$downloadData <- downloadHandler(
    filename = function() {
      paste('data-', Sys.Date(), '.csv', sep='')
    },
    content = function(con) {
      write.csv(Forwards_Fund(), con)
    }
  )


}

shinyApp(server = server, ui = ui)

错误

Listening on http://127.0.0.1:6132
Warning: Error in [: object of type 'closure' is not subsettable
Stack trace (innermost first):
    44: paste
    43: server [#9]
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: print
Error in timestrip[1] : object of type 'closure' is not subsettable

编辑: - 更新

这是一款 Shiny 的应用程序,可从基于目录的数据库中检索 CSV 文件的名称。我将继续使用下载按钮对其进行升级。

我的桌面上有一个名为 EquilibriumWeeklyMacro 的文件

C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/

它包含多个按日期排序的文件

C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/2016/03/

在那里我有一个名为

的CSV文件
XCP4P487FOFFET_20160315.CSV

注意 FOFFET_20160315 结尾

如果我在 Shiny 应用程序框中输入 2016-03-15,则以下 Shiny 应用程序应该能够从该文件中检索 XCP4P487FOFFET_20160315.CSV

除了最后一部分,下面的代码有效:

output$ForwardText  =  renderText({list.files(as.character(FileDirectory()), 
                                                     pattern = as.character(ForwardSearchString()), full.names=TRUE, 
                                                     ignore.case=TRUE)
                                                      })

我知道问题出在上面。 这是应用程序的代码:

library(shiny)


# XCP4P487FOFFET_20160315.CSV
# C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/2016/03/

ui <- fluidPage(

  # Date input by user

  dateInput(inputId = "NaVDate", label = "Select NaV Date",
            format = "yyyy-mm-dd", startview = "month", weekstart = 0),

  textOutput("ForwardText")

)



  server <- function(input, output) {



    DateInput <-       reactive({format(as.Date(input$NaVDate), "%Y%m%d")})


    FileDirectory <- reactive({paste("C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/",
                                     substr(as.character(DateInput()), 1, 4), "/",
                                     substr(as.character(DateInput()), 5, 6),"/", sep = "") 
                                      })
    # Search String 

    ForwardSearchString <- reactive({paste("FOFFET_", as.character(DateInput()), ".CSV")})

    # Search through file_path for criteria and create individual .csv filepath


   output$ForwardText  =  renderText({list.files(as.character(FileDirectory()), 
                                                 pattern = as.character(ForwardSearchString()), full.names=TRUE, 
                                                 ignore.case=TRUE)
                                                  })

  }

shinyApp(server = server, ui = ui)

最佳答案

Shiny 有一个名为 downloadButton 的小部件,可以找到文档 here .

我已经集成到您的应用中:

library(shiny)


ui <- fluidPage(

  # Date input by user
  dateInput(inputId = "NaVDate", label = "Select NaV Date", 
            format = "yyyy-mm-dd", startview = "month", weekstart = 0),
  # 
  textInput(inputId = "FundID", label = "Enter Fund ID (Numeric Only)", value = "", width = NULL, placeholder = NULL),

  downloadButton("downloadData", "Download")

)

server <- function(input, output) {

  # extract inputed month / year / day and vectorize ( inputed in YYYY-MM-DD format)


  timestrip   <- reactive({unlist(strsplit(as.character(input$NaVDate), split = ""))}) 

  Forwards_Fund  <- reactive({
    year   <- paste(timestrip()[1], timestrip()[2], timestrip()[3], timestrip()[4], sep = "")
    month  <- paste(timestrip()[6], timestrip()[7], sep = "")
    day    <- paste(timestrip()[9], timestrip()[10], sep = "")

    Date      = paste(year, month, day, sep = "")


    # create directory search criteria for the required files - eg.("XCP4P487FOFFET_20160315.CSV")

    Forwards  = paste0("*FOFFET_", Date, ".CSV$")


    # create main directory file path

    file_path <- paste0("C:/Users/dell Optiplex/Desktop/Equilibrium Weekly Macro/", year,"/", month) 


    # Search through file_path for criteria and create individual .csv filepath

    Forwards_CSV_path  = list.files(file_path, 
                                  pattern = Forwards, full.names=TRUE, 
                                  ignore.case=TRUE)


    # Read Data from CSV files

    Forwards_data = do.call(rbind, lapply(Forwards_CSV_path, function(x) read.csv(x, sep = ";", stringsAsFactors = FALSE)))


    # Extract data from the inputed fund code (input$FundID) 


    Forwards_data[which(Forwards_data$Fund.Code == input$FundID),]
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste('data-', Sys.Date(), '.csv', sep='')
    },
    content = function(con) {
      write.csv(Forwards_Fund(), con)
    }
  )

}

shinyApp(server = server, ui = ui)

关于r - 如何从按日期排序的基于目录的数据库中导出 .csv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36066564/

相关文章:

r - NbClust 包错误

r - 将多行分类为一个变量

sql-server - 查找 Azure SQL 数据库中的性能问题

c++ - VS2015 Express & VS2017 : Document cannot be opened: It has renamed, 删除或移动

c++ - 通过 C++ 将 QML 项作为图像复制到剪贴板

r - 从 R 文件导入内容

javascript - 为什么 console.log 不是 Shiny for R 中的函数?

datatables - 使用 select 元素从数据表导出数据导出 select 元素中的每个选项

mysql - 如何从 MySql Workbench 连接到互联网中的 MySql 服务器?

mysql - 如何将多行插入到一个表中,然后使用自动递增的 ID 插入到另一个表中?数据库