From ripley at stats.ox.ac.uk  Tue Jul 19 10:23:59 2005
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 19 Jul 2005 09:23:59 +0100 (BST)
Subject: [Rd] Minor "bug" in source()
In-Reply-To: <42DCA8B5.2000808@maths.lth.se>
References: <42DCA8B5.2000808@maths.lth.se>
Message-ID: <Pine.LNX.4.61.0507190921460.3594@gannet.stats>

I think at least a warning is appropriate here (and also for a URL), so 
have modified the code to have one in each case.

There is a reason why the chdir code is later, after the parsing, for it 
was thought more important to get an error from parsing than from failing 
to chdir.

On Tue, 19 Jul 2005, Henrik Bengtsson wrote:

> 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
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

-- 
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



[Rd] Minor "bug" in source()
Prof Brian Ripley