在Shiny

时间:2017-12-08 17:21:01

标签: r shiny

我希望我的Shiny应用允许用户指定文件夹的路径(本地)和显示所选路径。以下代码有效,但我无法弄清楚如何隐藏"字符(0)"在verbatimTextOutput中,直到选择文件夹。我尝试了条件面板(请参阅我的代码中的注释),但无法在此处找出要用作条件的内容(因为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

1 个答案:

答案 0 :(得分:2)

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