[Rd] Minor "bug" in source()

Henrik Bengtsson hb at maths.lth.se
Tue Jul 19 09:16:05 CEST 2005


For R v2.1.1 patched and R v2.2.0 devel:

Calling source(file, chdir=TRUE) with is.character(file) != TRUE, that 
is, with 'file' as a connection, will generate an error. Example:

 > file <- textConnection("cat('Hello world\n')")
 > source(file, chdir=TRUE)
Error in source(file, chdir = TRUE) : Object "ofile" not found

Of course, it does not make sense to change working directory, but 
quietly ignoring 'chdir' is better, I think.

This is due to a simple coding mistake in source() [sorry, 56k modem
=> no R source => no diff]:

<snip></snip>
     if (is.character(file)) {
         ofile <- file
         if (capabilities("iconv")) {
             if (identical(encoding, "unknown")) {
         <snip></snip>
     }
     Ne <- length(exprs <- .Internal(parse(file, n = -1, NULL,
         "?")))
     if (verbose)
         cat("--> parsed", Ne, "expressions; now eval(.)ing them:\n")
     if (Ne == 0)
         return(invisible())
     if (chdir && is.character(ofile)) {        <=== HERE
         isURL <- length(grep("^(ftp|http|file)://", ofile)) >
             0
         if (!isURL && (path <- dirname(ofile)) != ".") {
             owd <- getwd()
             on.exit(setwd(owd), add = TRUE)
             setwd(path)
         }
     }
<snip></snip>

I suggest to either i) do 'ofile <- file' before the first if-clause 
above, or better ii) move the last if-clause inside the first one (and 
remove " && is.character(ofile)").

Best

Henrik



More information about the R-devel mailing list