blue sky
2010-Feb-12 05:40 UTC
[R] How to get the source code for the assignment of a variable?
'get' can give me the source code for a function. Is there a way to get the source code for a variable? In the following example, the source code for x is 'y*y'. Is there a way to get it using the string 'x'?> f=function(){print('xx')} > get('f')function(){print('xx')}> y=3 > x=y*y > get('x')[1] 9
djhurio
2010-Feb-12 07:23 UTC
[R] How to get the source code for the assignment of a variable?
I believe there is not such thing as source code for a variable. I believe if you define x=y*y, x is keeping only the values of y*y, but not how they were computed. Am I right? ----- -- http://djhurio.blogspot.com/ http://twitter.com/djhurio http://www.linkedin.com/in/martinsliberts http://www.csb.gov.lv/ -- View this message in context: http://n4.nabble.com/How-to-get-the-source-code-for-the-assignment-of-a-variable-tp1478140p1478205.html Sent from the R help mailing list archive at Nabble.com.
Sharpie
2010-Feb-12 07:38 UTC
[R] How to get the source code for the assignment of a variable?
djhurio wrote:> > I believe there is not such thing as source code for a variable. I believe > if you define x=y*y, x is keeping only the values of y*y, but not how they > were computed. Am I right? >In general yes. Basic variables do not store a copy of the function call that created them. However, some objects have been build to store a memory of how they were created. Take for example a linear model: model <- lm( demand ~ Time, data = BOD ) One of the components of the `model` variable is called call and it stores a language object that is a copy of the lm function call that was used to generate `model`:> model$calllm(formula = demand ~ Time, data = BOD) This object could be passed to the eval() function to basically re-run the operation that generated `lm`. The str() function can be used to discover what sorts of tidbits may be hiding inside a variable. Hope this helps! -Charlie -- View this message in context: http://n4.nabble.com/How-to-get-the-source-code-for-the-assignment-of-a-variable-tp1478140p1478220.html Sent from the R help mailing list archive at Nabble.com.
Sharpie
2010-Feb-12 07:39 UTC
[R] How to get the source code for the assignment of a variable?
Sharpie wrote:> >> model$call > lm(formula = demand ~ Time, data = BOD) > > This object could be passed to the eval() function to basically re-run the > operation that generated `lm`. >That would be re-run the operation that generated `model`. I think I should go to sleep now... -Charlie -- View this message in context: http://n4.nabble.com/How-to-get-the-source-code-for-the-assignment-of-a-variable-tp1478140p1478223.html Sent from the R help mailing list archive at Nabble.com.
blue sky
2010-Feb-12 16:11 UTC
[R] How to get the source code for the assignment of a variable?
On Fri, Feb 12, 2010 at 1:38 AM, Sharpie <chuck at sharpsteen.net> wrote:> > > djhurio wrote: >> >> I believe there is not such thing as source code for a variable. I believe >> if you define x=y*y, x is keeping only the values of y*y, but not how they >> were computed. Am I right? >> > > In general yes. ?Basic variables do not store a copy of the function call > that created them. > > However, some objects have been build to store a memory of how they were > created. ?Take for example a linear model: > > ? ?model <- lm( demand ~ Time, data = BOD ) > > One of the components of the `model` variable is called call and it stores a > language object that is a copy of the lm function call that was used to > generate `model`: > >> model$call > lm(formula = demand ~ Time, data = BOD) > > This object could be passed to the eval() function to basically re-run the > operation that generated `lm`. > > The str() function can be used to discover what sorts of tidbits may be > hiding inside a variable.Since there is no solution to my original question. Let me change it a bit and see if there is a solution. Rscript essentially parse the R file. I have no idea how Rscript works. Do you know how to modify it to spit out the original code for the assignment of a variable?