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

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

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!

  1. #This is the test page for step 1: set the import and export directories
  2. library(shiny)
  3. library(shinyFiles)
  4. ########## User interface ##########
  5. ui = fluidPage(
  6. h5(strong('Select target directory for importing raw data:')),
  7. shinyDirButton('folderImp',
  8. 'Directory Selection',
  9. 'Please select a folder'),
  10. verbatimTextOutput("dir_input_text"),
  11. h5(strong('Select target directory for exporting analysis results:')),
  12. shinyDirButton('folderExp',
  13. 'Directory Selection',
  14. 'Please select a folder'),
  15. verbatimTextOutput("dir_output_text")
  16. )#close ui, fluidpage
  17. ########## Server logic #########
  18. server = function(input, output, session) {
  19. #### Input for user browse and data upload ####
  20. switch(Sys.info()[['sysname']],
  21. Windows= {main_dir <- "c:/Users"},
  22. Linux = {main_dir <- "/usr"},
  23. Darwin = {main_dir <- "~"}) #R script running on a mac
  24. shinyDirChoose(input,
  25. 'folderImp',
  26. session=session,
  27. roots=c(main = main_dir),
  28. filetypes = c('csv'),
  29. hidden = FALSE,
  30. allowDirCreate = FALSE)
  31. input_pathway <- reactive({parseDirPath(session, input$folderImp) })
  32. output$dir_input_text <- renderPrint({ input_pathway() })
  33. #### Export directory for analysis results ####
  34. shinyDirChoose(input,
  35. 'folderExp',
  36. session=session,
  37. roots=c(main = main_dir),
  38. filetypes = c('csv'),
  39. hidden = FALSE,
  40. allowDirCreate = FALSE)
  41. output_pathway <- reactive({parseDirPath(session, input$folderExp) })
  42. output$dir_output_text <- renderPrint({ output_pathway() })
  43. } # close server
  44. 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?

  1. shinyDirChoose(input,
  2. 'folderImp',
  3. session=session,
  4. roots=c(main = main_dir),
  5. filetypes = c('csv'),
  6. hidden = FALSE,
  7. allowDirCreate = FALSE)
  8. input_pathway <- reactive({ input$folderImp })
  9. output$dir_input_text <- renderText({ paste(input_pathway()$path[-1], collapse = "/") })
  10. #### Export directory for analysis results ####
  11. shinyDirChoose(input,
  12. 'folderExp',
  13. session=session,
  14. roots=c(main = main_dir),
  15. filetypes = c('csv'),
  16. hidden = FALSE,
  17. allowDirCreate = FALSE)
  18. output_pathway <- reactive({ input$folderExp })
  19. 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.

  1. #This is the test page for step 1: set the import and export directories
  2. library(shiny)
  3. library(shinyFiles)
  4. ########## User interface ##########
  5. ui = fluidPage(
  6. h5(strong('Select target directory for importing raw data:')),
  7. shinyDirButton('folderImp',
  8. 'Directory Selection',
  9. 'Please select a folder'),
  10. verbatimTextOutput("dir_input_text"),
  11. h5(strong('Select target directory for exporting analysis results:')),
  12. shinyDirButton('folderExp',
  13. 'Directory Selection',
  14. 'Please select a folder'),
  15. verbatimTextOutput("dir_output_text")
  16. )#close ui, fluidpage
  17. ########## Server logic #########
  18. server = function(input, output, session) {
  19. #### Input for user browse and data upload ####
  20. switch(Sys.info()[['sysname']],
  21. Windows= {main_dir <- "c:/Users"},
  22. Linux = {main_dir <- "/usr"},
  23. Darwin = {main_dir <- "~"}) #R script running on a mac
  24. shinyDirChoose(input,
  25. 'folderImp',
  26. session=session,
  27. roots=c(main = main_dir),
  28. filetypes = c('csv'),
  29. hidden = FALSE,
  30. allowDirCreate = FALSE)
  31. input_pathway <- reactive({ input$folderImp })
  32. output$dir_input_text <- renderText({
  33. ip <- input_pathway()
  34. ifelse('path' %in% names(ip),
  35. paste(ip$path[-1], collapse = "/"),
  36. '')
  37. })
  38. #### Export directory for analysis results ####
  39. shinyDirChoose(input,
  40. 'folderExp',
  41. session=session,
  42. roots=c(main = main_dir),
  43. filetypes = c('csv'),
  44. hidden = FALSE,
  45. allowDirCreate = FALSE)
  46. output_pathway <- reactive({ input$folderExp })
  47. output$dir_output_text <- renderText({
  48. op <- output_pathway()
  49. ifelse('path' %in% names(op),
  50. paste(op$path[-1], collapse = "/"),
  51. '')
  52. })
  53. } # close server
  54. 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:

确定