代码之家  ›  专栏  ›  技术社区  ›  Konrad

在rstudio中运行时,似乎未处理.first函数的内容

  •  1
  • Konrad  · 技术社区  · 6 年前

    在我的 .Rprofile 我定义如下 .First 有两个目的的功能:

    • 显示可用库路径
    • 更改的设置 colorout 如果运行主题鲜明的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
                  )
             }
          }
        }
      }
      

    问题

    在终端中运行时,函数返回所需的结果: enter image description here

    但是,将由 .第一 函数不显示:

    enter image description here

    颜色设置似乎也不适用。 当我作为 .First() 那就没问题了。

    我想了解rstudio启动的不同之处在于,它不会产生与在终端中运行并访问r会话相同的结果 .r轮廓 是吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Greg Snow    6 年前

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

    与rterm相比,rstudio启动中会话开始交互的时间可能有所不同。您可以将这样的代码放在 .第一 以下内容:

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