I have the following code:
install.packages("microbenchmark")
install.packages("readxl")
install.packages("data.table")
I would like to create a vector of a variable amount of package names without having to actually write the vector (these package names are or not necessarily installed). Desired output:
vector <- c("microbenchmark","readxl", "data.table")
I thought I would feed the packages to vector
as a string and go from there:
input <- 'install.packages("microbenchmark")
install.packages("readxl")
install.packages("data.table")'
vector <- gsub("install.packages\(", " ", input)
But it does not even create the vector properly. How should I do this?
CodePudding user response:
Maybe something like the following answers the question.
input <- 'install.packages("microbenchmark")
install.packages("readxl")
install.packages("data.table")'
input <- scan(text = input, what = character())
sub('^.*\\"(.*)\\".*$', '\\1', input)
#> [1] "microbenchmark" "readxl" "data.table"
Created on 2022-04-21 by the reprex package (v2.0.1)
CodePudding user response:
scan(text=gsub('install.packages|["()]', '', input), what='')
Read 3 items
[1] "microbenchmark" "readxl" "data.table"