Installing additional packages through Rprofile

176 views Asked by At

I want to install additional packages besides the default ones every time I restart my R Session.

I have tried this: options(HTTPUserAgent = sprintf("R/%s R (%s)", getRversion(), paste(getRversion(), R.version$platform, R.version$arch, R.version$os))) install.packages(c( 'shinydashboard', 'shinyWidgets', 'rlist', 'sortable', 'tidyverse', 'XML', 'DescTools', 'plotly', 'leaflet', 'tidyquant', 'umap' ))

And this options(defaultPackages = c(getOption("defaultPackages"), "tidyquant", ...)) neither of which works. If all I want to do is to install the tidyquant package upon the R Session restart, how do I get it to work inside the Rprofile file?

1

There are 1 answers

2
jared_mamrot On

I would definitely take @dirk-eddelbuettel's advice on this (XY problem), but it is possible if you add this to your .Rprofile:

.First <- function(){
 utils::install.packages("tidyquant", repos="https://cran.csiro.au/")
 library(tidyquant)
}

(This command is run before any packages are loaded so you need to specify the install.packages function comes from utils).

Not sure if this applies to windows; my system info:

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8

attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods  
[7] base     

other attached packages:
[1] tidyquant_1.0.4            quantmod_0.4.20           
[3] TTR_0.24.3                 PerformanceAnalytics_2.0.4
[5] xts_0.12.1                 zoo_1.8-10                
[7] lubridate_1.8.0           

loaded via a namespace (and not attached):
 [1] magrittr_2.0.3   tidyselect_1.1.2 munsell_0.5.0   
 [4] colorspace_2.0-3 lattice_0.20-45  R6_2.5.1        
 [7] rlang_1.0.2      quadprog_1.5-8   fansi_1.0.3     
[10] dplyr_1.0.9      httr_1.4.3       tcltk_4.1.3     
[13] tools_4.1.3      grid_4.1.3       gtable_0.3.0    
[16] utf8_1.2.2       DBI_1.1.2        cli_3.3.0       
[19] ellipsis_0.3.2   assertthat_0.2.1 tibble_3.1.7    
[22] lifecycle_1.0.1  crayon_1.5.1     purrr_0.3.4     
[25] ggplot2_3.3.6    vctrs_0.4.1      curl_4.3.2      
[28] Quandl_2.11.0    glue_1.6.2       compiler_4.1.3  
[31] pillar_1.7.0     generics_0.1.2   scales_1.2.0    
[34] jsonlite_1.8.0   pkgconfig_2.0.3 
> 

N.B. Choose your CRAN mirror accordingly (unless you also happen to be in Australia): https://cran.r-project.org/mirrors.html