0

In the example below I add a new variable (l_100km) to a data.table by reference and then create a plot. When I knit my RMarkdown file to HTML, I would like to see only the code and the plot. However, the data.table is also printed to HTML.

While I could avoid this behaviour by creating a new data.table using the <- operator, this would ignore this specific advantage of a data.table. Any other way to solve this problem?

---
output: html_document
---
```{r}
library(data.table)
myDT <- data.table(mtcars)
myDT[,log.mpg:=log(mpg)] # This line knits the data.table toHTML
plot(myDT$log.mpg,myDT$wt)

newDT <- myDT[,sqrt.mpg:=sqrt(mpg)]  # This avoids HTML output, but its not data.table style, i.e. not elegant
plot(myDT$sqrt.mpg,myDT$wt)
```
Till
  • 707
  • 3
  • 14

1 Answers1

1

The best solution would be to open a new chunk, which will be calculated, but not shown:

---
output: html_document
---
```{r}
library(data.table)
myDT <- data.table(mtcars)
```

```{r, results='hide'}
myDT[,log.mpg:=log(mpg)] # This line knits the data.table toHTML
```

```{r}
plot(myDT$log.mpg,myDT$wt)
newDT <- myDT[,sqrt.mpg:=sqrt(mpg)]  # This avoids HTML output, but its not data.table style, i.e. not elegant
plot(myDT$sqrt.mpg,myDT$wt)
```
J_F
  • 9,956
  • 2
  • 31
  • 55