问题描述
如何修改代码以允许浏览用户的子文件夹?
我在“用户”下找不到需要使用的子文件夹,其代码如下。
当我单击“用户”以查找子文件夹或被选中时,将显示Warning: Error in [: object of type 'closure' is not subsettable [No stack trace available]
。
ui <- fluidPage(
shinyDirButton("Btn_GetFolder","Choose a folder",title = "Please select a folder:",buttonType = "default",class = NULL),textoutput("txt_file")
)
server <- function(input,output,session){
volumes = getVolumes()
observe({
shinyDirChoose(input,"Btn_GetFolder",roots = volumes,session =
session)
if(!is.null(input$Btn_GetFolder)){
# browser()
myInputDir1 <- parseDirPath(volumes,input$Btn_GetFolder)
listBands <- list.files(myInputDir1,full.names = T)
output$txt_file <- renderText(listBands)
#Call function here.....
}
})
}
shinyApp(ui = ui,server = server)
解决方法
getVolumes
的帮助位于“值”部分:
函数返回可用卷的命名向量
因此,volume
是一个函数,需要进行评估以返回可用的卷。这也解释了您得到的错误:仅使用volumes
意味着您实际上提供了一个函数(基本上是一个闭包),后来在shinyDirChoose
中的某个地方被子集化了-不起作用。
library(shiny)
library(shinyFiles)
ui <- fluidPage(
shinyDirButton("Btn_GetFolder","Choose a folder",title = "Please select a folder:",buttonType = "default",class = NULL),textOutput("txt_file")
)
server <- function(input,output,session){
volumes = getVolumes()
observe({
shinyDirChoose(input,"Btn_GetFolder",roots = volumes(),session =
session)
if(!is.null(input$Btn_GetFolder)){
# browser()
myInputDir1 <- parseDirPath(volumes,input$Btn_GetFolder)
listBands <- list.files(myInputDir1,full.names = T)
output$txt_file <- renderText(listBands)
#Call function here.....
}
})
}
shinyApp(ui = ui,server = server)