Quantcast

Debugging a program written in the R language

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Debugging a program written in the R language

Thomas L Jones
>From Tom:

The subject is debugging a program written in the R language,under
Windows. (Sorry, but I do not know either the Apple OS or *nix.) A
computer program will usually not work on the first try, if only
because of the risk of typos. Instead, it must be debugged. Roughly,
here is the sequence:

(1) One codes a program using the R language,  and stores it on the
hard drive, using some particular editor.

(2) The program is fed to the R software, together with test data,
etc.

(3) A test computation is run and bugs are spotted.

(4) The program is corrected, using an editor, and the revised version
is stored on the hard drive.

(5) The sequence goes back to step (2) and is repeated until the
program hopefully works.

Unfortunately, the documentation doesn't really explain how to do all
of this, or if it is explained in the documentation, I can't find it.

Reading between the lines a bit, I infer that you are supposed to be
able to use something called a History file, then sort of work
backward in the code and make corrections. I never got it to work for
me. Also, it is unclear how you would handle code entered six weeks or
six months ago.

That is the bad news; the good news is that some kind soul told me
about a key trick; prepare the program in Windows text format (.txt)
and copy it and paste it into the console. The program will now run
from a user-defined "wrapper" or "driver" function.

I am aware that there is an editor called Emacs which you can use if
you are a member of the *nix community, which I am not.

Question: How are you -supposed- to debug a program which you have
written in the R language?

Tom
Thomas L. Jones, Ph.D., Computer Science

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Debugging a program written in the R language

Liaw, Andy
Firstly, some corrections:

- It's best to write code as functions in R, just like most sensible
languages.  You can then debug the functions.

- (X)Emacs has versions for Windows.  Prof. John Fox has kindly made
available some instructions on how Xemacs/ESS can be set up on Windows.

Now, Roger Peng has kindly made available a note on debugging R code:
http://www.biostat.jhsph.edu/~rpeng/docs/R-debug-tools.pdf

Also, for compiled code linked to R, Duncan Murdoch's notes will be helpful:
http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/

Finally, there's a `debug' package on CRAN with a R News article describing
it.

Andy

From: Thomas L Jones

>
> >From Tom:
>
> The subject is debugging a program written in the R language,under
> Windows. (Sorry, but I do not know either the Apple OS or *nix.) A
> computer program will usually not work on the first try, if only
> because of the risk of typos. Instead, it must be debugged. Roughly,
> here is the sequence:
>
> (1) One codes a program using the R language,  and stores it on the
> hard drive, using some particular editor.
>
> (2) The program is fed to the R software, together with test data,
> etc.
>
> (3) A test computation is run and bugs are spotted.
>
> (4) The program is corrected, using an editor, and the
> revised version
> is stored on the hard drive.
>
> (5) The sequence goes back to step (2) and is repeated until the
> program hopefully works.
>
> Unfortunately, the documentation doesn't really explain how to do all
> of this, or if it is explained in the documentation, I can't find it.
>
> Reading between the lines a bit, I infer that you are supposed to be
> able to use something called a History file, then sort of work
> backward in the code and make corrections. I never got it to work for
> me. Also, it is unclear how you would handle code entered six
> weeks or
> six months ago.
>
> That is the bad news; the good news is that some kind soul told me
> about a key trick; prepare the program in Windows text format (.txt)
> and copy it and paste it into the console. The program will now run
> from a user-defined "wrapper" or "driver" function.
>
> I am aware that there is an editor called Emacs which you can use if
> you are a member of the *nix community, which I am not.
>
> Question: How are you -supposed- to debug a program which you have
> written in the R language?
>
> Tom
> Thomas L. Jones, Ph.D., Computer Science
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Debugging a program written in the R language

Mike Prager
In reply to this post by Thomas L Jones
 From the R prompt, try typing

?source

A good editor, specialized for working with R source on Windows, is Tinn-R.



on 3/8/2006 4:12 PM Thomas L Jones said the following:

> >From Tom:
>
> The subject is debugging a program written in the R language,under
> Windows. (Sorry, but I do not know either the Apple OS or *nix.) A
> computer program will usually not work on the first try, if only
> because of the risk of typos. Instead, it must be debugged. Roughly,
> here is the sequence:
>
> (1) One codes a program using the R language,  and stores it on the
> hard drive, using some particular editor.
>
> (2) The program is fed to the R software, together with test data,
> etc.
>
> (3) A test computation is run and bugs are spotted.
>
> (4) The program is corrected, using an editor, and the revised version
> is stored on the hard drive.
>
> (5) The sequence goes back to step (2) and is repeated until the
> program hopefully works.
>
> Unfortunately, the documentation doesn't really explain how to do all
> of this, or if it is explained in the documentation, I can't find it.
>
> Reading between the lines a bit, I infer that you are supposed to be
> able to use something called a History file, then sort of work
> backward in the code and make corrections. I never got it to work for
> me. Also, it is unclear how you would handle code entered six weeks or
> six months ago.
>
> That is the bad news; the good news is that some kind soul told me
> about a key trick; prepare the program in Windows text format (.txt)
> and copy it and paste it into the console. The program will now run
> from a user-defined "wrapper" or "driver" function.
>
> I am aware that there is an editor called Emacs which you can use if
> you are a member of the *nix community, which I am not.
>
> Question: How are you -supposed- to debug a program which you have
> written in the R language?
>
> Tom
> Thomas L. Jones, Ph.D., Computer Science
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>  

--

Michael Prager, Ph.D.
Population Dynamics Team, NMFS SE Fisheries Science Center
NOAA Center for Coastal Fisheries and Habitat Research
Beaufort, North Carolina  28516
http://shrimp.ccfhrb.noaa.gov/~mprager/
Opinions expressed are personal, not official.  No
government endorsement of any product is made or implied.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Debugging a program written in the R language

roger bos
In reply to this post by Thomas L Jones
You could always use notepad, but there are better solutions.  There are
many text editors which will send the commands to R for you and return the
results and also offer syntax highlighting.  I like Tinn-R.  Xemacs is
probably the best, but its hard to learn (IMHO) and I have not taken the
time to do so.  There is also a host of GUIs that people are developing for
R.  Take a look at:
http://www.sciviews.org/_rgui/

I suggest R commander.
library(Rcmdr)
You will need to download it from CRAN.

HTH,

Roger

On 3/8/06, Thomas L Jones <[hidden email]> wrote:

>
> >From Tom:
>
> The subject is debugging a program written in the R language,under
> Windows. (Sorry, but I do not know either the Apple OS or *nix.) A
> computer program will usually not work on the first try, if only
> because of the risk of typos. Instead, it must be debugged. Roughly,
> here is the sequence:
>
> (1) One codes a program using the R language,  and stores it on the
> hard drive, using some particular editor.
>
> (2) The program is fed to the R software, together with test data,
> etc.
>
> (3) A test computation is run and bugs are spotted.
>
> (4) The program is corrected, using an editor, and the revised version
> is stored on the hard drive.
>
> (5) The sequence goes back to step (2) and is repeated until the
> program hopefully works.
>
> Unfortunately, the documentation doesn't really explain how to do all
> of this, or if it is explained in the documentation, I can't find it.
>
> Reading between the lines a bit, I infer that you are supposed to be
> able to use something called a History file, then sort of work
> backward in the code and make corrections. I never got it to work for
> me. Also, it is unclear how you would handle code entered six weeks or
> six months ago.
>
> That is the bad news; the good news is that some kind soul told me
> about a key trick; prepare the program in Windows text format (.txt)
> and copy it and paste it into the console. The program will now run
> from a user-defined "wrapper" or "driver" function.
>
> I am aware that there is an editor called Emacs which you can use if
> you are a member of the *nix community, which I am not.
>
> Question: How are you -supposed- to debug a program which you have
> written in the R language?
>
> Tom
> Thomas L. Jones, Ph.D., Computer Science
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Debugging a program written in the R language

Ferdinand Alimadhi
In reply to this post by Thomas L Jones
The simplest way:

1. Write the program using your favorite text editor and save it i.e.
c:/try.R
2. Open R and write source("c:/try.R")

See "source" help (?source) for more information

I would recommend that, even you are a windows user, you use the emacs
package for statistic (ESS). There are also other "IDE"s available.
In this respect I actually do not see any difference between R and other
programming languages

HTH



Thomas L Jones wrote:

>>From Tom:
>
>The subject is debugging a program written in the R language,under
>Windows. (Sorry, but I do not know either the Apple OS or *nix.) A
>computer program will usually not work on the first try, if only
>because of the risk of typos. Instead, it must be debugged. Roughly,
>here is the sequence:
>
>(1) One codes a program using the R language,  and stores it on the
>hard drive, using some particular editor.
>
>(2) The program is fed to the R software, together with test data,
>etc.
>
>(3) A test computation is run and bugs are spotted.
>
>(4) The program is corrected, using an editor, and the revised version
>is stored on the hard drive.
>
>(5) The sequence goes back to step (2) and is repeated until the
>program hopefully works.
>
>Unfortunately, the documentation doesn't really explain how to do all
>of this, or if it is explained in the documentation, I can't find it.
>
>Reading between the lines a bit, I infer that you are supposed to be
>able to use something called a History file, then sort of work
>backward in the code and make corrections. I never got it to work for
>me. Also, it is unclear how you would handle code entered six weeks or
>six months ago.
>
>That is the bad news; the good news is that some kind soul told me
>about a key trick; prepare the program in Windows text format (.txt)
>and copy it and paste it into the console. The program will now run
>from a user-defined "wrapper" or "driver" function.
>
>I am aware that there is an editor called Emacs which you can use if
>you are a member of the *nix community, which I am not.
>
>Question: How are you -supposed- to debug a program which you have
>written in the R language?
>
>Tom
>Thomas L. Jones, Ph.D., Computer Science
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>  
>


--
Ferdinand Alimadhi
Programmer / Analyst
Harvard University
The Institute for Quantitative Social Science
(617) 496-0187
[hidden email]
www.iq.harvard.edu

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Debugging a program written in the R language

Gabor Grothendieck
In reply to this post by Thomas L Jones
On 3/8/06, Thomas L Jones <[hidden email]> wrote:

> >From Tom:
>
> The subject is debugging a program written in the R language,under
> Windows. (Sorry, but I do not know either the Apple OS or *nix.) A
> computer program will usually not work on the first try, if only
> because of the risk of typos. Instead, it must be debugged. Roughly,
> here is the sequence:
>
> (1) One codes a program using the R language,  and stores it on the
> hard drive, using some particular editor.

Editors with various levels of integration with R can
be found at these links:

http://www.sciviews.org/_rgui/projects/Editors.html
http://ess.r-project.org/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Debugging a program written in the R language

Prof Brian Ripley
In reply to this post by Liaw, Andy
On Wed, 8 Mar 2006, Liaw, Andy wrote:

[...]
> Now, Roger Peng has kindly made available a note on debugging R code:
> http://www.biostat.jhsph.edu/~rpeng/docs/R-debug-tools.pdf

There is also a new chapter in the `Writing R Extensions' manual in the
R-devel version of R (to be R 2.3.0).  So now is a very good time for
people to offer further information/suggestions for that chapter.

> Also, for compiled code linked to R, Duncan Murdoch's notes will be helpful:
> http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/

That's Windows-specific: the new chapter is mainly Unix-based.

[...]

--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Debugging a program written in the R language

Robert Burrows
In reply to this post by Thomas L Jones

Thomas,

This is what I use for writing R programs. I use it with Linux but
adapting it for Windows shouldn't be that much of a problem for us MIT
guys.

In ~/.Rprofile (so it gets loaded every time I start R) I have
myedit <- function(object) {
     system("if [ ! -d $HOME/stat-misc/Rsrc ]; then mkdir $HOME/stat-misc/Rsrc; fi")
     system(paste("vim $HOME/stat-misc/Rsrc/",object,".R",sep=""))
     source(paste("~/stat-misc/Rsrc/",object,".R",sep=""))
}

To write a program called 'functionName' I do, within R,
"myedit('functionName')". This opens up an editor, vim in my case, for
writing the function and saves the result in the ~/stat-misc/Rsrc
directory and in the current R session. I can now repeat
"myedit('functionName')" until I finally get it right. The function is
also accessible with myedit() from any other R session at any time.

Good luck,

Robert Burrows, PhD
New England Biometrics
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Debugging a program written in the R language

Earl F. Glynn
In reply to this post by Prof Brian Ripley
"Prof Brian Ripley" <[hidden email]> wrote in message
news:[hidden email]...
> On Wed, 8 Mar 2006, Liaw, Andy wrote:

> So now is a very good time for
> people to offer further information/suggestions for that chapter.

This may be useful:
Mark Bravington, "Debugging Without (Too Many) Tears," R News, Vol 3, No 3,
Dec 2003, pp. 29-32.



R-News:

http://cran.r-project.org/doc/Rnews/



Exact link:

http://cran.r-project.org/doc/Rnews/Rnews_2003-3.pdf



efg

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Loading...