How to avoid re-install package when executing/run the code in R-script

Hi everyone, please help me to avoid re-install package when executing/run the code in R-script,
Day 1: I install ~40 packages to analyse the data (I work with R-project). Then i save the R-script
Day 2: I open the R-project and keep working on it. I use Source() to run all the code in R-script that I worked in day 1. However, when I use Source(), all the packages (~40 packages) re-install again and they send me ~40 messages (pls see the image) :frowning:

Could you please tell me how to avoid it?

Thank you!

If your script re-install package, it could be because you have some install.packages command in your script ? Can you check ?

You did not share your script so it is difficult to help you without the detail.

About the warning / question, it is just because a source version is more recent than the available binary, so it asks you which one you one.

2 Likes

My group (Win-Vector LLC) teaches to not mix installing packages and doing work in scripts. We advise: make one script that only installs packages, and have other scripts only attach the packages with the library() command (and all at the top of the script so you notice any not-installed packages early).

1 Like

Thanks so much for your advise! :grinning:

I did check the script and yes...I had some install.packages command in the script :disappointed_relieved: Now to avoid that problem, I make one script for installs packages, and other scripts only attach the packages with the library()!

Thank you so much!

1 Like

You may also consider using pacman::p_load(). It loads packages and installs them if missing. Check the examples in the vignette.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it: