gpt4 book ai didi

r - 在 Shiny 中显示选定的文件夹路径

转载 作者:行者123 更新时间:2023-12-04 11:56:54 25 4
gpt4 key购买 nike

我希望我的 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

最佳答案

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

关于r - 在 Shiny 中显示选定的文件夹路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47719285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com