>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
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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
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 <DrJones@alum.mit.edu> 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 > > ______________________________________________ > R-help@stat.math.ethz.ch 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]]
Ferdinand Alimadhi
2006-Mar-08 21:38 UTC
[R] Debugging a program written in the R language
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 > >______________________________________________ >R-help at stat.math.ethz.ch 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 falimadhi at iq.harvard.edu www.iq.harvard.edu
Gabor Grothendieck
2006-Mar-08 22:36 UTC
[R] Debugging a program written in the R language
On 3/8/06, Thomas L Jones <DrJones at alum.mit.edu> 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/
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 rbb at nebiometrics.com