确保在运行 .Rprofile 之前加载了所有默认包 [英] Make sure all default packages are loaded before running .Rprofile

查看:33
本文介绍了确保在运行 .Rprofile 之前加载了所有默认包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 RStudio 项目,我设置了这些项目来自动执行任务,例如对多项选择测试进行评分.完成任务所需的函数只是保存在一个文本文件中,并确保它们可供我或其他用户使用,我一直在使用 .Rprofile 文件,该文件只运行类似 source("MyFunctions.R") 在启动时.如果 .Rprofile 文件在 RStudio 项目的基目录下,它会在打开项目时自动运行

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

但是,我偶尔会遇到一些错误,即在获取脚本之前未加载基本 R 包提供的函数,例如 read.csvinstalled.packages,例如:

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"

有没有办法在运行 source() 之前等待默认包加载,而不是为所有默认包添加显式的 library() 调用加载失败?

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?

推荐答案

这个,来自 ?Startup 似乎很明确:

This, from ?Startup seems pretty definitive:

请注意,当站点和用户配置文件仅来源于'base' 包已加载,因此其他包中的 对象需要被例如引用'utils::dump.frames' 或明确之后加载相关包.

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.

基于该明确建议,使用 library() 加载必要的包看起来是您最好的选择.

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

这篇关于确保在运行 .Rprofile 之前加载了所有默认包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆