From murdoch.duncan at gmail.com  Wed Mar 27 01:12:54 2013
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Tue, 26 Mar 2013 20:12:54 -0400
Subject: [Rd] source, sys.source and error line numbers
In-Reply-To: <CABdHhvGuxJdc6Jgt-nhG5SGDG6m_Tkv0CRXL6VG+Wei-C4=0_w@mail.gmail.com>
References: <CAHavPHHLoyDJodDj6aOmKfAnTDuvYjOcNF_YsGYkJp7w3DOXMw@mail.gmail.com>
	<CABdHhvGuxJdc6Jgt-nhG5SGDG6m_Tkv0CRXL6VG+Wei-C4=0_w@mail.gmail.com>
Message-ID: <51523986.9020503@gmail.com>

On 13-03-26 6:45 PM, Hadley Wickham wrote:
> 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.

That's part of it, but there are also different defaults for 
keep.source.  It needs to be TRUE or the error location won't be known.

I'll fix the difference you noticed after 3.0.0 is released; I think it 
is not serious enough to slip in at this point.

Duncan Murdoch

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



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