How to open files, folders, websites in R

[This article was first published on Maëlle's R blog on Maëlle Salmon's personal website, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Coming to you from France, a post about Mise en place for R projects. In a less francophone phrasing: to get to work on something you have to open that thing, be it a script or a project or a website. The easier that is, the faster you get to work. In this post I’ll show a roundup of R functions and related tools for opening scripts, projects and websites for yourself or on behalf of the users of your code.

Many thanks to Hannah Frick for providing inspiration for some items of this post, and for reviewing it!

Open any file in the editor: utils::file.edit() or styling with {cli}

If you write code that creates a file at path and then is supposed to open it for the user, there is no need for you to use, say, rstudioapi::documentOpen(path) that only works in RStudio IDE. You can use a base R function, utils::file.edit()! It will open the path in the default editor. Without me setting up anything, that is the RStudio IDE when I’m already in RStudio and Positron when I’m already in Positron. From an R session in the terminal1, that is the default editor of my system2.

I was surprised to see that usethis::edit_file() uses an RStudio specific function when available:

# https://github.com/r-lib/usethis/blob/4aa55e72ccca131df2d98fcd84fff66724d6250a/R/edit.R#L32C1-L36C4
if (rstudio_available() && rstudioapi::hasFun("navigateToFile")) {
  rstudioapi::navigateToFile(path)
} else {
  utils::file.edit(path)
}

According to the commit that added this logic more than 7 years ago, “utils::file.edit opens in dialog” which I do not understand. Maybe it’s different depending on the OS? Maybe RStudio changed in the meantime? Please tell me if you have any more information on this. 🙏

In any case, I really enjoy file.edit() in code bases. Interactively, I do not use is as often as, say, Positron’s shortcut for navigating to files (Ctrl+P on my machine).

Last but not least, if you want to make it easy for the user to open a file, without opening it on their behalf, in messages emitted through the cli package you can use the file class:

cli::cli_alert_success("Hey go edit {.file config.toml} please!")

“If the terminal supports ANSI hyperlinks (e.g. RStudio, iTerm2, etc.), then cli creates a clickable link that opens the file in RStudio or with the default app for the file type.”

Open the script or test file of a script-test pair: usethis::use_r() and usethis::use_test()

Coming back to the wonderful usethis package, when you name your test files after your scripts as you should, running usethis::use_test() when focussed on an R script will open its test file, and conversely usethis::use_r() when focussed on a test file. This is still how I switch between the two even if inside Positron.

Open a project: positron, project launcher, {usethis}

This is all very good when within a project, but how do you enter your IDE in the first place? How sad would it be to lose momentum before even launching a project?

When I used the RStudio IDE, I would navigate to the folder of interest, then double-click on the .Rproj file. Hannah Frick inspired me, when demoing Positron to me, to think a bit harder about how much I click. 😅 I have adopted a more keyboard-based workflow since I switched to Positron, which I could have done with RStudio IDE.

One thing in particular that Hannah showed me that looks cool is the use of a project launcher.

Now, I myself still use the terminal. For instance if I clone a repository3, I’ll then open it with the positron command. If that command does not work for you yet on macOS or Windows, you might need to add Positron to your PATH.

Another way to open projects that I use a lot, in Positron and RStudio, is to click on the recent projects when the IDE is already up and running. Unfortunately, I created and currently often use the saperlipopette package that creates throwaway folders for practicing Git. This makes the list of recent projects utter rubbish. 🙃

Within saperlipopette itself, to create and open the exercise folder on behalf of the user, I use usethis::create_project(), which handily opens it in Positron when run in Positron and in RStudio when run in RStudio.

Open an URL in the browser: browseURL() or styling with {cli}

Sometimes you want the user of some code of yours to go admire or read a web page.

To open an URL in the default browser, you can use utils::browseURL(). In all of the usethis::browse_ functions, utils::browseURL() is what’s used under the hood. For instance:

browseURL("https://masalmon.eu/post")

To make it easy to open an URL from a message, you can use the URL class of the cli package. For instance:

cli::cli_alert_info("Go read that blog! {.url https://masalmon.eu/post}!")

When getting that message, the user will simply have to click on the link to follow it (after trusting the domain in Positron).

Open only in interactive sessions: interactive(), rlang::is_interactive()

If you use utils::file.edit() or utils::browseURL() in your code, you need to ensure the session is interactive.

Either

if (interactive()) {
  file.edit("config.toml")
}

Or

if (rlang::is_interactive()) {
  file.edit("config.toml")
}

Compared to interactive(), rlang::is_interactive() also checks whether knitr or testthat are in progress, and provides an escape hatch through the rlang_interactive option.

All of the usethis functions that try to open or browse something on behalf of the user behave differently based on rlang::is_interactive().

Conclusion

In this post I summarized some tools for opening scripts, projects, and URLs – for yourself or on behalf of your user. I’d like to add some words of conclusion beyond the summary table below, but after so much talk of opening, I have trouble closing this post. 😁

Target to open Tool Audience
File utils::file.edit() Users of your code
IDE’s shortcut You – get to know your IDE
.file class of cli message Users of your code
R script from test file usethis::use_r() You
IDE’s shortcut You – get to know your IDE
Test file from R script usethis::use_test() You
IDE’s shortcut You – get to know your IDE
Project (folder) .Rproj file RStudio IDE users who like clicking
List of recent projects within the IDE You when the IDE is already launched
rstudio, positron Terminal dwellers
Project launcher Positron users on macOS
usethis::create_project() Users of your code that created a project
URL utils::browseURL() Users of your code
.url class of cli message Users of your code

  1. Look how adventurous I was when preparing this post! Running R from a terminal! ↩︎

  2. Which is… Atom, meaning I live in the past since its end-of-life occurred a few years ago. 👻 I actually rarely use it. ↩︎

  3. I know I could use usethis::create_from_github() and sometimes do but not always… Maybe the message of this post is that my workflows are a big mess. ↩︎

To leave a comment for the author, please follow the link and comment on their blog: Maëlle's R blog on Maëlle Salmon's personal website.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)