Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:= shouldn't print in knitr #1930

Closed
Javdat opened this issue Nov 26, 2016 · 9 comments
Closed

:= shouldn't print in knitr #1930

Javdat opened this issue Nov 26, 2016 · 9 comments
Milestone

Comments

@Javdat
Copy link

Javdat commented Nov 26, 2016

It is the same as previously solved issue (#505)

Also see: http://stackoverflow.com/questions/15267018/knitr-gets-tricked-by-data-table-assignment/15268392?noredirect=1#comment66714047_15268392

It was supposedly solved in v1.9.5. But issue is still reproducible in v1.9.6

library(data.table)
cars <- data.table(cars)
cars[ , test := rnorm(50)]

This should not get printed in knitr, but it does.

See demonstration: http://rpubs.com/Keell/230348

Related (although I don't have printr loaded): #1326

@shrektan
Copy link
Member

It's related to rmarkdown package I guess, see the issue here:

rstudio/rmarkdown#829

@mattdowle
Copy link
Member

I just commented here : http://stackoverflow.com/questions/15267018/knitr-gets-tricked-by-data-table-assignment/15268392?noredirect=1#comment68916947_15268392

Thanks. Here's the passing test in data.table. As far as I can see it covers yours. What's the difference? Your example does not seem to be reproducible in the sense that I can run it with R --vanilla. https://github.com/Rdatatable/data.table/blob/master/tests/knitr.Rmd

Here is the .save output that R CMD check compares to : https://github.com/Rdatatable/data.table/blob/master/tests/knitr.Rout.save

@mattdowle mattdowle added this to the v1.9.10 milestone Nov 28, 2016
@Javdat
Copy link
Author

Javdat commented Nov 29, 2016

Hi,

I have checked the test. It fails for me in RStudio knit button but passes with R --vanilla.

Further checks shows that it is indeed connected to rmarkdown (As mentioned by shrektan)

Simple modification for the knitr.Rmd file, proves it.
(Adding require(rmarkdown) at the beginning of the code)

```{r test_id, message=FALSE, results="show", echo=TRUE, warning=FALSE}
require(rmarkdown)
require(data.table)              # print?
DT = data.table(x=1:3, y=4:6)    # no
DT                               # yes
DT[, z := 7:9]                   # no
print(DT[, z := 10:12])          # yes
if (1 < 2) DT[, a := 1L]         # no
DT                               # yes
```
Some text.

```{r}
sessionInfo()
```

Notice failure at DT[, z := 7:9]. This is with R --vanilla

> source("knitr.R")
Loading required package: knitr
Loading required package: rmarkdown
Loading required package: data.table
data.table 1.9.6  For help type ?data.table or https://github.com/Rdatatable/data.table/wiki
The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way

```r
require(rmarkdown)
require(data.table)              # print?
DT = data.table(x=1:3, y=4:6)    # no
DT                               # yes
```

```
##    x y
## 1: 1 4
## 2: 2 5
## 3: 3 6
```

```r
DT[, z := 7:9]                   # no
```

```
##    x y z
## 1: 1 4 7
## 2: 2 5 8
## 3: 3 6 9
```

```r
print(DT[, z := 10:12])          # yes
```

```
##    x y  z
## 1: 1 4 10
## 2: 2 5 11
## 3: 3 6 12
```

```r
if (1 < 2) DT[, a := 1L]         # no
```

```
##    x y  z a
## 1: 1 4 10 1
## 2: 2 5 11 1
## 3: 3 6 12 1
```

```r
DT                               # yes
```

```
##    x y  z a
## 1: 1 4 10 1
## 2: 2 5 11 1
## 3: 3 6 12 1
```
Some text.

```r
sessionInfo()
```

```
## R version 3.3.1 (2016-06-21)
## Platform: x86_64-apple-darwin15.0.0 (64-bit)
## Running under: OS X 10.12.1 (Sierra)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] data.table_1.9.6 rmarkdown_1.2    knitr_1.15.1    
## 
## loaded via a namespace (and not attached):
##  [1] backports_1.0.4 magrittr_1.5    rprojroot_1.1   htmltools_0.3.5
##  [5] tools_3.3.1     Rcpp_0.12.8     stringi_1.1.2   digest_0.6.10  
##  [9] stringr_1.1.0   chron_2.3-47    evaluate_0.10
```

@shrektan
Copy link
Member

shrektan commented Dec 1, 2016

@Javdat Yes, it's very annoying, honestly speaking! Now I use invisible(dt[i, j:=expr]) in all my rmarkdown files...

@esox
Copy link

esox commented Jan 17, 2017

This is reproducible in 1.10.0.
I use the same trick as @shrektan (although at times dt <- dt[i, j:=expr]) is more readable) i

@jucor
Copy link

jucor commented Jan 27, 2017

Same problem here. Thanks @shrektan , the invisible workaround saved my documnet from a monstruous 7M-rows table :/ [I couldn't figure out why the knit seemed stuck and R was unresponsive, as everywhere := says it returns the result invisibly].

@mattdowle mattdowle modified the milestones: v1.10.6, Candidate Feb 1, 2017
@Javdat
Copy link
Author

Javdat commented Mar 8, 2017

Is fixed in development version of rmarkdown

See: rstudio/rmarkdown#829

devtools::install_github("rstudio/rmarkdown")

@Javdat Javdat closed this as completed Mar 8, 2017
@MichaelChirico
Copy link
Member

@Javdat if I'm not mistaken, that version has now been pushed to CRAN as well:

https://cran.r-project.org/web/packages/rmarkdown/index.html

@DaveParr
Copy link

DaveParr commented Apr 5, 2017

It has, updating RMarkdown via RStudio pointed at default CRAN to version 1.4 instantaneously fixed the issue. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants