how does one print code

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

how does one print code

David.Epstein
There is quite a long piece of code defining a certain function in one of the R packages.
I think the code has a bug and I want to get the code into a file so that I can take a proper look, and possibly fix it.

how does one do this? (I mean getting the code into a file, not fixing the bug.) I suppose I could copy and paste, but that's a bit error prone for various reasons. I want the same arrangement of code formatting as in the original---copy and paste often messes this up.

I tried using as.character(functionname) but that wasn't at all appreciated. I  tried searching the archives of this forum but couldn't figure out exactly what to search for (got too many hits). I also tried Google, but that was also no help.

Thanks
David
Reply | Threaded
Open this post in threaded view
|

Re: how does one print code

Phil Spector
David -
    You can do what you want pretty easily using sink.
Suppose you want the source code for function "blah"
in the file "blah.func":

sink('blah.func')
print(blah)
sink()

  - Phil Spector
  Statistical Computing Facility
  Department of Statistics
  UC Berkeley
  [hidden email]



On Thu, 8 Apr 2010, David.Epstein wrote:

>
> There is quite a long piece of code defining a certain function in one of the
> R packages.
> I think the code has a bug and I want to get the code into a file so that I
> can take a proper look, and possibly fix it.
>
> how does one do this? (I mean getting the code into a file, not fixing the
> bug.) I suppose I could copy and paste, but that's a bit error prone for
> various reasons. I want the same arrangement of code formatting as in the
> original---copy and paste often messes this up.
>
> I tried using as.character(functionname) but that wasn't at all appreciated.
> I  tried searching the archives of this forum but couldn't figure out
> exactly what to search for (got too many hits). I also tried Google, but
> that was also no help.
>
> Thanks
> David
> --
> View this message in context: http://n4.nabble.com/how-does-one-print-code-tp1788686p1788686.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: how does one print code

Ista Zahn
In reply to this post by David.Epstein
Hi David,
You can use sink, like this:
sink(file="lm.R") ## redirects output to the file lm.R
print(lm) #prints the lm function
sink() #r redirects output back to the console.

Or just download the source...

-Ista

On Thu, Apr 8, 2010 at 10:16 PM, David.Epstein
<[hidden email]>wrote:

>
> There is quite a long piece of code defining a certain function in one of
> the
> R packages.
> I think the code has a bug and I want to get the code into a file so that I
> can take a proper look, and possibly fix it.
>
> how does one do this? (I mean getting the code into a file, not fixing the
> bug.) I suppose I could copy and paste, but that's a bit error prone for
> various reasons. I want the same arrangement of code formatting as in the
> original---copy and paste often messes this up.
>
> I tried using as.character(functionname) but that wasn't at all
> appreciated.
> I  tried searching the archives of this forum but couldn't figure out
> exactly what to search for (got too many hits). I also tried Google, but
> that was also no help.
>
> Thanks
> David
> --
> View this message in context:
> http://n4.nabble.com/how-does-one-print-code-tp1788686p1788686.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>



--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

        [[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: how does one print code

Duncan Murdoch
In reply to this post by David.Epstein
On 08/04/2010 5:16 PM, David.Epstein wrote:
> There is quite a long piece of code defining a certain function in one of the
> R packages.
> I think the code has a bug and I want to get the code into a file so that I
> can take a proper look, and possibly fix it.
>
> how does one do this? (I mean getting the code into a file, not fixing the
> bug.) I suppose I could copy and paste, but that's a bit error prone for
> various reasons. I want the same arrangement of code formatting as in the
> original---copy and paste often messes this up.

You can't get this unless the package was installed with
R_KEEP_PKG_SOURCE.  What you see in the console is *not* the original,
it's a deparsed version of it.

To get the original, download the source to the package, and look in the
R subdirectory.  If it's a base package, the package sources are online at

https://svn.r-project.org/R/trunk/src/library

This is the development trunk; different released versions will be in
different subdirs below /R, e.g. 2.10.1 will be in

https://svn.r-project.org/R/tags/R-2-10-1/src/library/

Duncan Murdoch

>
> I tried using as.character(functionname) but that wasn't at all appreciated.
> I  tried searching the archives of this forum but couldn't figure out
> exactly what to search for (got too many hits). I also tried Google, but
> that was also no help.
>
> Thanks
> David

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: how does one print code

David.Epstein
I downloaded the code, as Duncan Murdoch suggested. I also used sink() as suggested by others and found that the two methods gave identical results. I then fixed the bug in a private file and tried it out on a number of examples. It now seems to work fine, as far as I can tell.

From CRAN, I found a publication date in 2005, and the author's email address. Should I do anything else, in addition to writing to the author, which I have already done?

Thanks
David