0

I've created a variable and want to use that variable to select my desired function in a package (e.g. package::function), however, the variable name is interpreted literally instead of being evaluated.

Here's the approach:

library(GSEABase)
library(tidyverse)


### SET ONTOLOGY GROUP (e.g. Biological Process = BP, Molecular Function = MF, Cellular Component = CC)
ontology <- "BP"

### Set GOOFFSPRING database, based on ontology group set above
go_offspring <- paste("GO", ontology, "OFFSPRING", sep = "")

## Need to know the 'offspring' of each term in the ontology, and this is given by the data in:
GO.db::go_offspring

## Create function to parse out GO terms assigned to each GOslim
## Courtesy Bioconductor Support: https://support.bioconductor.org/p/128407/
mappedIds <-
  function(df, collection, OFFSPRING)
  {
    map <- as.list(OFFSPRING[rownames(df)])
    mapped <- lapply(map, intersect, ids(collection))
    df[["go_terms"]] <- vapply(unname(mapped), paste, collapse = ";", character(1L))
    df
  }

## Run the function
slimsdf <- mappedIds(slimsdf, myCollection, go_offspring)

This spits out the error:

Error: 'go_offspring' is not an exported object from 'namespace:GO.db'

When playing around in the R Studio console, I also notice that when I type

GO.db::

the autocomplete feature does not list my go_offspring variable as an option; it only lists the available functions within the GO.db package.

Seeing this behavior suggests there's a scope issue, in that the package cannot see variable definitions defined outside of the package.

Is there any way around this?

I've looked at this http://adv-r.had.co.nz/Environments.html, but I'm not entirely sure I follow all of it, nor do I see how to manipulate my environments to allow passing go_offspring to GO.db::.

kubu4
  • 157
  • 13

1 Answers1

1

You can use getFromNamespace to get the function via its character name from the namespace.

slimsdf <- mappedIds(slimsdf, myCollection, getFromNamespace(go_offspring, "GO.db"))
MrFlick
  • 195,160
  • 17
  • 277
  • 295