[R] Printout and saved results

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Tue Mar 26 13:39:03 CET 2024


Just FYI, the R interpreter typically saves the last value returned briefly
in a variable called .Last.value that can be accessed before you do anything
else.

> sin(.5)
[1] 0.4794255
> temp <- .Last.value
> print(temp)
[1] 0.4794255
> sin(.666)
[1] 0.6178457
> .Last.value
[1] 0.6178457
> temp
[1] 0.4794255
> invisible(sin(0.2))
> .Last.value
[1] 0.1986693

So perhaps if you grab it in time, you can call your function and let the
REPL display it (or not) and yet save the value.



-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Jeff Newmiller via
R-help
Sent: Tuesday, March 26, 2024 1:03 AM
To: r-help using r-project.org
Subject: Re: [R] Printout and saved results

Your desire is not unusual among novices... but it is really not a good idea
for your function to be making those decisions. Look at how R does things:

The lm function prints nothing... it returns an object containing the result
of a linear regression. If you happen to call it directly from the R command
prompt and don't assign it to a variable, then the command interpreter
notices that return value and prints it. Since the lm object has a dedicated
print method associated with it, that output looks different than a plain
list object would... but the fact that it has a special print method
(?print.lm) is just window dressing unrelated to your request.

The important part is that the lm function doesn't even consider printing
anything out... it is the code that calls the function that determines
whether it will get printed. So...

lm( hp ~ disp, data = mtcars )  # printed by command interpreter
z <- lm( hp ~ disp, data = mtcars ) # assignment operator returns the value
of z to the command processor, but invisibly
( z <- lm( hp ~ disp, data = mtcars ) ) # strips off the invisible marking
so the value gets printed

Another example:

f <- function() {
  x <- 4
  x  # doesn't print
  invisible( 5 ) # return invisible result
}

f()  # doesn't print 4 because there is no command prompt looking at x alone
on a line... it is inside f
# command prompt doesn't print 5 because that 5 has been marked as invisible
(f()) # command interpreter prints 5

Leaving it up to the calling code to decide whether to print gives you the
option of calling your analysis function possibly thousands of times and
figuring out some slick way to summarize all those runs without thousands of
printouts that you are not going to wade through anyway and would only slow
the computer down (printing really does slow the computer down!)


On March 25, 2024 9:00:49 PM PDT, Steven Yen <styen using ntu.edu.tw> wrote:
>I just like the subroutine to spit out results (Mean, Std.dev, etc.) and
also be able to access the results for further processing, i.e.,
>
>v$Mean
>
>v$Std.dev
>
>On 3/26/2024 11:24 AM, Richard O'Keefe wrote:
>> Not clear what you mean by "saved".
>> If you call a function and the result is printed, the result is
>> remembered for a wee while in
>> the variable .Last.value, so you can do
>>> function.with.interesting.result(.......)
>>> retained.interesting.result <- .Last.value
>> or even
>>> .Last.value -> retained.interesting.result
>> If you know before you start writing the expression that you want to
>> save the value,
>> you can wrap the assignment in parentheses, making it an expression:
>> 
>>> (retained.interesting.result <-
function.with.interesting.result(......))
>> 
>> On Tue, 26 Mar 2024 at 15:03, Steven Yen <styen using ntu.edu.tw> wrote:
>>> How can I have both printout and saved results at the same time.
>>> 
>>> The subroutine first return "out" and the printout gets printed, but not
>>> saved.
>>> 
>>> I then run the "invisible" line. Results got saved and accessible but no
>>> printout.
>>> 
>>> How can I have both printout and also have the results saved? Thank you!
>>> 
>>>   > dstat4 <- function(data,digits=3){
>>> +   Mean    <- apply(data,2,mean,na.rm=TRUE)
>>> +   Std.dev <- apply(data,2,sd,  na.rm=TRUE)
>>> +   Min <- apply(data,2,min,na.rm=TRUE)
>>> +   Max <- apply(data,2,max,na.rm=TRUE)
>>> +   Obs <- dim(data)[1]
>>> +   out <-round(cbind(Mean,Std.dev,Min,Max,Obs),digits)
>>> +   out
>>> + # invisible(list(Mean=Mean,Std.dev=Std.dev,Min=Min,Max=Max))
>>> + }
>>>   > x1<-rnorm(n=5,mean=5, sd=1)
>>>   > x2<-rnorm(n=5,mean=10,sd=2)
>>>   > w<-rnorm(n=5,mean=2,sd=0.3)
>>>   > mydata<-data.frame(cbind(x1,x2))
>>>   > v<-dstat4(mydata); v
>>>        Mean Std.dev   Min    Max Obs
>>> x1  5.000   0.922 3.900  6.282   5
>>> x2 10.769   1.713 9.209 13.346   5
>>>   > v$Mean
>>> Error in v$Mean : $ operator is invalid for atomic vectors
>>>   > dstat4 <- function(data,digits=3){
>>> +   Mean    <- apply(data,2,mean,na.rm=TRUE)
>>> +   Std.dev <- apply(data,2,sd,  na.rm=TRUE)
>>> +   Min <- apply(data,2,min,na.rm=TRUE)
>>> +   Max <- apply(data,2,max,na.rm=TRUE)
>>> +   Obs <- dim(data)[1]
>>> +   out <-round(cbind(Mean,Std.dev,Min,Max,Obs),digits)
>>> + # out
>>> +   invisible(list(Mean=Mean,Std.dev=Std.dev,Min=Min,Max=Max))
>>> + }
>>> 
>>>   > v<-dstat4(mydata)
>>>   > v$Mean
>>>         x1       x2
>>> 4.233051 9.564454
>>> 
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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 using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.

-- 
Sent from my phone. Please excuse my brevity.

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list