I wrote this simple function to install and import libraries, if they are not installed yet. Here is my code:
check_library <- function(my_lib){
tryCatch(
expr = {
library(my_lib)
},
error = {
install.packages(my_lib)
library(my_lib)
}
)
}
lista_libraries = list('tidyverse', 'rvest', 'bizdays','dplyr','lubridate')
for (k in lista_libraries) {
check_library(k)
}
However, when I run the function (within the above loop) R
prints the following Error message
:
Error in library(my_lib) : there is no package called ‘my_lib’
I mean, my_lib
is just the argument name, why R is trying to evaluate it instead of evaluating the actual value used when the function is called?