0

my testing .Rprofile:

message("~/.Rprofile starting")

options(repos="https://cran.microsoft.com/")
if (!require("data.table")) install.packages("data.table")

message("~/.Rprofile ending")

works if I start with R --vanilla and then source("~/.Rprofile"), but not with a standard startup R, in which case the automatic sourcing of ~/.Rprofile gives me

~/.Rprofile starting
Loading required package: data.table
Error: could not find function "install.packages"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘data.table’
>

is there something (a library) that needs to be loaded first, that is usually loaded only after .Rprofile is executed (otherwise)?

I tried to debug this through .libPaths(), but lost it after I could not figure out where .lib.loc comes from in the function. there must be some specific startup functioning related to the embedding of the .Rprofile somewhere, which defines .lib.loc and does some more stuff.

ivo Welch
  • 2,427
  • 2
  • 23
  • 31
  • When interactive, R normally loads `utils`, but not when non-interactive as with Rscript or similar. Either use `library(utils)` or prepend with `utils::`. – r2evans Nov 25 '22 at 22:16
  • 2
    I think the detailed answer to what you are trying to ham-first here is that _within the startup sequence_ when `~/.Rprofiel` is loaded tbe packages such as `utils` are not yet loaded so you probably need an explicit `utils::install.packages()`. – Dirk Eddelbuettel Nov 25 '22 at 22:17
  • 1
    @r2evans: Try `Rscript -e 'search()'`. The `utils` package gets loaded but I think later than Ivo needs it here. – Dirk Eddelbuettel Nov 25 '22 at 22:18

0 Answers0