How can I pass additional arguments to a custom Gviz importFunction in DataTrack?
1
0
Entering edit mode
@sidders-benjamin-5968
Last seen 8.6 years ago
Cambridge, UK

Hello,

I wrote a custom import function for the DataTrack constructor in Gviz (detailed here) to allow visualisation of stranded RNAseq data. In that example I hard coded the library orientation but would now like to provide an additional argument to the custom import function that specifies the library orientation.

However, I can't seem to pass additional arguments to the custom function e.g. these both fail (where strandedBamImport is the custom function and libType is the additional argument):

dataTrack = DataTrack(bamFile, genome="hg19", chromosome=myChr, importFunction=strandedBamImport, libType="fr-firststrand", stream=TRUE)

and

dataTrack = DataTrack(bamFile, genome="hg19", chromosome=myChr, importFunction=strandedBamImport(libType="fr-firststrand"), stream=TRUE)

Other than setting a global variable, is there a way around this that I am missing?

 

gviz • 1.3k views
ADD COMMENT
1
Entering edit mode
@james-w-macdonald-5106
Last seen 1 hour ago
United States

You cannot pass arguments to a function that are not already specified in the API. In other words, if you have a function

fun <- function(arg1, arg2)  cat(paste("I don't do anything, haha!\nBut here are your stupid args:\n", arg1, "\n", arg2, "\n"))

You cannot pass in arguments that don't exist:

> fun("this","that")
I don't do anything, haha!
But here are your stupid args:
 this
 that
> fun("this","that", nonexistentarg = "really?")
Error in fun("this", "that", nonexistentarg = "really?") :
  unused argument (nonexistentarg = "really?")

If you want to pass in arbitrary arguments you need the ellipsis (...) argument.

> fun <- function(arg1, arg2, ...)  cat(paste("I don't do anything, haha!\nBut here are your stupid args:\n", arg1, "\n", arg2, "\n"))
> fun("this","that", nonexistentarg = "really?")
I don't do anything, haha!
But here are your stupid args:
 this
 that

Which doesn't help with this stupid example, but say you have something else.

> fun <- function(vals, ...) plot(vals, ...)
> fun(1:10)
> fun(1:10, main = "Look Ma! Sweet R plot.")
> fun(1:10, main = "Whatevs", xlab = "something", ylab = "or other")

Now you can pass arbitrary commands to other functions, but only if a.) you use the ellipsis in your calls to sub-functions, and b.) the sub-functions have arguments with that name, and c.) you use argument names (e.g., you cannot use positional arguments for any arguments past the ellipsis). In other words,

> fun <- function(x, ..., y) plot(x,y, ...)
> fun(1:10, 2:11)
Error in plot(x, y, ...) (from #1) :
  error in evaluating the argument 'y' in selecting a method for function 'plot': Error: argument "y" is missing, with no default
> fun(1:10, y = 2:11, main = "MAIN TITLE")
ADD COMMENT

Login before adding your answer.

Traffic: 999 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6