[R] Rprofile.site and automatic installation of missing packages

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Tue Mar 21 17:42:44 CET 2023


On 21/03/2023 9:58 a.m., PIKAL Petr wrote:
> Hallo Duncan
> 
> Tested but does not work so something other must be wrong.
> 
> R version 4.2.2.
>> installed.packages()[,"Package"]
>            base           boot          class        cluster      codetools       compiler       datasets        foreign       graphics      grDevices           grid     KernSmooth
>          "base"         "boot"        "class"      "cluster"    "codetools"     "compiler"     "datasets"      "foreign"     "graphics"    "grDevices"         "grid"   "KernSmooth"
>         lattice           MASS         Matrix        methods           mgcv           nlme           nnet       parallel          rpart        spatial        splines          stats
>       "lattice"         "MASS"       "Matrix"      "methods"         "mgcv"         "nlme"         "nnet"     "parallel"        "rpart"      "spatial"      "splines"        "stats"
>          stats4       survival          tcltk          tools   translations          utils
>        "stats4"     "survival"        "tcltk"        "tools" "translations"        "utils"
> 
> My Rprofile.site
> # Things you might want to change
> options(papersize="a4")
> options(help_type="html")
> 
> library(utils)
> library(MASS)
> 
> #**********
> test <-(scan("pack.txt", character(), quote = ""))
> x<- utils::installed.packages()
> utils::install.packages(test[!test %in% x], repos="https://cloud.r-project.org")
> 
> ##**********
> 
> Options are set and working.
> MASS should be loaded but is not
> 
>> search()
> [1] ".GlobalEnv"        "package:stats"     "package:graphics"  "package:grDevices" "package:utils"     "package:datasets"  "package:methods"   "Autoloads"         "package:base"
>>
> 
> Any suggestion where to look?

I'd add code to print the values of x and test to confirm that things 
are proceeding as you expect.  I don't know if print() or cat() will 
work there; you might need to use message().

For attaching packages, you should see ?Startup again:  this is done via 
options() or an environment variable, not library() calls in the profile 
file.

Duncan Murdoch

> 
> Best regards
> Petr
> 
> -----Original Message-----
> From: Duncan Murdoch <murdoch.duncan using gmail.com>
> Sent: Tuesday, March 21, 2023 1:55 PM
> To: PIKAL Petr <petr.pikal using precheza.cz>; r-help <r-help using r-project.org>
> Subject: Re: [R] Rprofile.site and automatic installation of missing packages
> 
> ?Startup says:  "Note that when the site and user profile files are sourced only the base package is loaded, so objects in other packages need to be referred to by e.g. utils::dump.frames or after explicitly loading the package concerned."
> 
> So you need utils::installed.packages and utils::install.packages .
> 
> Duncan Murdoch
> 
> On 21/03/2023 8:04 a.m., PIKAL Petr wrote:
>> Dear all.
>>
>>
>>
>> I am trying to install missing (not installed) packages during startup
>> of R through code in Rprofile.site but I miserably failed and I am not
>> sure what I am doing wrong.
>>
>>
>>
>> R is installed to C:Program files but it is not writable for the
>> users, therefore I cannot change Rprofile.site located in root etc
>> directory. I however can put Rprofile.site in users home directory
>> (Documents) and use it for R startup setting (partly).
>>
>> However I want for less experienced users to put a code here to check
>> installed packages, check if some specified set of packages is
>> installed and install them, but it is not working.
>>
>>
>>
>> The code in Rprofile.site is:
>>
>>
>>
>> #**********
>>
>> test <- scan("pack.txt", character(), quote = "")
>>
>> inst <- installed.packages()
>>
>> install.packages(test[!test %in% inst],
>> repos="https://cloud.r-project.org")
>>
>> #**********
>>
>>
>>
>> An example of pack.txt is e.g.
>>
>> ggplot2
>>
>> zoo
>>
>>
>>
>> but the code is not executed and packages are not installed. If I use
>> this code after R starts, everything is OK and packages are installed
>> to
>>
>>
>>
>>> Sys.getenv("R_LIBS_USER")
>>
>> [1] "C:\\Users\\PikalP\\AppData\\Local/R/win-library/4.2"
>>
>>>
>>
>> The same applies if I put e.g. library(MASS) in the Rprofile.site, the
>> package is not loaded but after R is live, library(MASS) loads a package.
>>
>>
>>
>> So my question is What is the best way to check after fresh R
>> installation if some predefined set of packages is installed and if
>> not, perform an installation without user intervention in Windows environment?
>>
>>
>>
>> S pozdravem | Best Regards
>>
>> Petr
>>
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>



More information about the R-help mailing list