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

spin R to Rmd produces a file which doesn't run correctly in RStudio #1590

Closed
avehtari opened this issue Aug 8, 2018 · 9 comments
Closed
Milestone

Comments

@avehtari
Copy link

avehtari commented Aug 8, 2018

Tested both with CRAN and github version.

Download https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R and run

spin("knitr-spin.R", knit = FALSE, format = "Rmd")

This produces knitr-spin.Rmd which can be further successfully knitted to, e.g., html, but can't be run in RStudio.
The following lines in knitr-spin.R

#+ test-chisq5
sum(x^2) # chi-square distribution with df 5
#+ test-chisq4
sum((x - mean(x))^2) # df is 4 now

are spun to following lines in knitr-spin.Rmd

```{r test-chisq5}
sum(x^2) # chi-square distribution with df 5
```{r test-chisq4}
sum((x - mean(x))^2) # df is 4 now
```

If this is run in RStudio, the following error occurs

> ```{r test-chisq4}
Error: attempt to use zero-length variable name

The error comes as there are three backticks missing from the block test-chisq5. A working Rmd looks like this

```{r test-chisq5}
sum(x^2) # chi-square distribution with df 5
```
```{r test-chisq4}
sum((x - mean(x))^2) # df is 4 now
```
@cderv
Copy link
Collaborator

cderv commented Aug 8, 2018

Hi, I found the question interesting because the example file is here for a long time now. So I tried to investigate, using reprex as much as possible.

# Get the file
temp_file <- tempfile(fileext = ".R")
file.copy(system.file("examples/knitr-spin.R", package = "knitr"), temp_file)
#> [1] TRUE
# spin 
rmd <- knitr::spin(temp_file, knit = FALSE, format = "Rmd")
# seems to some backticks are lacking
readLines(rmd)[40:50]
#>  [1] ""                                                                          
#>  [2] "And you can also write two chunks successively like this:"                 
#>  [3] ""                                                                          
#>  [4] "```{r test-chisq5}"                                                        
#>  [5] "sum(x^2) # chi-square distribution with df 5"                              
#>  [6] "```{r test-chisq4}"                                                        
#>  [7] "sum((x - mean(x))^2) # df is 4 now"                                        
#>  [8] "```"                                                                       
#>  [9] ""                                                                          
#> [10] "Done. Call spin('knitr-spin.R') to make silk from sow's ear now and knit a"
#> [11] "lovely purse."
# no error
html <- rmarkdown::render(rmd, quiet = TRUE)
# Check in interactive RStudio
file.edit(rmd)

then clic on knit button. and …
this working for me with those version

#> RStudio : 1.2.747
#> Knitr : 1.20
#> rmarkdown: 1.10

I also tested with
Knitr : 1.20.10
rmarkdown: 1.10.11

I can’t reproduce but I agree it is lacking some ``` to be a correctly formated RMD.
It is just missing a new line in the R file to have a correct format

# add a space in R file
lines <- xfun::read_utf8(temp_file)
which_line <- which(lines == "#+ test-chisq4")
new_lines <- c(lines[seq.int(1:(which_line - 1))], "", lines[seq.int(which_line, length(lines))])
xfun::write_utf8(new_lines, temp_file)
# re-spin
rmd <- knitr::spin(temp_file, knit = FALSE, format = "Rmd")
# correctly formatted
readLines(rmd)[40:50]
#>  [1] ""                                                                          
#>  [2] "And you can also write two chunks successively like this:"                 
#>  [3] ""                                                                          
#>  [4] "```{r test-chisq5}"                                                        
#>  [5] "sum(x^2) # chi-square distribution with df 5"                              
#>  [6] ""                                                                          
#>  [7] "```{r test-chisq4}"                                                        
#>  [8] "sum((x - mean(x))^2) # df is 4 now"                                        
#>  [9] "```"                                                                       
#> [10] ""                                                                          
#> [11] "Done. Call spin('knitr-spin.R') to make silk from sow's ear now and knit a"

At the end, I am not sure if the current syntax is wrong or not, and why you have an error that I don't have.
Adding a newline seems like a fix, at least for formatting issue, but I let the maintainer see what he has to say.

@avehtari
Copy link
Author

avehtari commented Aug 8, 2018

then clic on knit button. and …
this working for me with those version

Knit works for me, too, but running with clicking "Run All" or "Run Current Chunk" etc. doesn't work.

It is just missing a new line in the R file to have a correct format

I did also notice that it works with extra empty lines, but that breaks the current chunks in R files. I have a project with almost hundred R files, which I would prefer not to have those extra empty lines.

@avehtari
Copy link
Author

avehtari commented Aug 8, 2018

Here's my sessionInfo

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.utf8        
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=fi_FI.utf8        LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.20.10

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18    withr_2.1.2     digest_0.6.15   rprojroot_1.3-2 mime_0.5       
 [6] backports_1.1.2 magrittr_1.5    evaluate_0.11   highr_0.7       stringi_1.2.4  
[11] rstudioapi_0.7  rmarkdown_1.10  devtools_1.13.6 tools_3.5.1     stringr_1.3.1  
[16] markdown_0.8    rsconnect_0.8.8 xfun_0.3        yaml_2.2.0      compiler_3.5.1 
[21] memoise_1.1.0   htmltools_0.3.6

@yihui
Copy link
Owner

yihui commented Aug 8, 2018

It is only a matter of the RStudio IDE support. The closing ``` is not necessary if the next element in the document is also a code chunk, i.e. the above Rmd is syntactically valid. Yes, I know it is a little surprising. This goes way back to knitr's ancestor Noweb: https://en.wikipedia.org/wiki/Noweb

I guess the more I explain, the more confused you will be, so I'd better stop here. I recommend you to ignore my bad example inst/examples/knitr-spin.R. Usually you probably don't write two code chunks next to each other. I mean it should be more common that you write a code chunk, then some text, then another code chunk, ...

@avehtari
Copy link
Author

avehtari commented Aug 8, 2018

It is only a matter of the RStudio IDE support. The closing ``` is not necessary if the next element in the document is also a code chunk, i.e. the above Rmd is syntactically valid

I accept this answer.

I mean it should be more common that you write a code chunk, then some text, then another code chunk, ...

The case I have is where the code forms a logical chunk, but some lines produce too much output and I'd like to use results='hide'. R files would look better without empty lines, but I accept my faith (and try to get my collaborator to switch to Rmd anyway)

@avehtari avehtari closed this as completed Aug 8, 2018
@yihui
Copy link
Owner

yihui commented Aug 8, 2018

I'll see how easy it is to add the missing (but in theory unnecessary) three backticks. Or if you can figure it out in the source code of spin, you are welcome to send a pull request!

@avehtari avehtari reopened this Aug 8, 2018
@yihui yihui added this to the v1.21 milestone Aug 9, 2018
@yihui yihui closed this as completed in 7762747 Aug 9, 2018
@yihui
Copy link
Owner

yihui commented Aug 9, 2018

Should be fixed now. Thanks for the report!

@avehtari
Copy link
Author

Thanks for the fix and very useful package!

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants