rstudioapi::selectDirectory - run app from terminal

I want to launch a shiny application from a terminal. My application has a button that when pressed opens a window to select a folder with the function rstudioapi::selectDirectory()

But when i click on the button i get this error:

$ "Rscript.exe" -e "shiny::runApp('Shiny/app.R')"
Loading required package: shiny
Warning: package 'shiny' was built under R version 3.5.3

Listening on http://127.0.0.1:6283
Warning: Error in : RStudio not running
  76: stop
  75: verifyAvailable
  74: callFun
  73: rstudioapi::selectDirectory
  72: observeEventHandler [D:/Users/folder/Shiny/app.R#12]
   1: shiny::runApp
library(shiny)

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

  actionButton("folder", "Choose folder")

)

server <- function(input, output) {
  observeEvent(input$folder, {
    path.file <- rstudioapi::selectDirectory()
    print(path.file)
  })

}

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

The problem is that rstudioapi::selectDirectory() requires that RStudio is running, and it's not. You'll need to use some other technique instead.

Hello,
ha ok :frowning: . I found this alternative shinyFiles

library(shiny)
library(shinyFiles)

ui <- fluidPage(
  bootstrapPage(
    shinyDirButton(id="folder", label="folder", title="folder", 
                   buttonType = "default", 
                   class = NULL, icon = NULL, style = NULL),
    verbatimTextOutput("directorypath")
  )
)
server <- function(input, output,session) {
  
  volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
  
  
  shinyDirChoose(input, "folder", roots = volumes, 
                 session = session, 
                 restrictions = system.file(package = "base"))
  
  output$directorypath <- renderPrint({
    parseDirPath(volumes, input$folder)
  })
  
}
shinyApp(ui, server)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.