第一次闪亮的用户和我想我有这个设置的大部分。我从基于目录的数据库中检索某些数据的代码。输出是一个csv文件,我希望能够直接从shiny下载。
下面的代码是非常基本的:
它接受inputed
日期并分隔d/m/y并创建搜索项
某些代表不同文件的关键字也被创建用于搜索
它搜索特定文件夹并检索csv。文件
然后它只从这个文件中检索标记为inputs
的数据
这些数据应该可以通过闪亮的csv或xls格式的应用程序下载。
我的意见是:ouptuted
=这是日期(用于搜索和检索.csv)inputId = "NaVDate"
=这是在步骤4中对数据进行排序的id
输出为:
Forwards_Fund
这些已经包含在下面的代码中:
用户界面
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")
)
服务器.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
编辑:-更新
这是一个闪亮的应用程序,从基于目录的数据库中检索csv文件的名称。我将继续升级它与下载按钮。
我的桌面上有一个名为EquilibiumWeeklymacro的文件
C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/
它包含多个按日期排序的文件
C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/2016/03/
在那里我有一个csv文件叫做
XCP4P487FOFFET_20160315.CSV
注意
inputId = "FundID"
和FOFFET_
结尾如果我在shiny app框中输入
20160315
,那么下面的shiny app应该能够从该文件中检索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有一个称为adownloadButton
的小部件,可以找到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/