gpt4 book ai didi

r - 在启动时检测 R session 是否在 RStudio 中运行

转载 作者:行者123 更新时间:2023-12-01 03:40:58 25 4
gpt4 key购买 nike

<分区>

我在终端和 RStudio(在 mac 和 linux 上)中都使用 R,想知道是否可以对两者使用不同的 .Rprofile,或者最好使用相同的基础 .Rprofile 但来源不同的环境特定调整脚本。

我认为将以下代码放入我的 .Rprofile 会起作用,但不幸的是 session_info 当时没有设置 .First 运行。 Sys.getenv 也不是。

.First <- function(){
# [STUFF I ALWAYS WANT TO DO]
# Load my favourite packages
# Set CRAN mirror
# etc. etc.

# [ENVIRONMENT SPECIFIC TWEAKS]
if(grepl("RStudio", session_info()$platform$ui)){
tryCatch(source("~/.R_RStudio"), error=print)
} else {
tryCatch(source("~/.R_terminal"), error=print)
}
}

我还尝试在 .bash_profile 中设置 alias R='R --args terminal' 这确实允许我检测 session 是否从 bash 启动,但它搞砸 R CMD ... 和任何使用其他命令行参数的脚本。

我意识到可能无法从 R session 中检测到它是从哪里开始的,但也许 RStudio 中有一些我不知道的聪明选项。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com