2

How can I suppress the output of a function? In this case, the output i want to suppress is a plot that is automatically produced, even when i assign the function call to an object. For example, if I run:

require(sensemakr)
estimate <- 0.091321        
est_se <- 0.008321
dof <- 10000

sense_df <- sensemakr::ovb_contour_plot(estimate = estimate,
                                        se = est_se,
                                        dof = dof)

I get the values I want in sense_df but it also prints the below plot enter image description here

This is suboptimal as all the code is part of a markdown file and i'm using the results in sense_df to make my own plot in the next chunk. So the automatic printing of this plot is just a bad example of what I go on to make.

I've tried following the advice in this post to use capture.output, invisible, or sink but none seem to work.

EDIT 1:

The function is even overriding {r, fig.show = "hide"} at the beginning of my chunk.

EDIT 2:

I have also attempted to follow the suggestion in this post. Wrapping the function call in dev.off returns the error 'list' object cannot be coerced to type 'double', and wrapping it in pdf(file = NULL) does prevent the plot from showing up but there is still a plot-sized print in the output, just no actual plot within it. E.g.

enter image description here

C.Robin
  • 1,085
  • 1
  • 10
  • 23
  • Have you tried putting an `invisible()` around the code? – user2974951 Dec 07 '21 at 13:56
  • I have and it didn't work. Will edit post to make that clear – C.Robin Dec 07 '21 at 13:58
  • Maybe `dont_plot <- capture.output(.)`. – Rui Barradas Dec 07 '21 at 14:04
  • Try this workaround: https://stackoverflow.com/questions/24759130/disable-plot-display-in-r/24762900#24762900 – qdread Dec 07 '21 at 14:05
  • 1
    Does this answer your question? [Disable plot display in R](https://stackoverflow.com/questions/24759130/disable-plot-display-in-r) – qdread Dec 07 '21 at 14:07
  • 1
    Thanks for these ideas. I have attempted them all but to no avail -- have edited post to document the troubleshooting steps i've tried and results – C.Robin Dec 07 '21 at 14:20
  • 3
    I'd try editing the function or making your own version. [Here's the code on Github](https://github.com/carloscinelli/sensemakr/blob/0fa610909e4bb1082b274cd28673fe5b3b68e8cd/R/ovb_plots.R#L337). Looks like the output you want is generated between lines 367 and 460, everything after that is plotting stuff you can ignore. – Gregor Thomas Dec 07 '21 at 14:45
  • Thanks Gregor. I think that's the right way forward – C.Robin Dec 07 '21 at 14:59

0 Answers0