我想创建一个闪亮的应用程序,我可以用它来控制天气数据。我希望用户能够告诉应用程序与所有气象站列出的目录,并有应用程序列出所有的气象站,所以他们可以选择一个接一个。为此,我希望创建一个下拉菜单,其中列出了目录中的文件名所确定的所有电台。
我从其他答案中获得了大部分代码(我承认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)这些有什么问题?我一直收到一个错误,说‘找不到函数’路径‘,服务器崩溃了。任何帮助都是很好的。
发布于 2020-09-05 15:07:49
我不确定shinyDirChoose等是从哪里来的,因为我不认为它们是标准的闪亮函数。无论如何,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)https://stackoverflow.com/questions/63749476
复制相似问题