Make sure all default packages are loaded before running .Rprofile

自闭症网瘾萝莉.ら 提交于 2021-02-04 17:54:33

问题


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, e.g.:

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?


回答1:


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 e.g. '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.




回答2:


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.



来源:https://stackoverflow.com/questions/13595759/make-sure-all-default-packages-are-loaded-before-running-rprofile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!