Thanks Duncan I expected some silly mistake on my side.
Just for completeness, I finally tested .Rprofile file with the code and it worked. So now the question is whether to use your suggestion or .Rprofile file way. I tend to use Rprofile.site way as it enables experienced users to modify their .Rprofile to get customised way how to start R. Best regards Petr > -----Original Message----- > From: Duncan Murdoch <murdoch.dun...@gmail.com> > Sent: Tuesday, March 21, 2023 1:55 PM > To: PIKAL Petr <petr.pi...@precheza.cz>; r-help <r-help@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@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.
______________________________________________ R-help@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.