Open a File Folder in R

code
rtip
shell
Author

Steven P. Sanderson II, MPH

Published

February 28, 2023

Inroduction

When writing a function, it is possible that you may want to ask the user where they want the data stored and if they want to open the file folder after the download has taken place. Well we can do this in R by invoking the shell.exec() command where we use a variable like f_path that is the path to the folder. We are going to go over a super simple example.

Function

Here is the function:

shell.exec(file)

Here are the arguments.

  • file - file, directory or URL to be opened.

Now let’s go over a simple example

Example

Here we go.

# Create a temporary file to store the zip file
f_path <- utils::choose.dir()

# Open file folder?
if (.open_folder){
    shell.exec(f_path)
}

If in our function creation we make a variable .open_folder and set it equal to TRUE then the if statement will execute and shell.exec(f_path) will open the specified path set by utils::choose.dir()

Voila!