1

I am using a summary() function from an R package. It prints out multiple results by using cat() and print() functions interweavingly (I checked its source code by getAnywhere()). It uses cat() to output descriptive messages, and uses print() to print out the dataframe and the matrix that I wanted to save. I am using R notebook, so I can see the cat() output and matrix output in the R Console tab, and the dataframe output by itself in another result tab. I wasn't able to save or retrieve the dataframe.

I tried sink() and capture.output() functions, but it only saved the descriptive messages and matrix together in a text file, and no dataframe saving. Does anyone know how to retrieve the dataframe object by itself? And I may also need to retrieve the matrix object by itself too.


The R package is called "lmmlasso". I guess it's not very well maintained. Here I am just giving an example code:

summary.foo= function(){
  cat("random effects\n")
  print(matrix(rnorm(9),3))
  cat("fixed effects \n")
  print(data.frame(X = c("A","B","C"), Estimate = rnorm(3)))
}

summary.foo()

I was not able to solve it directly, so I turned to the alternative way: I copied the original source code, return the dataframe and matrix objects at the end, and changed its function name. I got the dataframe by using the new summary function.

huajun
  • 31
  • 5
  • 1
    Is this a private package, or are you able to share enough details to allow folks here to help you? – Allan Cameron Jan 06 '23 at 20:42
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 06 '23 at 21:11
  • @AllanCameron It's a package called "lmmlasso". I guess it's a very initial implementation and lack of maintenance. – huajun Jan 07 '23 at 21:24
  • @MrFlick Hi. I just included an example code. I solved the problem indirectly, but it's still meaningful to discuss other possible ways. – huajun Jan 07 '23 at 21:25
  • There's no general way to solve this problem because every function might do something different. That's one of the downsides I guess of letting anyone publish whatever package code they want to CRAN -- it's not necessarily consistent. There's not one solution that will work for everything so without specific examples, it's really impossible to offer specific advice. – MrFlick Jan 09 '23 at 14:40

1 Answers1

-1

You should provide a reproducible code. Anyway, try to assign it to a variable and then use the variable attributes, either with $ or @, to access the output you desire

RYann
  • 567
  • 1
  • 7