Re: [R] Modify base R functions in Rprofile.site

2009-10-04 Thread cls59



Yihui Xie wrote:
 
 Hi everyone,
 
 I want to modify two base R functions 'parse' and 'deparse'
 immediately after R has started, so I added some code in the file
 'Rprofile.site' under the 'etc' directory. Here is a simple example:
 
 parse=function(...){
 base::parse(...)
 }
 
 I'll get an error when I start R as follows:
 
 Error: cannot change value of locked binding for 'parse'
 
 Is there a solution for my problem? Please note that I *have to*
 override the function names. Thanks very much!
 
 


Hi Yihui,

I have had cause to do some some similar things to Sweave functions-- maybe
the following will help:

  # Find out where the function lives.
  env - as.environment( 'package:base' )

  # Crack the binding.
  unlockBinding( 'parse', env )

  # Replace the function.
  assignInNamespace( 'parse', function(...){
  
   # Your function here, or an object that contains it.

   }, ns = 'base' )

  # Relock the binding.
  lockBinding( 'parse', env )


Note that the above is *very* dark voodoo-- it may cause unforeseen
consequences.

Good luck!

-Charlie

-
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: 
http://www.nabble.com/Modify-base-R-functions-in-Rprofile.site-tp25735437p25739660.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


Re: [R] Modify base R functions in Rprofile.site

2009-10-04 Thread Duncan Murdoch

On 04/10/2009 12:41 PM, cls59 wrote:



Yihui Xie wrote:

Hi everyone,

I want to modify two base R functions 'parse' and 'deparse'
immediately after R has started, so I added some code in the file
'Rprofile.site' under the 'etc' directory. Here is a simple example:


Why not just modify their source, and rebuild R?

Duncan Murdoch



parse=function(...){
base::parse(...)
}

I'll get an error when I start R as follows:

Error: cannot change value of locked binding for 'parse'

Is there a solution for my problem? Please note that I *have to*
override the function names. Thanks very much!





Hi Yihui,

I have had cause to do some some similar things to Sweave functions-- maybe
the following will help:

  # Find out where the function lives.
  env - as.environment( 'package:base' )

  # Crack the binding.
  unlockBinding( 'parse', env )

  # Replace the function.
  assignInNamespace( 'parse', function(...){
  
   # Your function here, or an object that contains it.


   }, ns = 'base' )

  # Relock the binding.
  lockBinding( 'parse', env )


Note that the above is *very* dark voodoo-- it may cause unforeseen
consequences.

Good luck!

-Charlie

-
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University


__
R-help@r-project.org 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.


Re: [R] Modify base R functions in Rprofile.site

2009-10-04 Thread cls59


Duncan Murdoch-2 wrote:
 
 
 Why not just modify their source, and rebuild R?
 
 Duncan Murdoch
 
 

In my case, the IT guys don't always provide the necessary tools and/or
permissions to do this. Which is why we are fortunate that R is so flexible
as to allow modification of core routines in some cases.

-Charlie

-
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: 
http://www.nabble.com/Modify-base-R-functions-in-Rprofile.site-tp25735437p25740138.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


Re: [R] Modify base R functions in Rprofile.site

2009-10-04 Thread Yihui Xie
Thanks a lot, Charlie. What a coincidence -- I'm also working on
Sweave functions. parse() and deparse() can make the code more tidy
(they are used in source() and RweaveLatexRuncode()),

 RweaveLatex
function ()
{
list(setup = RweaveLatexSetup, runcode = RweaveLatexRuncode,
writedoc = RweaveLatexWritedoc, finish = RweaveLatexFinish,
checkopts = RweaveLatexOptions)
}
environment: namespace:utils

but we can either (1) let R automatically 'tidy up' our code and
remove all the comments or (2) keep the comments but leave the
original code untouched.

I worked out a trick to preserve the comments while tidying up the R
code, and I want to replace parse() and deparse() with my customized
functions, in which case Sweave will keep the comments.

However, my functions will need base::parse() and base::deparse() to
help me, therefore my real difficulty is, how to let Sweave use my
functions to parse and deparse R code without really modifying them in
the base enviroment? I'm using the command 'R CMD Sweave'. If there is
no neat approach, I'll change my question to: how to write a package,
say, Sweave2, that can be run using command line 'R CMD Sweave2 file'?

Regards,
Yihui
--
Yihui Xie xieyi...@gmail.com
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA



2009/10/4 cls59 ch...@sharpsteen.net:



 Yihui Xie wrote:

 Hi everyone,

 I want to modify two base R functions 'parse' and 'deparse'
 immediately after R has started, so I added some code in the file
 'Rprofile.site' under the 'etc' directory. Here is a simple example:

 parse=function(...){
     base::parse(...)
 }

 I'll get an error when I start R as follows:

 Error: cannot change value of locked binding for 'parse'

 Is there a solution for my problem? Please note that I *have to*
 override the function names. Thanks very much!




 Hi Yihui,

 I have had cause to do some some similar things to Sweave functions-- maybe
 the following will help:

  # Find out where the function lives.
  env - as.environment( 'package:base' )

  # Crack the binding.
  unlockBinding( 'parse', env )

  # Replace the function.
  assignInNamespace( 'parse', function(...){

   # Your function here, or an object that contains it.

   }, ns = 'base' )

  # Relock the binding.
  lockBinding( 'parse', env )


 Note that the above is *very* dark voodoo-- it may cause unforeseen
 consequences.

 Good luck!

 -Charlie

 -
 Charlie Sharpsteen
 Undergraduate
 Environmental Resources Engineering
 Humboldt State University

__
R-help@r-project.org 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.


Re: [R] Modify base R functions in Rprofile.site

2009-10-04 Thread Duncan Murdoch

On 04/10/2009 6:07 PM, Yihui Xie wrote:

Thanks a lot, Charlie. What a coincidence -- I'm also working on
Sweave functions. parse() and deparse() can make the code more tidy
(they are used in source() and RweaveLatexRuncode()),


RweaveLatex

function ()
{
list(setup = RweaveLatexSetup, runcode = RweaveLatexRuncode,
writedoc = RweaveLatexWritedoc, finish = RweaveLatexFinish,
checkopts = RweaveLatexOptions)
}
environment: namespace:utils

but we can either (1) let R automatically 'tidy up' our code and
remove all the comments or (2) keep the comments but leave the
original code untouched.

I worked out a trick to preserve the comments while tidying up the R
code, and I want to replace parse() and deparse() with my customized
functions, in which case Sweave will keep the comments.

However, my functions will need base::parse() and base::deparse() to
help me, therefore my real difficulty is, how to let Sweave use my
functions to parse and deparse R code without really modifying them in
the base enviroment? I'm using the command 'R CMD Sweave'. 


I think it's really unlikely that you need to modify those two 
functions.  If you leave the source references turned on, the standard 
parse will tell you where all the code is; you can then write it out in 
any way you like, calling the standard deparse on bits and pieces, and 
mixing in the comments as you like.


The disadvantage of replacing parse or deparse is the maintenance:  if 
the standard ones change, you have to decide whether to incorporate 
those changes or not, in every case.  Neither parse nor deparse change a 
lot, but there have still been 2-3 dozen changes in the last year.


Now, if you choose instead to modify the original sources, in most cases 
Subversion will correctly merge the new changes in.  So that's better 
than a wholesale replacement, but still not as good as simply working 
with the output of the standard functions.


 If there is
 no neat approach, I'll change my question to: how to write a package,
 say, Sweave2, that can be run using command line 'R CMD Sweave2 file'?

Currently you can do that by writing Sweave2 as a Perl script and 
installing it in the RHOME/bin directory, but that may change:  we are 
working hard to do away with the need for Perl.


Duncan Murdoch



Regards,
Yihui
--
Yihui Xie xieyi...@gmail.com
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA



2009/10/4 cls59 ch...@sharpsteen.net:



Yihui Xie wrote:

Hi everyone,

I want to modify two base R functions 'parse' and 'deparse'
immediately after R has started, so I added some code in the file
'Rprofile.site' under the 'etc' directory. Here is a simple example:

parse=function(...){
base::parse(...)
}

I'll get an error when I start R as follows:

Error: cannot change value of locked binding for 'parse'

Is there a solution for my problem? Please note that I *have to*
override the function names. Thanks very much!




Hi Yihui,

I have had cause to do some some similar things to Sweave functions-- maybe
the following will help:

 # Find out where the function lives.
 env - as.environment( 'package:base' )

 # Crack the binding.
 unlockBinding( 'parse', env )

 # Replace the function.
 assignInNamespace( 'parse', function(...){

  # Your function here, or an object that contains it.

  }, ns = 'base' )

 # Relock the binding.
 lockBinding( 'parse', env )


Note that the above is *very* dark voodoo-- it may cause unforeseen
consequences.

Good luck!

-Charlie

-
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University


__
R-help@r-project.org 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.


__
R-help@r-project.org 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.


Re: [R] Modify base R functions in Rprofile.site

2009-10-04 Thread cls59


Yihui Xie wrote:
 
 
 Thanks a lot, Charlie. What a coincidence -- I'm also working on 
 Sweave functions. parse() and deparse() can make the code more tidy 
 (they are used in source() and RweaveLatexRuncode()), 
 
 RweaveLatex 
 function () 
 { 
 list(setup = RweaveLatexSetup, runcode = RweaveLatexRuncode, 
 writedoc = RweaveLatexWritedoc, finish = RweaveLatexFinish, 
 checkopts = RweaveLatexOptions) 
 } 
 environment: namespace:utils 
 
 but we can either (1) let R automatically 'tidy up' our code and 
 remove all the comments or (2) keep the comments but leave the 
 original code untouched. 
 
 I worked out a trick to preserve the comments while tidying up the R 
 code, and I want to replace parse() and deparse() with my customized 
 functions, in which case Sweave will keep the comments. 
 
 However, my functions will need base::parse() and base::deparse() to
 help me, therefore my real difficulty is, how to let Sweave use my
 functions to parse and deparse R code without really modifying them in
 the base enviroment? I'm using the command 'R CMD Sweave'. 
 
 

Well, if you want to alter the behavior of RweaveLatexRunCode(), it can be
done from within a Sweave file. This happens to be what I did to Sweave-- I
wanted a little more control over the specifics of how it formatted code
chunk output in the .tex file. The steps involved are:

1. Make a copy of the Sweave function makeRweaveLatexCoderunner(). Patch it
so that it generates a RweaveLatexRuncode() function of your liking.

2. Overwrite utils:::makeRweaveLatexCodeRunner using the method I described
previously.

3. Generate a new code runner function using your patched version of
makeRweaveLatexCodeRunner().

4. Search all environments for old versions of RweaveLatexRuncode() and
replace them with your patched one.

An example of this approach is used to build the tikzDevice vignette, you
can view it at:

http://github.com/Sharpie/RTikZDevice/blob/master/inst/doc/tikzDevice.Rnw

Look for the line that says Begin Mother of All R Hacks.




Yihui Xie wrote:
 
 
 If there is no neat approach, I'll change my question to: how to write a
 package,
 say, Sweave2, that can be run using command line 'R CMD Sweave2 file'?
 
 

This is also doable, at least for Linux and OS X systems that are installing
from source. You need to add a Make target that installs your script, named
Sweave2, into R_HOME/bin/ when the package is installed. This is the
approach we took in the pgfSweave package-- you can view an example at:

http://github.com/cameronbracken/pgfSweave/blob/master/src/Makevars

I hope this helps!

-Charlie

-
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: 
http://www.nabble.com/Modify-base-R-functions-in-Rprofile.site-tp25735437p25743082.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


Re: [R] Modify base R functions in Rprofile.site

2009-10-04 Thread Yihui Xie
Hi Charlie,

Thanks a lot! Your dark voodoo really helps!

And also, thanks so much to Duncan for your detailed explanation!
Finally I used Charlie's trick to modify makeRweaveLatexCoderunner(),
and has left parse()  deparse() untouched.

Regards,
Yihui
--
Yihui Xie xieyi...@gmail.com
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA



On Sun, Oct 4, 2009 at 5:51 PM, cls59 ch...@sharpsteen.net wrote:


 Yihui Xie wrote:


 Thanks a lot, Charlie. What a coincidence -- I'm also working on
 Sweave functions. parse() and deparse() can make the code more tidy
 (they are used in source() and RweaveLatexRuncode()),

 RweaveLatex
 function ()
 {
     list(setup = RweaveLatexSetup, runcode = RweaveLatexRuncode,
         writedoc = RweaveLatexWritedoc, finish = RweaveLatexFinish,
         checkopts = RweaveLatexOptions)
 }
 environment: namespace:utils

 but we can either (1) let R automatically 'tidy up' our code and
 remove all the comments or (2) keep the comments but leave the
 original code untouched.

 I worked out a trick to preserve the comments while tidying up the R
 code, and I want to replace parse() and deparse() with my customized
 functions, in which case Sweave will keep the comments.

 However, my functions will need base::parse() and base::deparse() to
 help me, therefore my real difficulty is, how to let Sweave use my
 functions to parse and deparse R code without really modifying them in
 the base enviroment? I'm using the command 'R CMD Sweave'.



 Well, if you want to alter the behavior of RweaveLatexRunCode(), it can be
 done from within a Sweave file. This happens to be what I did to Sweave-- I
 wanted a little more control over the specifics of how it formatted code
 chunk output in the .tex file. The steps involved are:

 1. Make a copy of the Sweave function makeRweaveLatexCoderunner(). Patch it
 so that it generates a RweaveLatexRuncode() function of your liking.

 2. Overwrite utils:::makeRweaveLatexCodeRunner using the method I described
 previously.

 3. Generate a new code runner function using your patched version of
 makeRweaveLatexCodeRunner().

 4. Search all environments for old versions of RweaveLatexRuncode() and
 replace them with your patched one.

 An example of this approach is used to build the tikzDevice vignette, you
 can view it at:

 http://github.com/Sharpie/RTikZDevice/blob/master/inst/doc/tikzDevice.Rnw

 Look for the line that says Begin Mother of All R Hacks.




 Yihui Xie wrote:


 If there is no neat approach, I'll change my question to: how to write a
 package,
 say, Sweave2, that can be run using command line 'R CMD Sweave2 file'?



 This is also doable, at least for Linux and OS X systems that are installing
 from source. You need to add a Make target that installs your script, named
 Sweave2, into R_HOME/bin/ when the package is installed. This is the
 approach we took in the pgfSweave package-- you can view an example at:

 http://github.com/cameronbracken/pgfSweave/blob/master/src/Makevars

 I hope this helps!

 -Charlie

 -
 Charlie Sharpsteen
 Undergraduate
 Environmental Resources Engineering
 Humboldt State University

__
R-help@r-project.org 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.