Search code examples
rbioconductor

How to load and use data file with R whose name is in a variable?


I would like to load a data file in R using data(), with the data set's name stored in a variable. Doing this without the data set name stored in a variable is trivial:

> library(ChIPpeakAnno)
> data(TSS.human.NCBI36)
> # Use data:
> TSS.human.NCBI36 # Prints out contents of data set

When the data set name is stored in a variable, however, I'm not sure how to accomplish the same task.

> library(ChIPpeakAnno)
> assembly <- 'TSS.human.NCBI36'
> data(list=c(assembly)) # Hackish way of loading the data from a variable
> # Now I wish to access the data, but I don't know how.

data()'s return value is simply the name of the data set loaded. The data file I'm trying to load is located at ~/R/2.15/library/ChIPpeakAnno/data/TSS.human.NCBI36.rda -- I do not believe there is anything Bioconductor-specific to it.

Thanks!


Solution

  • If you're trying to figure out how to access data programmatically when you just have the objects name in a character vector you can use get.

    library(ChIPpeakAnno)
    assembly <- 'TSS.human.NCBI36'
    data(list=c(assembly)) 
    
    # Now store the data into 'dat'
    dat <- get(assembly)
    # Now you can use 'dat' anywhere you would normally use TSS.human.NCBI36
    head(start(dat))
    #[1]  1873  4274 20229 24417 24417 42912
    head(start(TSS.human.NCBI36))
    #[1]  1873  4274 20229 24417 24417 42912