1

I have recently started using RStudio (2022.02.0) and specifically R Markdown. However, I can not figure out the following: In which file is the information stored about which packages should be loaded? I imagine there needs to be a file similar to nodes package.json. I could not make out any file in the project folder, hidden or not, that contained the required packages. I never bothered about this before because it never caused a problem. However, I would now like to explicitly specify every package I need in the markdown document via library() and remove all ambiguity about package inclusion (it's for a uni project).

More specifically, I am talking about this r markdown code:

---
title: "RMarkdown test"
output: html_notebook
---

```{r}
plot(cars)
```

This code works and plots the dataframe cars from the datasets package using the function plot from the graphics package. However, the only way I can see which package is loaded is via the "Packages" tab in RStudio. The code itself does not make any of that explicit.

Now my specific questions:

  1. Where is the information about required packages stored?
  2. Can I somehow deactivate all "implicit" usage of packages and make r throw an error if a package is not explicitly loaded within the file itself via library()?

I know that this question might seem trivial to you, but I could just not find an answer on stackoverflow, google, the R docs, or anywhere else.

markuhau
  • 11
  • 4
  • 1
    You’ll want to check out the ‘renv’ package. It comes closest to your requirement for something akin to Node’s `package.json`. Base R has no equivalent (admittedly ‘renv’ doesn’t either, but at least it can create a lockfile, and it can collect dependencies from your project code and automatically install them). – Konrad Rudolph Mar 15 '22 at 12:32
  • 1
    The datasets and graphics packages are both special cases because they're part of the roughly 15 "base" packages that are always loaded and available when opening R. So they're the only ones that are "implicitly" used. See https://stackoverflow.com/questions/9700799/difference-between-r-base-and-r-recommended-packages – qdread Mar 15 '22 at 12:33
  • 2
    Short answers: 1. There's no such thing in *base* R. If you want to provide a detailed "manifest" of the packages you use in a project, see for instance the packages `renv` or `groundhog`. And 2. R works already like that by default. If you want to keep it simple, just do what @Basti suggested in the comment above. – Philopolis Mar 15 '22 at 12:33
  • 1
    Regarding your second question: no, this isn’t possible in base R (well, you can specify `R_DEFAULT_PACKAGES`, but this will break stuff!), but you can achieve this inside modules with the [‘box’ package](https://klmr.me/box/). In fact, ‘box’ aims to provide R with a modern module system, such as you’d be used to it from e.g. Node, because R’s built-in system for loading dependencies isn’t particularly modern. – Konrad Rudolph Mar 15 '22 at 12:34
  • To clarify my previous comment, R *would* throw an error if any package *other than the few base packages* is not explicitly loaded within the file itself with `library()` – qdread Mar 15 '22 at 12:34

0 Answers0