[R] getting to a point where packages are installed and "ready to use", without unnecessarily reinstalling packages

Thierry Onkelinx th|erry@onke||nx @end|ng |rom |nbo@be
Tue Oct 25 15:59:14 CEST 2022


Dear Kelly,

Have a look at the renv package (https://CRAN.R-project.org/package=renv).
Once setup, your code reduces to renv::restore()

Best regards,

ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry.onkelinx using inbo.be
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////

<https://www.inbo.be>


Op di 25 okt. 2022 om 15:54 schreef Kelly Thompson <kt1572757 using gmail.com>:

> I have R packages I want to use.
>
> Q. What is the "best" way to get to a point where all of the packages
> are installed and "ready to use", AND where I only install or
> re-install a package if doing so is needed?
>
> #I searched the web for insights and found these:
> https://hohenfeld.is/posts/check-if-a-package-is-installed-in-r/
>
> https://stackoverflow.com/questions/9341635/check-for-installed-packages-before-running-install-packages
>
> Based on what I read there, I "think" I should use the require function.
>
> Here is what I came up with.
>
> Is there anything "wrong" with this code, and are there any ways I can
> improve the code?
>
> ### START OF REPRODUCIBLE CODE
>
> #install and load packages (list the packages I want in a vector,
> check if they are available to use, install if needed, load and
> attach, review)
>
> #create a vector with the character vector of the name(s) of
> package(s) I want to use
> packages_i_want_to_use <- c('RODBC', 'data.table', 'matrixStats',
> 'plyr', 'MASS', 'dplyr', 'lubridate')
> #packages_i_want_to_use <- c("this_pac_does_not_exist", "abcz",
> "lubridate")
>
> #use the require function to check if the package(s) is (are) available
> packages_exist_true_false <- sapply(X = packages_i_want_to_use, FUN =
> require, character.only = TRUE, quietly = TRUE)
>
> # create a vector with the names of the packages that need to be installed
> packages_to_install <-
> packages_i_want_to_use[packages_exist_true_false == FALSE]
>
> #specify the repo(s) AKA CRAN mirror I want to use
> myrepo <- 'https://ftp.osuosl.org/pub/cran/'
>
> #install the package(s)
> install.packages(pkgs = packages_to_install, repos = myrepo)
>
> #load and attach the packages_i_want_to_use using the library function
> sapply(X = packages_i_want_to_use, FUN = library, character.only = TRUE)
>
> #review
> #review to determine if the packages are available, using require()
> packages_exist_true_false_review <- sapply(X = packages_i_want_to_use,
> FUN = require, character.only = TRUE, quietly = TRUE)
> packages_exist_true_false_review
>
> ### END OF REPRODUCIBLE CODE
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list