简体   繁体   中英

Make sure all default packages are loaded before running .Rprofile

I have a couple of RStudio projects that I've set up to automate tasks like grading multiple choice tests. The functions necessary to do the tasks are just saved in a text file, and to make sure they're available to me or other users I've been using an .Rprofile file that just runs something like source("MyFunctions.R") on startup. If an .Rprofile file is in the base directory of an RStudio project, it's automatically run when opening the project

However, I've run into occasional errors where functions provided by the base R packages aren't loaded before the script is sourced, functions like read.csv or installed.packages , eg:

Error in eval(expr, envir, enclos) : 
  could not find function "installed.packages"

Is there some way to wait for the default packages to load before source() is run, rather than adding explicit library() calls for all the default packages that fail to load?

This, from ?Startup seems pretty definitive:

Note that when the site and user profile files are sourced only the 'base' package is loaded, so objects in other packages need to be referred to by eg 'utils::dump.frames' or after explicitly loading the package concerned .

Based on that explicit recommendation, using library() to load the necessary packages looks like your best bet.

Having been pointed towards the ?Startup documentation by Josh's answer, it looks like adding a .First.sys() call at the top of .Rprofile may be an option, as that seems to be the function that handles loading of all the default packages. It is normally run after .Rprofile , which would explain the errors I was getting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM