How to make `choose.dir` run only the first time that user runs a function in R?

Viewed 52

I use choose.dir() in my simple function below to pop up a windows-type screen for user to choose a working directory for the png files produced in the function.

Question

However, I was wondering how I could have this choose.dir() to pop-up ONLY on the first run of the function? (not each time user wants to run the function)

Right now, the choose.dir() window pops up each time I run the function.

Pch = function(n = 5){

setwd(choose.dir())    ## How to make this run only the first time that user runs the function?


for(i in 1:n) {

 png(paste0("plot_", i, ".png"), width = 1200, height = 1300, res = 200)

  plot( rnorm(1e2) )

  dev.off()

    }
}

## TEST HERE:
Pch()
1 Answers
Related