在RStudio中运行时,.First函数的内容似乎未被处理

在RStudio中运行时,.First函数的内容似乎未被处理,r,rstudio,rprofile,R,Rstudio,Rprofile,在我的.Rprofile中,我定义了以下.First函数,它有两个用途: 显示可用的库路径 如果运行带有明亮主题的RStudio,请更改软件包的设置 .First <- function() { if (interactive()) { cat( "\nProcessed RProfile at:", date(), "\n", "Available libraries", "

在我的
.Rprofile
中,我定义了以下
.First
函数,它有两个用途:

  • 显示可用的库路径
  • 如果运行带有明亮主题的RStudio,请更改软件包的设置

    .First <- function() {
        if (interactive()) {
           cat(
             "\nProcessed RProfile at:",
             date(),
             "\n",
             "Available libraries",
             "\n",
             paste(.libPaths(), collapse = "\n "),
             "\n"
             )
    
        # Check if running bright theme and adjust colour settings
        if (assertive::is_rstudio()) {
            theme_info <- rstudioapi::getThemeInfo()
            if (!theme_info$dark) {
                colorout::setOutputColors(
                    normal = 2,
                    negnum = 3,
                    zero = 3,
                    number = 3,
                    date = 3,
                    string = 6,
                    const = 5,
                    false = 5,
                    true = 2,
                    infinite = 5,
                    index = 2,
                    stderror = 4,
                    warn = c(1, 0, 1),
                    error = c(1, 7),
                    verbose = TRUE,
                    zero.limit = NA
                )
           }
        }
      }
    }
    

    。首先首先,函数的整个主体被包装在
    if(interactive()){}
    中。
    interactive
    函数将返回
    TRUE
    ,因此当您从命令行运行函数时,代码将运行。但是,当启动时自动运行
    .First
    时,
    交互式
    函数返回
    FALSE
    ,并跳过整个函数体

    与Rterm相比,RStudio启动时会话开始交互的时间可能有所不同。您可以将类似这样的代码放在
    的开头来测试这一点。首先

    if(interactive()) {
      cat('Interactive is TRUE\n')
    } else {
      cat('Interactive is FALSE\n')
    }
    

    谢谢你提供答案。我想知道,在终端中运行时,该函数是否应该保持未处理状态?情况似乎并非如此,因为从命令行启动R产生的结果相当于寻源和运行。首先是“手动”。您的编辑回答了我的评论,非常有趣。这就解释了为什么我在《最后一个》中也有类似的逻辑。