1

Revised title to clarify focus.

We notice an anomaly that R markdown and data.table interact in a surprising way. Same does not happen when knitting LaTeX. Commands which not have a return within the R session do cause a return within the knitted markdown output. I trace the problem back to commands like the following, which do not produce an output in R,

````{r}
poolballs[ , weight2:=2 * weight]
```

but inside Rmarkdown, the output includes the full print of the poolballs DT. Same does not happen if we knit an equivalent chunk in LaTeX.

This produced some funny HTML output because I wrote chunks like this, intending to display only the first 5 lines

```{r}
poolballs[ , weight2:=2 * weight]
head(poolballs)
```

Markdown parses that as the equivalent of two chunks,

> poolballs[ , weight2:=2 * weight]
> poolballs

> head(poolballs)

Here's the markdown file to demonstrate

---
title: "Data Table Guide"
author:
 - name: Paul Johnson
   affiliation: Center for Research Methods and Data Analysis, University of Kansas
   email: pauljohn@ku.edu

date: "`r format(Sys.time(), '%Y %B %d')`" 
output:
   html_document: 
   theme: united 
   highlight: haddock 
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, comment=NA)
options(width = 70)
```

```{r make_pb_dt}
set.seed(234234)
library(data.table)
poolballs <- data.table(
    number = 1:15,
    weight = rnorm(15, 45.7, 0.8),
    diameter = c(3, 2.9, 3.1) #shows recyling
) 
poolballs
```

I want the following to show only head in line 2
```{r}
poolballs[ , weight2:=2 * weight]
head(poolballs)
```

Compare the HTML output:

http://pj.freefaculty.org/scraps/mre-dt.html

I'm sorry if this is a known feature of markdown. I've coded around this wrinkle by hiding chunks, but it seems somewhat inconvenient. Today i'm curious enough to ask you about it. I wrote the same chunks in a LaTeX file and the funny DT output problem does not happen. I put link to PDF from LaTeX in http:/pj.freefaculty.org/scraps/mre-dt-3.pdf

pauljohn32
  • 2,079
  • 21
  • 28

1 Answers1

0

In your final chunk, knitr sees that you have two objects that its attempting to print and you're getting the output for both. This isn't a feature, and has been addressed in a previous question.

If you want to only print the head of the first object in that chunk, your code should be head(poolballs[, weight2:=2 * weight])

Jake Kaupp
  • 7,892
  • 2
  • 26
  • 36
  • This is wrong on several levels. First, the first line produces no output! Run it in R, you should see that. I did not ask for markdown to print it. Second, markdown is behaving differently than noweb documents. Run same chunk in LaTeX file with Sweave or knitr. In neither one does the first line cause output. I do not want to recode that 1 line, I have a purpose in writing it that way. Compare knitted LaTeX here: http://pj.freefaculty.org/scraps/mre-dt-3.pdf (Rnw same folder). Line 1 does not print. – pauljohn32 Dec 01 '16 at 14:23
  • I stand corrected, it might be a bug but it's definitely not a feature. I understand the `data.table` assignment that you're doing shouldn't produce output, but I don't think the print methods in knitr for html recognize that. It sees it as making a call to an `R` object and tries to print it accordingly. You can read more about it here: https://stackoverflow.com/questions/15267018/knitr-gets-tricked-by-data-table-assignment – Jake Kaupp Dec 01 '16 at 14:44