简体   繁体   中英

The behavior of the knitr button in RStudio when rendering an Rmd file sitting inside a blogdown static folder

I have an Rmd file that I want to convert to a pdf file. Currently it is located in a subfolder of the "static" folder that is a part of my blog site structure created by the blogdown package. The problem is, when I hit the "Knit" button in RStudio, it calls rmarkdown::render_site(...) while I was expecting rmarkdown::render(...) . I do have a file R/build.R with a single line of a command blogdown::build_dir("static") so this is weird to me. When I tried to convert other Rmd files in a different folder (not associated with my blog folders/files) everything works perfectly fine as expected.

To get what I want, I am currently typing rmarkdown::render("myfile.Rmd") in the console, or I am using infinite moon reader, but neither is as convenient as the "Knit" button:(

The following is the output from xfun::session_info('blogdown') after updating the blogdown package to ver 1.1:

> xfun::session_info('blogdown')
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363), RStudio 1.4.1103

Locale:
  LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
  LC_NUMERIC=C                           LC_TIME=English_United States.1252    

Package version:
  base64enc_0.1.3 BH_1.75.0.0     blogdown_1.1    bookdown_0.21   digest_0.6.27   evaluate_0.14   glue_1.4.2     
  graphics_4.0.3  grDevices_4.0.3 highr_0.8       htmltools_0.5.0 httpuv_1.5.4    jsonlite_1.7.2  knitr_1.30     
  later_1.1.0.1   magrittr_2.0.1  markdown_1.1    methods_4.0.3   mime_0.9        promises_1.1.1  R6_2.5.0       
  Rcpp_1.0.5      rlang_0.4.9     rmarkdown_2.6   servr_0.21      stats_4.0.3     stringi_1.5.3   stringr_1.4.0  
  tinytex_0.29    tools_4.0.3     utils_4.0.3     xfun_0.20       yaml_2.2.1     

edited: I am not absolutely sure if this will help, but the HW3.Rmd file I want to convert to pdf goes like the following:

---
title: "Homework 3"
subtitle: "due Feb 2, 2021"
output:
  pdf_document: default
---

```{r}
1 + 1
```

When I save this file in the folder as in C:/Users/jungl/Dropbox/GitHub/blog2020/static/Drexel_2021/HW3.Rmd (here C:/Users/jungl/Dropbox/GitHub/blog2020 is the root folder that holds all folders/files created by blogdown), the "Knit" button calls, unexpectedly, rmarkdown::render_site(...) . However, when I copy the same HW3.Rmd in a folder as in C:/Users/jungl/Dropbox/test/HW3.Rmd , "Knit" button works as expected and calls rmarkdown::render(...) . So it looks like the "Knit" button of RStudio somehow automatically determines whether it should call rmarkdown::render_site(...) or rmarkdown::render(...) based on whether the Rmd file under work is located in a (sub)folder of the root folder that holds blogdown-generated folders/files.

edited: The Github repo is at https://github.com/junglee0713/blog2020 and I just checked that the same problem still persists. The HW3.Rmd file that I want to convert to PDF is in https://github.com/junglee0713/blog2020/tree/master/static/Drexel_2021

Yet another edit: installing the dev version of blogdown seems to solve the problem (output below, note it still calls rmarkdown::render_site(...) ), but with another issue. It renders other Rmd files in the same directory, say HW1.Rmd and HW2.Rmd , to respective PDF files.

==> rmarkdown::render_site('C:/Users/jungl/Dropbox/GitHub/blog2020/static/Drexel_2021/HW3.Rmd',  encoding = 'UTF-8');
  |..................                                                    |  25%
  ordinary text without R code

  |...................................                                   |  50%
label: setup (with options) 
List of 1
 $ include: logi FALSE



processing file: HW3.Rmd
  |....................................................                  |  75%
  ordinary text without R code

  |......................................................................| 100%
label: unnamed-chunk-1 (with options) 
List of 1
 $ eval: symbol F


"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS HW3.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output HW3.tex --lua-filter "C:\Users\jungl\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\jungl\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in" 
output file: HW3.knit.md


Output created: HW3.pdf
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

So each time I render HW3.Rmd to a PDF, I get unsolicited updates of HW1.pdf and HW2.pdf too (as you see, I don't have any ggplot figure in HW3.Rmd and the output warns about picking better value with binwidth. HW2.Rmd does have geom_histogram() in it). What's more interesting to me is that in the folder where HW1.Rmd , HW2.Rmd , and HW3.Rmd are located, there are other Rmd files I convert to HTML (say Drexel_2021_Lecture_1.Rmd , Drexel_2021_Lecture_2.Rmd , and Drexel_2021_Lecture_3.Rmd -- they are xaringan slides) and they don't get affected by the knitting.

When you click the "Knit" button, blogdown does call rmarkdown::render_site() first, but this function actually calls rmarkdown::render() eventually .

If it doesn't compile your Rmd file to PDF when you click the Knit button, please make sure you are using the latest version of blogdown , because it sounds like a bug that I have fixed a few months ago ( when in doubt, try to update packages ).

If you install the current development version of blogdown (restart R after installation):

install.packages('blogdown', repos = c(
  rstudio = 'https://rstudio.r-universe.dev',
  CRAN = 'https://cloud.r-project.org'
))

you will see detailed log of knitting in the R Markdown tab when you click the Knit button:

R Markdown 选项卡

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM