使用Shiny应用中的verbatimTextOutput和shinyDirChoose时,可以看到目录路径。

huangapple go评论69阅读模式
英文:

Seeing the directory path when using verbatimTextOutput and shinyDirChoose in a Shiny app

问题

以下是您要翻译的内容:

"I have an MRE of my app, where I am trying to choose a directory for importing csv files. I would like to have the path for a chosen directory shown in a text box below the button. So far, I get character(0) before I select a directory, and after I choose a directory I get the error: object of type 'environment' is not subsettable. Does anyone have any ideas what may be causing this? Thanks!"

"UPDATE: I have it working now that so I can see the pathway after a directory is chosen, but before I choose a directory I see this error: $ operator is invalid for atomic vectors. Does anyone have another way for me to set this up so that I don't get this error?"

请注意,代码部分未被翻译。

英文:

I have an MRE of my app, where I am trying to choose a directory for importing csv files. I would like to have the path for a chosen directory shown in a text box below the button. So far, I get character(0) before I select a directory, and after I choose a directory I get the error: object of type 'environment' is not subsettable. Does anyone have any ideas what may be causing this? Thanks!

#This is the test page for step 1: set the import and export directories

library(shiny)
library(shinyFiles)


########## User interface ##########
ui = fluidPage(
  
  h5(strong('Select target directory for importing raw data:')),
  
  
  shinyDirButton('folderImp', 
                 'Directory Selection', 
                 'Please select a folder'),
  
  verbatimTextOutput("dir_input_text"),
  
  h5(strong('Select target directory for exporting analysis results:')),
  
  shinyDirButton('folderExp', 
                 'Directory Selection', 
                 'Please select a folder'),
  
  verbatimTextOutput("dir_output_text")
  
)#close ui, fluidpage


########## Server logic #########

server = function(input, output, session) {
  
  #### Input for user browse and data upload ####
  
  switch(Sys.info()[['sysname']],
         Windows= {main_dir <- "c:/Users"},
         Linux  = {main_dir <- "/usr"},
         Darwin = {main_dir <- "~"}) #R script running on a mac
  
  shinyDirChoose(input, 
                 'folderImp', 
                 session=session,
                 roots=c(main = main_dir),
                 filetypes = c('csv'),
                 hidden = FALSE,
                 allowDirCreate = FALSE)
  
  input_pathway <- reactive({parseDirPath(session, input$folderImp) })

  output$dir_input_text <- renderPrint({ input_pathway() })
  
  
  #### Export directory for analysis results ####
  
  shinyDirChoose(input, 
                 'folderExp', 
                 session=session,
                 roots=c(main = main_dir),
                 filetypes = c('csv'),
                 hidden = FALSE,
                 allowDirCreate = FALSE)
  
  output_pathway <- reactive({parseDirPath(session, input$folderExp) })
  
  output$dir_output_text <- renderPrint({ output_pathway() })
  
} # close server

shinyApp(ui = ui, server = server)

UPDATE
I have it working now that so I can see the pathway after a directory is chosen, but before I choose a directory I see this error:
$ operator is invalid for atomic vectors. Does anyone have another way for me to set this up so that I don't get this error?

  shinyDirChoose(input, 
                 'folderImp', 
                 session=session,
                 roots=c(main = main_dir),
                 filetypes = c('csv'),
                 hidden = FALSE,
                 allowDirCreate = FALSE)
  
  input_pathway <- reactive({ input$folderImp })

  output$dir_input_text <- renderText({ paste(input_pathway()$path[-1], collapse = "/") })
  
  
  #### Export directory for analysis results ####
  
  shinyDirChoose(input, 
                 'folderExp', 
                 session=session,
                 roots=c(main = main_dir),
                 filetypes = c('csv'),
                 hidden = FALSE,
                 allowDirCreate = FALSE)
  
  output_pathway <- reactive({ input$folderExp })
  
  output$dir_output_text <- renderText({ paste(output_pathway()$path[-1], collapse = "/") })

答案1

得分: 1

output$dir_input_text <- renderText({ paste(input_pathway()$path[-1], collapse = "/") }) 需要您的反应性 input_pathway() 方法返回一个类似列表的对象,该对象具有一个名为 "path" 的字段,可以通过 $ 运算符访问。在选择文件路径之前,“path”字段为NULL。同样适用于 output$dir_output_text <- renderText({ paste(output_pathway()$path[-1], collapse = "/") }) 为了避免错误,您应该检查 input_pathway()output_pathway() 方法是否返回一个实际具有 "path" 字段的对象。

英文:

output$dir_input_text <- renderText({ paste(input_pathway()$path[-1], collapse = "/") }) expects your reactive input_pathway() method to return an list-like object that has a field called "path" that can be accessed via the $ operator. Until you select a file path, the "path" field is NULL. The same is true for output$dir_output_text <- renderText({ paste(output_pathway()$path[-1], collapse = "/") }) in order to avoid the error you should check to see if input_pathway() and output_pathway() methods return an object that actually has a "path" field.

#This is the test page for step 1: set the import and export directories

library(shiny)
library(shinyFiles)


########## User interface ##########
ui = fluidPage(
  
  h5(strong('Select target directory for importing raw data:')),
  
  
  shinyDirButton('folderImp', 
                 'Directory Selection', 
                 'Please select a folder'),
  
  verbatimTextOutput("dir_input_text"),
  
  h5(strong('Select target directory for exporting analysis results:')),
  
  shinyDirButton('folderExp', 
                 'Directory Selection', 
                 'Please select a folder'),
  
  verbatimTextOutput("dir_output_text")
  
)#close ui, fluidpage


########## Server logic #########

server = function(input, output, session) {
  
  #### Input for user browse and data upload ####
  
  switch(Sys.info()[['sysname']],
         Windows= {main_dir <- "c:/Users"},
         Linux  = {main_dir <- "/usr"},
         Darwin = {main_dir <- "~"}) #R script running on a mac
  
  shinyDirChoose(input, 
                 'folderImp', 
                 session=session,
                 roots=c(main = main_dir),
                 filetypes = c('csv'),
                 hidden = FALSE,
                 allowDirCreate = FALSE)
  
  input_pathway <- reactive({ input$folderImp })
  
  output$dir_input_text <- renderText({ 
    ip <- input_pathway()
    ifelse('path' %in% names(ip),
           paste(ip$path[-1], collapse = "/"),
           '')
  })
  
  
  #### Export directory for analysis results ####
  
  shinyDirChoose(input, 
                 'folderExp', 
                 session=session,
                 roots=c(main = main_dir),
                 filetypes = c('csv'),
                 hidden = FALSE,
                 allowDirCreate = FALSE)
  
  output_pathway <- reactive({ input$folderExp })
  
  output$dir_output_text <- renderText({ 
    op <- output_pathway()
    ifelse('path' %in% names(op),
           paste(op$path[-1], collapse = "/"),
           '')
  })
  
} # close server

shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年5月30日 04:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定