When does R Studio autoload packages?

I have encountered a problem using the groundhog package, which I think we have tracked down to the behaviour of R Studio in auto-loading packages.

See the entries from 2021-02-19 here for further background info on how this issue presents itself:

When I open R Studio afresh it is set to open my existing project. In Tools > Global Options... I have "Restore .RData into workspace at startup" set to false (unchecked). In Tools > Project Options this is set to "No".

Opening R Studio and immediately entering sessionInfo() at the Console, I get:

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6      digest_0.6.25   crayon_1.4.1    lifecycle_0.2.0 magrittr_2.0.1  evaluate_0.14  
 [7] pillar_1.4.7    rlang_0.4.10    vctrs_0.3.6     ellipsis_0.3.1  rmarkdown_2.3   forcats_0.5.0  
[13] tools_4.0.2     hms_0.5.3       yaml_2.2.1      xfun_0.16       compiler_4.0.2  pkgconfig_2.0.3
[19] htmltools_0.5.0 knitr_1.29      haven_2.3.1     tibble_3.0.6  

I have not consciously done anything to load the set of packages listed at the end of that sessionInfo() report.

I have checked the three locations mentioned in this Stack Overflow answer, and do not see a .Rprofile file in any of them.

If I restart my R session (Ctrl+Shift+F10) and re-run sessionInfo(), the packages are not loaded:

Restarting R session...

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2   

However, when I run a chunk in my Rmd file that ostensibly doesn't have anything to do with most of these packages, it seems to trigger loading of them. This, however, is intermittent. Sometimes the packages are loaded and sometimes they are not.

If the chunk I am running is the first chunk in the Rmd, that seems from my experiments to not trigger the loading of these packages.

If I have a chunk above my intended chunk headed {r setup, include=FALSE} containing only knitr::opts_chunk$set(echo = TRUE), this seems to normally be enough to cause the packages to load.

If, however, I have a chunk above my intended chunk headed just {r} and containing only an empty line, it seems to cause the packages to be loaded about half the time, and not be loaded the other half.

I would like to understand what it is that causes R Studio to automatically load or not load packages. I would ideally like to know what to set to stop it from doing these things, but if there's no setting then understanding what the triggers are so that I can work around them would be good.

For example, I've restarted R and re-run my code probably a couple of dozen times in different variants. I think I see a pattern where the presence of a setup chunk pretty consistently triggers the loading of packages, and where the presence of an untitled empty chunk sometimes but not always triggers it. But the number of repetitions I can manually run in each configuration means that this could be a coincidence.

I also tried creating a MWE, and the presence of at least one subsequent chunk seemed to be relevant for whether the loading was triggered. But again, I have somewhat low confidence that that was a real effect rather than coincidence.

And I seemed to get the packages autoloaded if I ran a chunk (using the play button) or if I selected the code and hit Ctrl+Enter, but not if I copied the code and pasted it into the Console. (EDIT: I'm less certain on this observation now. I have been trying to work around the autoloading by running the relevant code by pasting it into the Console, and I think it does sometimes still trigger some packages to be loaded.)

Any information on what's going on here would be much appreciated. Thanks in advance!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.