17

I have a dataframe which I want to print as a warning to show NA values.

The reason I can't use print alone is that I am running an RMarkdown document which I want to run without adding this dataframe to the pdf but separately print the dataframe with all my other warning messages in the console.

When I try message(df) it just pastes a long string with all of the columns together and converts dates into numeric.

I tried using message(kable(df)) which was almost perfect but the rows don't appear on new lines. Any suggestions?

Molx
  • 6,816
  • 2
  • 31
  • 47
Anon.user111
  • 468
  • 1
  • 5
  • 16
  • I really don't understand what you're referring too in the phrase "to show NA values" in the first sentence – Señor O Apr 12 '16 at 14:27
  • Sorry I should have made this clearer, the NA values are irrelevant. I just want to print a dataframe using message. That dataframe just happens to have rows with NA values in. – Anon.user111 Apr 12 '16 at 14:59

1 Answers1

25

My guess is you want to use capture.output():

> message(paste0(capture.output(iris), collapse = "\n"))
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
...
Thomas
  • 43,637
  • 12
  • 109
  • 140