2017-12-08 11 views
1

Shiny 앱에서 사용자가 폴더 경로를 로컬로 지정할 수있게하고 표시으로 선택 경로를 표시합니다. 다음 코드는 작동하지만 폴더가 선택 될 때까지 verbatimTextOutput에서 "character (0)"를 숨기는 방법을 알아낼 수 없습니다. 조건부 패널 (내 코드에서 주석 참조)을 시도했지만 여기에서 조건으로 사용할 항목을 파악할 수 없습니다 (shinyDirButton은 표준 작업 버튼이 아니기 때문에 ...). 고맙습니다!Shiny에서 선택한 폴더 경로 표시

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

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) 
+0

감사합니다 , 그것은 일했다! 그러나 조건부 서식을 사용하고 전체 필드를 선택 후에 만 ​​표시 할 수 있다고 생각합니까? – kintany

+0

당신은 그것을위한 조건부 포맷을 필요로하지 않고 단지'placeholder = FALSE'를'verbatimTextOutput' 함수에 설정하십시오. – jsb

+0

새로운 질문을하지만 어쩌면 빠른 대답을 할 수 있습니다. verbatimTextOutput에 자리 표시자가 없지만 사용자가 폴더를 선택하기 전에 정적 인 방법을 알고 있습니까? 예를 들어, 현재 디렉토리의 경로. 정말 고마워. – kintany