[R] Testing if a variable is specified within a function & adding TRUE/FALSE options to functions

Simon Goodman s.j.goodman at leeds.ac.uk
Wed Jun 29 17:42:33 CEST 2011


I have 2 related questions about functions.

1. I am writing a function to plot data from a time series with the form

myplot<-function(data, d1,d2) {    }

Where d1 and d2 are two dates in a time series. The idea being that if no
values for d1 and d2 are entered then the function defaults to plotting the
whole time series, else it plots the data for the interval specified by d1
and d2.

I am attempting to test if the variable d1 has been inputted by using a
different function, orginally posted on a R help forum by Brian Ripley.

testObject <- function(object)
{
   exists(as.character(substitute(object)))
}

here testObject(x) returns FALSE if x is not currently present a variable in
the work space image.

testObject works fine outside my plotting function, but not within it.... it
always returns FALSE inside the plotting function even when d1 is being
given by the user.

I get the same result even if the testObject function is defined inside the
plotting function....

I suspect this may be due to enviroment being searched for d1.... but can't
find work out how to make it search for d1 within the 'myplot' function - I
think this can done using 'where' or 'environment' - but the documentation
on these commands is a little opaque.

2. For the 'myplot' function I would also like to add a customlegend=TRUE
(or FALSE) option, which determines if a custom legend is plotted (if not
inputted it would default to TRUE), but haven't been able to find anything
on how to specify this kind TRUE/FALSE of option for functions.

Thanks, Simon 








--
View this message in context: http://r.789695.n4.nabble.com/Testing-if-a-variable-is-specified-within-a-function-adding-TRUE-FALSE-options-to-functions-tp3633248p3633248.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list