Calling getwd() from within a shiny app

I am creating a Shiny app that needs to access a user's working directory. I assumed getwd() would work from within the Shiny app, however this always points to the directory of the Shiny app itself.

Example:

From the R console:

setwd("~/Documents")
getwd()
[1] "/Users/chris.harrison/Documents"

From the following Shiny app saved on the desktop:

library(shiny)

ui <- fluidPage(
  verbatimTextOutput("wd")
)

server <- function(input, output){
 output$wd <- renderText(getwd())
}

shinyApp(ui = ui, server = server)

And then calling:

setwd("~/Documents")
shiny::runApp("~/Desktop/app.R")

the output is:

/Users/chris.harrison/Desktop

Is there any way to call the user's working directory from within the app?

Many thanks,

Chris

It's not obvious to me why you would want to do this or why your users would want to allow this. Nonetheless, the first thing that comes to mind is that you might be interested in R's file manipulation capabilities either through base R or a package like fs.

I really don't think there is. Shiny will always set the working directory as it runs.

Can you say why you want to and there might be something else you could do instead?

2 Likes