在闪亮中显示选定的文件夹路径 [英] Display selected folder path in Shiny

查看:9
本文介绍了在闪亮中显示选定的文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的闪亮应用程序允许用户指定文件夹的路径(本地)并显示所选路径。下面的代码可以工作,但是在选择文件夹之前,我不知道如何在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

推荐答案

在服务器函数中,使用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)

这篇关于在闪亮中显示选定的文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆