In this page:
This article outlines details about installing R packages in IDAS. We also recommend using tools like conda and Python virtual environments in IDAS to manage your projects.
Installing R Packages in IDAS - The Basics
To install R packages in IDAS, use the install.packages()
function. For example, you can run the following code in the Console tab in RStudio, in an R script file, or in a Jupyter Notebook:
install.packages(c("PACKAGE_NAME_1", "PACKAGE_NAME_2", ...), repos="http://cran.r-project.org")
For more information about the install.packages()
function, type the following in the Console tab in RStudio:
?install.packages
That will display the documentation in the Help tab in RStudio:
R Library Path in IDAS
Getting (Viewing) the Library Paths in IDAS
In IDAS, R looks for packages in 2 locations: a) the user library and b) the system library.
You can view the paths to these libraries by typing the following code in R:
.libPaths()
The screenshot below shows the output of the .libPaths()
function. The output shows 2 paths:
"/home/grudderham/R/x86_64-pc-linux-gnu-library/4.1"
is the user library."/opt/R/lib/R/library"
is the system library.
More About the User Library
The path to the R user library in IDAS is:
/home/HAWKID/R/x86_64-pc-linux-gnu-library
Any IDAS user can install R packages in their personal user library. The screenshot below shows the location of the user library, as seen in the Files tab in RStudio. For each version of R, a folder is created to store the packages for that version. For example, the folder 3.6 contains the packages that have been installed for R version 3.6.1:
Setting (Changing) the User Library Path
The .libPaths()
function can also be used to set the library path where R looks for packages. You may need to do this if you are switching to a newer R version in IDAS for the first time.
For example, suppose you have been using R 4.1.1 in IDAS and you want to start using R 4.2.2 in IDAS for the first time. The steps below need to be done only once.
1. After you log in to IDAS and select an IDAS instance with R 4.2.2, use .libPaths() to check the library paths:
2. The output above shows that R is still looking for packages in the 4.1 folder. We can also check the "~/R/x86_64-pc-linux-gnu-library" folder to see if a 4.2 folder has been created automatically:
3. If a 4.2 folder has not been created automatically as in the above screenshot, we can create it by typing the following command in the Console tab in RStudio:
dir.create("~/R/x86_64-pc-linux-gnu-library/4.2")
4. After running the command, a 4.2 folder will be created:
5. Next, use .libPaths() to change the user library path to the new folder. Run the following in RStudio:
.libPaths("~/R/x86_64-pc-linux-gnu-library/4.2")
6. Finally, use .libPaths() again to double check the library paths. In the screenshot below, the user library path has been changed to the 4.2 folder. The system library path has not been changed:
7. You can now install packages in R version 4.2.2 as usual.
For example, install the "XML" package:
> install.packages(“XML”)
If you want to be sure, you can also specify exactly where you want to install packages by using the “lib” argument:
> install.packages("XML", lib = "~/R/x86_64-pc-linux-gnu-library/4.2")
Then load the package as usual:
> library(XML)
More About the System Library
The path to the R system library in IDAS is:
/opt/R/lib/R/library
The system library contains packages that are installed by IDAS systems administrators. These packages are available to all IDAS users. In most cases, IDAS users do not have permissions to install R packages to the system library.
For Class Instructors - Installing R Packages for All Students in the Class
The instructor of a class may request to have permissions to install R packages to the global library for their class. Packages installed in the global library for that class will be available to all students enrolled in the class.
Instructors can install packages to the global library for their class using the following R code:
install.packages(c("package1", "package2", "package3"), lib = "/opt/R/lib/R/library")
The instructor also has their own user library. Please see the sections on R Library Paths in IDAS above.
Important: Sometimes a package that is installed in the global library might use dependencies from the instructor's personal library. In that case, the students might have trouble loading the package even though it looks like the package is already installed in the global library. Therefore, instructors please make sure that you install all packages in the global library so all dependencies are available there for students.
You can check which packages are installed in the global library by typing the following in Terminal (next to the Console tab in RStudio):
ls -a /opt/R/lib/R/library
You will see that some packages are already installed in the course instance as part of the standard R install. You can use the same command to check which packages are in your personal library. Please substitute the R version at the end of the path, such as 3.6, 4.0, 4.1, or 4.2.
ls -a ~/R/x86_64-pc-linux-gnu-library/4.2
Contact
If you have any questions or comments, please contact ITS - Research Services at research-computing@uiowa.edu.