在R启动时运行R脚本

编程入门 行业动态 更新时间:2024-10-27 02:22:47
本文介绍了在R启动时运行R脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只想在启动R 时自动运行R脚本 example.r 我正在使用 R版本3.2.3

I just want to run an R script example.r automatically when I start R I am using R version 3.2.3 on centOS

我搜索了这个,但我不知道。

I searched for this but I couldn't figure that out.

推荐答案

R的启动行为可以通过编辑 Rprofile.site ,这可能需要创建,因为默认的R安装不会自动为您执行此操作。在CentOS 7上,此文件应位于目录 / usr / lib64 / R / etc / 中;或更普遍的是 $ R_HOME / etc / ,其中 R_HOME 可以通过运行 Sys来确定。 R会话中的getenv( R_HOME)。

The startup behavior of R can be modified by editing Rprofile.site, which may need to be created, as the default R installation does not do this for you automatically. On CentOS 7, this file should be located in the directory /usr/lib64/R/etc/; or more generally $R_HOME/etc/, where R_HOME can be determined by running Sys.getenv("R_HOME") from an R session.

例如,如果我按如下方式修改 Rprofile.site

For example, if I modify my Rprofile.site as follows,

[nathan@xxx] cat /tmp/example.r x <- 1.5 y <- 2.5 z <- 3.5 t <- Sys.time() [nathan@xxx] cat /usr/lib64/R/etc/Rprofile.site options(prompt = "R> ") options(continue = " ") options(stringsAsFactors = FALSE) options(scipen = 4) source("/tmp/example.r")

更改将反映在新的R会话中:

the changes will be reflected in a new R session:

尽管在在此示例中,习惯上将此类代码包装在中。首先<-function(){...} 以确保其在会话启动后立即运行:

Although apparently not necessary in this example, it is customary to wrap such code in .First <- function() { ... } to ensure that it is run immediately upon session startup:

[nathan@xxx] cat /usr/lib64/R/etc/Rprofile.site options(prompt = "R> ") options(continue = " ") options(stringsAsFactors = FALSE) options(scipen = 4) .First <- function() { source("/tmp/example.r") }

更多推荐

在R启动时运行R脚本

本文发布于:2023-11-05 06:22:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   脚本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!