-
-
Notifications
You must be signed in to change notification settings - Fork 878
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
Comments
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 …
I can’t reproduce but I agree it is lacking some ``` to be a correctly formated RMD. # 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. |
Knit works for me, too, but running with clicking "Run All" or "Run Current Chunk" etc. doesn't work.
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. |
Here's my sessionInfo
|
It is only a matter of the RStudio IDE support. The closing 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 |
I accept this answer.
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) |
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! |
Should be fixed now. Thanks for the report! |
Thanks for the fix and very useful package! |
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. |
Tested both with CRAN and github version.
Download https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R and run
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
are spun to following lines in knitr-spin.Rmd
If this is run in RStudio, the following error occurs
The error comes as there are three backticks missing from the block test-chisq5. A working Rmd looks like this
The text was updated successfully, but these errors were encountered: