torenunez
torenunez

Reputation: 130

How do I supress sqldf messages in R Markdown?

I have an R code chunk that calls the sqldf package. When I knit the Rmd into HTML, the HTML prints and displays the sqldf code. How do I hide/supress it? I've already tried to tinker with the chunk settings by turning ECHO off, suppressing warnings, etc...

This is the printed text I want to remove from the HTML:

'## sqldf: library(RSQLite)
'## sqldf: m <- dbDriver("SQLite")
'## sqldf: connection <- dbConnect(m, dbname = ":memory:")
'## sqldf: initExtension(connection)
'## sqldf: dbWriteTable

....etc.

These are the current settings of my chunk:

```{r sqldata, echo = FALSE, message=F, warning=F}

library(sqldf)
MyQuery <- sqldf(MyString,verbose=TRUE) 

```

Upvotes: 1

Views: 496

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 146224

If you don't want any output at all (no messages, no data frame, no code shown...), set include = FALSE as a chunk option.

If you want to show the code, but you don't like the sqldf notes, change verbose = TRUE to verbose = FALSE.

If you look at the sqldf code, you see things like this:

if (verbose) {
    cat("sqldf: dbGetQuery(connection, '", x[i], 
        "')\n", sep = "")
}

So those outputs are produced by cat(), not message() or warning(), so to knitr they are indistinguishable from the object printed to the console at the end.

If you really want to show the code (chunk option echo = TRUE) with (sqldf option) verbose = TRUE, but you don't want all the verbose output (seems strange) I'd suggest spoofing it with two code chunks, one that is not evaluated but displayed, and one that is evaluated but not included.

```{r sqldata, eval = FALSE}
library(sqldf)
MyQuery <- sqldf(MyString,verbose=TRUE)
```


```{r sqldata, eval = TRUE, include = FALSE}
library(sqldf)
MyQuery <- sqldf(MyString)
```

Upvotes: 4

Related Questions