我想创建一个Shiny应用程序,可以用它来质量控制天气数据。我希望用户能够告诉该应用程序一个列出所有气象站的目录,并让该应用程序列出所有气象站,以便可以一个一个地选择它们。为此,我想创建一个列出所有工作站的下拉菜单,该菜单由目录中的文件名确定。
我从其他答案中获得了大部分代码(我承认Shiny对我来说很难!)。我的问题是如何获取应用以列出文件(工作站ID)?
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
shinyDirButton("dir", "Input directory", "Upload"),
verbatimTextOutput("dir", placeholder = TRUE),
selectInput("Station", "Select Station:",
datapath)
),
mainPanel()
)
)
server <- function(input, output){
shinyDirChoose(
input,
'dir',
roots = c(home = 'C:\\'),
filetypes = c("csv")
)
dir <- reactive(input$dir)
output$dir <- renderText({
parseDirPath(c(home = 'C:\\'), dir())
})
observeEvent(ignoreNULL = TRUE,
eventExpr = {
input$dir
},
handlerExpr = {
# if (!"path" %in% names(dir())) return()
home <- normalizePath("C:\\")
datapath <-
file.path(home, paste(unlist(dir()$path[-1]), collapse = .Platform$file.sep))
},
output$files <- list.files(datapath$files)
})
# Run the application
shinyApp(ui = ui, server = server)
我遇到的问题是
output$files <- list.files(datapath$files)
和
selectInput("Station","Select Station:",datapath$files)
这些有什么问题?我不断收到错误消息,说“找不到函数“路径”,并且服务器崩溃。任何帮助都会很棒。
我不确定shinyDirChoose
等等来自哪里,因为我不认为它们是标准的Shiny函数。无论如何,output$files <- list.files(...)
由于它不在被动环境中而无法工作。我认为一种更好的方法是动态呈现下拉菜单(即使用renderUI/uiOutput
)。由于我不确定shinyDirButton
来自何处,因此我们可以通过一个简单的下拉菜单进行操作。
这是一个可重现的示例。我在顶部注释掉了代码;它会创建三个带有空白文件的嵌套目录,因此我们可以测试目录和文件选择器是否正常工作。我们正在尽一切getwd()
努力使此过程易于再现。
# create some test dirs
# lapply(c(1:3), function(x) {
# dir.create(paste0(getwd(), '/', x))
# lapply(c('a','b','c'), function(y) {
# file.create(paste0(getwd(), '/', x, '/', x, y))
# })
# })
library(shiny)
ui <- {
fluidPage(
fluidRow(
selectInput('dir_selector',
label = 'Select directory',
choices = list.files(getwd())),
verbatimTextOutput('selected_dir'),
uiOutput('file_selector')
)
)
}
server <- function(input, output, session) {
# get selected directory from input
output$selected_dir <- renderText(paste0('Selected directory: ', input$dir_selector))
# render dropdown menu of files
output$file_selector <- renderUI({
files <- list.files(paste0(getwd(), '/', input$dir_selector))
selectInput('file_selector',
label = 'Select file',
choices = files)
})
}
shinyApp(ui, server)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句