[R] executing from .R source file in the src package

Duncan Murdoch murdoch.duncan at gmail.com
Wed Apr 6 14:05:03 CEST 2011


On 11-04-06 2:13 AM, Larry wrote:
> Can I run R code straight from R src (.R) file instead of .rdb/.rdx?  I of
> course tried simply unzipping tar.gz in the R_LIBS directory but R complains
> with "not a valid installed package".
>
> Real issue: I am very new to R and all, so this could be something basic.
>   I'm trying to use ess-tracebug (Emacs front-end to trace/browser et al).
>   It works great when I trace functions in .R files because browser outputs
> filename+line-number when it hits a breakpoint. i.e. something like this:
>
> debug at /home/lgd/test/R/test3.R#1: {
>
> It even moves the cursor as you step through the function.  This is just
> lovely as I'm sure everyone knows.  However, in case of a trace on function
> in a package (ie loaded from a .rdb/.rdx) there is no filename/linenum
> information probably because its not retained.  i.e. it prints something
> like this:
>
> debugging in: train(net, P, target, error.criterium = "LMS", report = TRUE,
>
> Any way to work around this?  Thanks for all insights.

I don't know ESS, but to get debug info in a package, you need to 
install the package with debug information included.  By default 
source() includes it, installed packages don't.

Setting the environment variable

R_KEEP_PKG_SOURCE=yes

before running

R CMD INSTALL foo.tar.gz

will install the debug information.  Then the browser, etc. will list 
filename and line number information.  This is also necessary for 
setBreakpoint to work, but then you'll also probably need to say which 
environments to look in, e.g.

setBreakpoint("Sweave.R#70", env=environment(Sweave))

will set a breakpoint in whatever function is defined at line 70 of 
Sweave.R, as long as that function lives in the same environment as Sweave.

Duncan Murdoch



More information about the R-help mailing list