【问题标题】:Display selected folder path in Shiny在 Shiny 中显示选定的文件夹路径
【发布时间】:2017-12-08 17:21:01
【问题描述】:

我希望我的 Shiny 应用程序允许用户指定文件夹的路径(本地)并显示选定的路径。以下代码有效,但在选择文件夹之前,我无法弄清楚如何在 verbatimTextOutput 中隐藏“character(0)”。我尝试了条件面板(请参阅我的代码中的注释),但无法确定在这里使用什么作为条件(因为 shinyDirButton 不是标准操作按钮......)。谢谢!

library(shiny)
library(shinyFiles)

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  mainPanel(
    shinyDirButton("dir", "Input directory", "Upload"),
    #conditionalPanel(
      #condition = "???",
      verbatimTextOutput('dir')
    #)
  )
)

server <- function(input, output) {

  shinyDirChoose(input, 'dir', roots = c(home = '~'), filetypes = c('', 'txt','bigWig',"tsv","csv","bw"))

  dir <- reactive(input$dir)
  output$dir <- renderPrint({parseDirPath(c(home = '~'), dir())})

  observeEvent(
    ignoreNULL = TRUE,
    eventExpr = {
      input$dir
    },
    handlerExpr = {
      home <- normalizePath("~")
      datapath <<- file.path(home, paste(unlist(dir()$path[-1]), collapse = .Platform$file.sep))
    }
  )
}

# Run the application 
shinyApp(ui = ui, server = server)

我能找到的最接近的问题是这个,但它不能解决我的问题:R conditionalPanel reacts to output

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    在服务器函数中,使用renderText而不是renderPrint

    library(shiny)
    library(shinyFiles)
    
    # Define UI for application that draws a histogram
    ui <- fluidPage( # Application title
      mainPanel(
        shinyDirButton("dir", "Input directory", "Upload"),
        verbatimTextOutput("dir", placeholder = TRUE)  # added a placeholder
      ))
    
    server <- function(input, output) {
      shinyDirChoose(
        input,
        'dir',
        roots = c(home = '~'),
        filetypes = c('', 'txt', 'bigWig', "tsv", "csv", "bw")
      )
    
      dir <- reactive(input$dir)
      output$dir <- renderText({  # use renderText instead of renderPrint
        parseDirPath(c(home = '~'), dir())
      })
    
      observeEvent(ignoreNULL = TRUE,
                   eventExpr = {
                     input$dir
                   },
                   handlerExpr = {
                     home <- normalizePath("~")
                     datapath <<-
                       file.path(home, paste(unlist(dir()$path[-1]), collapse = .Platform$file.sep))
                   })
    }
    
    # Run the application
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 谢谢,成功了!但是你认为可以使用条件格式,让整个字段只出现在选择之后吗?
    • 您不需要条件格式,只需在verbatimTextOutput 函数中设置placeholder = FALSE
    • 我会问一个新问题,但也许你在这里有一个快速的答案。在用户选择文件夹之前,您知道如何在 verbatimTextOutput 中没有占位符,而是在静态中使用 smth 吗?例如,当前目录的路径。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2017-01-28
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多