[Rd] source, sys.source and error line numbers

Hadley Wickham h.wickham at gmail.com
Tue Mar 26 23:45:07 CET 2013


It turns out the reason for this is pretty simple:

sys.source does:
for (i in exprs) eval(i, envir)

where source basically does
n <- length(exprs)
for (i in seq_len(n)) eval(expr[i], envir)

so the problem is presumably related to the way that for strips attributes.

Hadley

On Tue, Mar 19, 2013 at 4:03 AM, Renaud Gaujoux
<renaud at mancala.cbio.uct.ac.za> wrote:
> Hi,
>
> is there a way to retrieve the line number of where en error occurred when
> sourcing a file in a tryCatch statement? Is it stored somewhere accessible?
> It is not found in the error object.
>
> Consider the following code/output and note the difference in the traceback
> between source (has line number) and sys.source (has no line number).
>
> Thank you,
> Renaud
>
>
> ########
> # code
> ########
> codefile <- tempfile()
> write("# some comment
> # some good lines
> a <- 1
> # a bad line
> stop('an error')
> # another good line
> b <- 2
> ", file=codefile)
>
> # with source() the line number is displayed
> source(codefile)
> traceback()
> tryCatch(source(codefile), error= function(e){ str(e) })
>
> # with sys.source() the line number is _not_ displayed
> e <- new.env()
> sys.source(codefile, e)
> traceback()
>
> sessionInfo()
>
> #####
> # output
> #####
>
>> codefile <- tempfile()
>> write("# some comment
> + # some good lines
> + a <- 1
> + # a bad line
> + stop('an error')
> + # another good line
> + b <- 2
> + ", file=codefile)
>> # with source() the line number is displayed
>> source(codefile)
> Error in eval(expr, envir, enclos) : an error
>> traceback()
> 5: stop("an error") at file46641af8754#5
> 4: eval(expr, envir, enclos)
> 3: eval(ei, envir)
> 2: withVisible(eval(ei, envir))
> 1: source(codefile)
>> tryCatch(source(codefile), error= function(e){ str(e) })
> List of 2
>  $ message: chr "an error"
>  $ call   : language eval(expr, envir, enclos)
>  - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
>>
>> # with sys.source() the line number is _not_ displayed
>> e <- new.env()
>> sys.source(codefile, e)
> Error in eval(expr, envir, enclos) : an error
>> traceback()
> 4: stop("an error")
> 3: eval(expr, envir, enclos)
> 2: eval(i, envir)
> 1: sys.source(codefile, e)
>>
>> sessionInfo()
> R version 2.15.3 (2013-03-01)
> Platform: i686-pc-linux-gnu (32-bit)
>
> locale:
>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
>  [7] LC_PAPER=C                 LC_NAME=C
>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



-- 
Chief Scientist, RStudio
http://had.co.nz/



More information about the R-devel mailing list