Get library and load in 1 step?

trinker

ggplot2orBust
#1
Often I see a package I want and I download it and then imediately load it using the install.packages() and library() functions. Well I'm lazy and want to combine this into one step (and not have to type out install.package). So I attempted to write a function to do that. The function gets the library but then will not load it. Why? I can load it then with library(package name) but I can't make it do both install and load in one step. I included the error code as well.

Please enable my laziness.

Code:
get.lib<-function(package){
    pack1<-substitute(package)
       pack<-as.character(pack1)
install.packages(pack)
library(package)
}
Code:
get.lib(car)
Installing package(s) into ‘C:/Users/Documents/R/win-library/2.14’
(as ‘lib’ is unspecified)
trying URL 'http://lib.stat.cmu.edu/R/CRAN/bin/windows/contrib/2.14/car_2.0-10.zip'
Content type 'application/zip' length 755493 bytes (737 Kb)
opened URL
downloaded 737 Kb

package ‘car’ successfully unpacked and MD5 sums checked

The downloaded packages are in
C:\Users\RiAppData\Local\Temp\Rtmp4Mh3jO\downloaded_packages
Error in library(package) : there is no package called ‘package’
 

Dason

Ambassador to the humans
#2
Code:
library(pack, character.only = TRUE)
remember that with library you don't have to enter a string - it will use a substitute to figure out what the exact argument was (like you did) but since the argument you're providing is "package" - it assumes you want to load a package named package.

or if you want horrible code...

Code:
eval(parse(text = paste("library(", pack,")")))
which I wrote and then told myself I was being an idiot...