[R] quotes within quotes

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Apr 9 18:39:05 CEST 2003


On Wed, 9 Apr 2003, janet rosenbaum wrote:

> Thanks for all the suggestions.  
> Some people asked why I'm bothering with all this.  This is just
> the toy code I'm trying on the command-line, but the two errors below
> are the same as I get in the real code.  

I think those people were right: you seem to be misunderstanding do.call.

> It's true that I could avoid the modularity, but it improves readability
> and maintainability immensely.
> 
> Here are my results to three suggestions.  The pathnames below are all
> abbreviated for readability.
> 
> 1.  do.call seems to work.  
> 
> Both of the following successfully read the data in:
> 
> > read.dta("C:/Documents and Settings/mexchn_gary.dta", convert.factors=FALSE)
> > do.call("read.dta", list(file="C:/Documents and Settings/mexchn_gary.dta", convert.factors=FALSE))
> 
> 2.  quotes within quotes:
> 
> Correspondants suggested using single quotes or backslashes to escape 
> the quotes.  It turns out R interprets both the same way.
> 
> > cmd <- "read.dta"
> > opt <- "convert.factors=FALSE"
> > data.file <- 'file="C:/Documents and Settings/mexchn_gary.dta"'
> > do.call(cmd, list(data.file, opt))
> Error in read.dta("file=\"C:/Documents and Settings/mexchn_gary.dta\"",  : 
>         unable to open file

That's nothing to do with quotes within quotes. You should be giving a 
*named list* to do.call, not a list of "var=value" strings.

> 3.  Avoid using quotes within quotes
> 
> When we call read.dta and don't use "file=", it works (see #1), but not 
> inside do.call.
> 
> > data.file <- "C:/Documents and Settings/mexchn_gary.dta"
> > do.call(cmd, list(data.file, opt))
>  Error in if (convert.dates) { : argument is not interpretable as
>  logical

It's your `opt' which is in the wrong format:
	
cmd <- "read.dta"
cf <- FALSE
data.file <- "C:/Documents and Settings/mexchn_gary.dta"
do.call(cmd, list(file=data.file, convert.factors=cf))

is a correct way to use do.call. You could also do something like

args <- list(data.file, cf)
names(args) <- c("file", "convert.factors")
do.call(cmd, args)

and that's the sort of thing discussed for useful examples in section 3.5 
of `S Programming'.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list