Dennis Aguilar
Dennis Aguilar

Reputation: 113

How to use strings stored in vectors in another function

Hello i'm trying this for loop in which i enable multiple libraries in r.

lbs<-c("plyr","dplyr","magrittr","readr")

for (i in 1:length(lbs)) {

       library(lb[i])

      } 

but i get this error

 Error in library(lb[i]) : 'package' must be of length 1

My questions covers two dillemas.

  1. How do i use strings stored in vectors to use them in another function?

  2. How do i tell rstudio to enable certain libraries by default every time in open r.

Upvotes: 0

Views: 29

Answers (1)

joran
joran

Reputation: 173577

In short:

  1. The library() function is weird. Try library(lb[i],character.only = TRUE). There is an example illustrating this at the very bottom of ?library, for what it's worth.
  2. Read ?Startup, in particular about using .Rprofile files.

Upvotes: 2

Related Questions