[R] Printing

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Aug 11 17:59:59 CEST 2024


Às 15:36 de 11/08/2024, Steven Yen escreveu:
> Thanks. Will try it.
> 
> Have not tried it but I think the following may work:
> 
> out$results<-NULL
> 
> out$results$ei<-ap
> 
> out$results$vi<-vap
> 
> All I need is printing by returning out (unless I turn it off). And, 
> retrieve ap and vap as needed as shown above. Guess I need to read more 
> about invisible.
> 
> On 8/11/2024 10:09 PM, Rui Barradas wrote:
>> Às 09:51 de 11/08/2024, Steven Yen escreveu:
>>> Hi
>>>
>>> In the following codes, I had to choose between printing (= TRUE) or 
>>> deliver something for grab (ei, vi). Is there a way to get both--that 
>>> is, to print and also have ei and vi for grab? Thanks.
>>>
>>> Steven
>>>
>>> ...
>>>
>>> out<-round(as.data.frame(cbind(ap,se,t,p)),digits)
>>> out<-cbind(out,sig)
>>> out<-out[!grepl(colnames(zx)[1],rownames(out)),]
>>> if(printing){
>>> cat("\nAPPs of bivariate ordered probit probabilities",
>>>      "\nWritten by Steven T. Yen (Last update: 08.11.24)",
>>>      "\ny1.level=",     y1.level,
>>>      "  y2.level=",     y2.level,
>>>      "\njoint12 =",     joint12,
>>>      "\nmarg1 =",       marg1,
>>>      "\nmarg2 =",       marg2,
>>>      "\ncond12 =",      cond12,
>>>      "\ncond21 =",      cond21,
>>>      "\nCovariance matrix:",vb.method,
>>>      "\nWeighted =",        weighted,
>>>      "\nAt means =",        mean,
>>>      "\nProb x 100 =",      times100,
>>>      "\ntesting ="      ,   testing,
>>>      "\nuse_bb_and_vbb = ",use_bb_and_vbb,
>>>      "\nsample size =",     length(y1),"\n")
>>> if (!resampling) cat("\nSEs by delta method","\n")
>>> if  (resampling) cat("\nSEs K-R resampling with",ndraws,"draws\n")
>>> return(out)
>>> } else {
>>> invisible(list("ei"=ap,"vi"=vap))
>>>
>>> ______________________________________________
>>> 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.
>>
>> Hello,
>>
>> Maybe change the end of the code to return a bigger list.
>>
>>
>> ll <- list(out = out, ei = ap, vi = vap)
>> return(ll)
>> } else {
>> invisible(list("ei"=ap,"vi"=vap))
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>>
Hello,

Use descriptive names, print the data frame in the function and return a 
list invisibly.

Also,
1) Why create a data.frame with rounded numbers? I never do this. To 
round numbers is a matter of results display and in the case of df's 
should be left to the print.data.frame method. Always return the numbers 
as they are. See comment below.
2) And never, ever code the creation of a df as

as.data.frame(cbind(.))

If the vectors are a mix of numeric and character they will all be 
coerced to the least common denominator, all vectors will become of 
class character.



# don't do this
df <- round(as.data.frame(cbind(ap, se, t, p)), digits)
df <- cbind(df, sig)

# do this instead
df <- data.frame(ap, se, t, p, sig)

df <- df[!grepl(colnames(zx)[1],rownames(df)), ]

out <- NULL
if(printing){
   #
   [...rest of code...]
   #
   out$data <- df
   cat("data:\n")
   print(out$data)
}
out$results <- list(ei = ap, vi = vap)
invisible(out)



Hope this helps,

Rui Barradas


-- 
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
www.avg.com



More information about the R-help mailing list