Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205491

Why R isn't replacing the function argument by the given value?

$
0
0

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?


Viewing all articles
Browse latest Browse all 205491

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>