简体   繁体   English

在软件包安装过程中向.First()添加行

[英]Adding line to .First() during package installation

I'm developing R packages for my company of not so tech savy employees. 我正在为我的公司设计R包,但他们不是那么精通技术的员工。

To facilitate the life of the users I'm thinking of having a set of scripts at a global location that can get sourced at startup in order to update and load relevant packages. 为了简化用户的生活,我考虑在全球范围内拥有一组脚本,这些脚本可以在启动时获取,以更新和加载相关软件包。

My idea is to have a script for each custom package that only gets sourced if that particalar package is installed. 我的想法是为每个自定义程序包创建一个脚本,该脚本仅在安装了特殊程序包的情况下才会获得源代码。 For this to work I need to modify the .First() function when the user installs the different packages, such that after packages A is installed: 为此,我需要在用户安装其他软件包时(例如在安装软件包A之后)修改.First()函数:

.First() <- function(){
    source('script_to_package_A')
}

and if package B is then installed: 并且然后安装了软件包B:

.First() <- function(){
    source('script_to_package_A')
    source('script_to_package_B')
}

Thus i am interested to add a line inside the .Rprofile file if .First() is already defined and the line is not in there already or, if no .Rprofile exists, create it. 因此,如果已经定义了.First()且该行尚不在其中,或者如果不存在.Rprofile,则可以在.Rprofile文件中添加一行。

What would be the best way to achieve this? 实现这一目标的最佳方法是什么?

best wishes Thomas 最好的祝福托马斯

EDIT: Just to clarify - what I am looking for is a safe way to modify the .First() function that gets called during startup. 编辑:只是为了澄清-我正在寻找的是一种安全的方法来修改在启动过程中被调用的.First()函数。 That means that if a user already has defined a .First() function the additions will just be appended instead of replacing the old .First(). 这意味着,如果用户已经定义了.First()函数,则只会添加附加内容,而不是替换旧的.First()。 Preferably this addition to first will happen when a specific package is installed but this is not necessary. 最好是在安装特定的软件包时首先进行添加,但这不是必需的。 I'm fairly confident in what to add to the .First() function so this is not in question. 我对添加到.First()函数中的内容非常有信心,因此这不是问题。

I suspect that the easiest way to get what you want is to have a single package installed on all machines. 我怀疑获得所需内容的最简单方法是在所有计算机上安装一个软件包。 If you can alter the .First function on everyones machine, then you can install a package. 如果可以在每个人的计算机上更改.First函数,则可以安装软件包。

Your .First function would be: 您的.First函数将是:

.First() <- function(){
    library(mypkg)
    run_mypkg_fun()
    ##Could also check for package updates
    ##update.packages("mypkg"...)
}

The function run_mypkg_fun can do all the checks you want, including: 函数run_mypkg_fun可以执行所有所需的检查,包括:

  • installing packages 安装软件包
  • running different functions conditional on what's installed. 根据安装的内容运行不同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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