title: When showing a ggplot in a shiny app, how do I capture the ggplot warning that appears in the console, and display in the app?
body: <p>I have a simple app, below, that displays a ggplot. ggplot generates a warning in the console (see picture at bottom). I'd like to capture the warning, and display it in the app, under the plot.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>library(shiny)
  library(ggplot2)
  
  ui &lt;- fluidPage(
  
     titlePanel("How do i output ggplot warnings? :("),
     mainPanel(
         plotOutput("my_plot_that_generates_warnings"),
         tags$br(),
         verbatimTextOutput(outputId='ggplot_warnings')
     )
  )
  
  server &lt;- function(input, output) {
  
      messages &lt;- reactiveValues(ggplot_warning = 'How to capture warning and display it?')
      output$my_plot_that_generates_warnings &lt;- renderPlot({
          tryCatch({
  
              ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
                  geom_point() +
                  geom_smooth()
  
          }, message = function(e) {
              messages$ggplot_warning &lt;- e$message
          }, warning = function(e) {
              messages$ggplot_warning &lt;- e$message
          }, error = function(e) {
              messages$ggplot_warning &lt;- e$message
          })
      })
  
      output$ggplot_warnings &lt;- renderPrint({
          cat(messages$ggplot_warning)
      })
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>I assume the underlying problem has to do with lazy evaluation and the fact that the graph doesn't actually get rendered (and the warning generated) until after the <code>trycatch</code>. I can actually get the warning to appear in the <code>verbatimTextOutput</code> by wrapping ggplot call in a <code>print()</code>, but then of course the plot doesn't show up.</p>
  
  <p>In my ignorance of the problem I've tried <code>force()</code> rather than <code>print()</code> but it didn't work.</p>
  
  <p><a href="https://i.stack.imgur.com/hYMAO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hYMAO.png" alt="enter image description here"></a></p>