在我的应用程序,用户需要选择一个文件夹,该文件夹中,他需要选择一个文件(文件名后缀为“.seg”)依赖闪亮的物体
此代码工作 -
library(shiny)
ui <- shinyUI(fluidPage(
# select a folder
column(2, absolutePanel(fixed = TRUE, width = '180px',
selectInput("pick_a_folder", label = '', selected='choose a folder',
choices = setNames(as.list(c('choose a folder',
basename(list.dirs(recursive = FALSE)))),
c('choose a folder',
basename(list.dirs(recursive = FALSE))))))),
# select a file
column(2, absolutePanel(fixed = TRUE, width = '180px',
conditionalPanel(condition='!(input.pick_a_folder=="choose a folder")',
uiOutput('fileselection'))))
))
server <- shinyServer(function(input, output) {
# dinamic file selection. find the files list after folder is choosen
output$fileselection <- renderUI({
selectInput('pick_file', '', selected = 'choose a file',
choices=setNames(as.list(c('choose a file',basename(list.files(path=input$pick_a_folder,recursive=FALSE, pattern='\\.seg$')))),
c('choose a file',basename(list.files(path = input$pick_a_folder, recursive = FALSE, pattern='\\.seg$')))))
})
})
shinyApp(ui = ui, server = server)
问题是,如果我在运行代码后向工作目录添加文件夹,则不会显示该文件夹。 于是,我就夹选择移动到服务器,并使其依赖于一个刷新按钮,但我得到一个错误的list.files
错误:无效的“路径”的说法 这是我的代码 -
library(shiny)
ui <- shinyUI(fluidPage(
# refresh butten for root directory
column(1, absolutePanel(fixed=TRUE, actionButton("refresh_wd", "refresh"))),
# select a folder
column(2, absolutePanel(fixed = TRUE, width = '180px', uiOutput('folderselection'))),
# select a file
column(2, absolutePanel(fixed = TRUE, width = '180px',
conditionalPanel(condition='!(input.pick_a_folder=="choose a folder")',
uiOutput('fileselection'))))
))
server <- shinyServer(function(input, output) {
# refresh root directory
wd_folders <- eventReactive(input$refresh_wd, {
basename(list.dirs(recursive = FALSE))
})
output$folderselection <- renderUI({
selectInput('pick_a_folder', '', selected = 'choose a folder',
choices = setNames(as.list(c('choose a folder', wd_folders())),
c('choose a folder', wd_folders())))
})
# dinamic file selection. find the file list after folder is choosen
output$fileselection <- renderUI({
selectInput('pick_a_file', '', selected = 'choose a file',
choices=setNames(as.list(c('choose a file',basename(list.files(path=input$pick_a_folder,recursive=FALSE, pattern='\\.seg$')))),
c('choose a file',basename(list.files(path = input$pick_a_folder, recursive = FALSE, pattern='\\.seg$')))))
})
})
shinyApp(ui = ui, server = server)
因为你使用eventReactive()
将只显示文件夹,甚至文件夹选择列表中点击某人后o任何帮助将不胜感激
只是为了确保:您是否希望用户在服务器端选择文件,还是从自己的计算机中选择文件? –
从我的脚本在哪里,在服务器 –
所以'fileInput()'不是你正在寻找的然后:http://shiny.rstudio.com/reference/shiny/latest/fileInput.html –