Saturday, 15 September 2012

r - Passing variables to the library function -



r - Passing variables to the library function -

this question has reply here:

load r bundle character string 2 answers package <- c("car","ggplot2","pastecs","psych") (i in package){ if (!(i %in% rownames(installed.packages()))){ install.packages(i) } else{ print(paste(i,"has been installed")) library(i) } }

i wrote loop see whether bundle installed, , if available, library should load it.

however got error: there no bundle called 'i'

why can't pass value in variable i library function ?

here's simpler version of code (incorporating @csgillespie's suggestion):

p <- c("car","ggplot2","pastecs","psych") for(i in seq_along(p)) { if(!require(p[i], character.only=true)) { install.packages(p[i]) library(p[i], character.only=true) } }

note code not work because of non-standard evaluation in library , require. character.only argument resolves (per documentation ? library):

character.only logical indicating whether bundle or help can assumed character strings.

r load

No comments:

Post a Comment