[英]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.我有一个要转换为 pdf 文件的 Rmd 文件。 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.
目前它位于“静态”文件夹的子文件夹中,该文件夹是我的博客站点结构的一部分,由 blogdown package 创建。 The problem is, when I hit the "Knit" button in RStudio, it calls
rmarkdown::render_site(...)
while I was expecting rmarkdown::render(...)
.问题是,当我点击 RStudio 中的“编织”按钮时,它会调用
rmarkdown::render_site(...)
而我期待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.我确实有一个文件
R/build.R
有一行命令blogdown::build_dir("static")
所以这对我来说很奇怪。 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.当我尝试转换不同文件夹中的其他 Rmd 文件(与我的博客文件夹/文件无关)时,一切都按预期工作得很好。
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:(为了得到我想要的,我目前正在控制台中输入
rmarkdown::render("myfile.Rmd")
,或者我正在使用无限月亮阅读器,但都没有“Knit”按钮那么方便:(
The following is the output from xfun::session_info('blogdown')
after updating the blogdown package to ver 1.1:以下是来自
xfun::session_info('blogdown')
的 output 将 blogdown package 更新到 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:编辑:我不确定这是否有帮助,但我想转换为 pdf 的
HW3.Rmd
文件如下所示:
---
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(...)
.当我将此文件保存在
C:/Users/jungl/Dropbox/GitHub/blog2020/static/Drexel_2021/HW3.Rmd
中的文件夹中时(这里C:/Users/jungl/Dropbox/GitHub/blog2020
所有由 blogdown 创建的文件夹/文件),“Knit”按钮意外调用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(...)
.但是,当我在文件夹中复制与
HW3.Rmd
C:/Users/jungl/Dropbox/test/HW3.Rmd
相同的 HW3.Rmd 时,“Knit”按钮按预期工作并调用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.所以它看起来像 RStudio 的“Knit”按钮以某种方式自动确定它是否应该根据工作中的
rmarkdown::render_site(...)
文件是否位于rmarkdown::render(...)
sub) 包含 blogdown 生成的文件夹/文件的根文件夹的文件夹。
edited: The Github repo is at https://github.com/junglee0713/blog2020
and I just checked that the same problem still persists.编辑:Github 存储库位于
https://github.com/junglee0713/blog2020
,我刚刚检查过同样的问题仍然存在。 The HW3.Rmd
file that I want to convert to PDF is in https://github.com/junglee0713/blog2020/tree/master/static/Drexel_2021
我要转换为 PDF 的
HW3.Rmd
文件位于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.另一个编辑:安装blogdown的开发版本似乎解决了这个问题(下面的输出,注意它仍然调用
rmarkdown::render_site(...)
),但有另一个问题。 It renders other Rmd files in the same directory, say HW1.Rmd
and HW2.Rmd
, to respective PDF files.它将同一目录中的其他 Rmd 文件(例如
HW1.Rmd
和HW2.Rmd
)渲染到相应的 PDF 文件中。
==> 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). 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与HW2.Rmd
中确实有geom_histogram()
)。 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.对我来说更有趣的是,在
HW1.Rmd
、 HW2.Rmd
和HW3.Rmd
所在的文件夹中,还有其他 Rmd 文件我转换为 HTML (比如Drexel_2021_Lecture_1.Rmd
、 Drexel_2021_Lecture_2.Rmd
和Drexel_2021_Lecture_3.Rmd
- 它们是 xaringan 幻灯片)并且它们不会受到针织的影响。
When you click the "Knit" button, blogdown does call rmarkdown::render_site()
first, but this function actually calls rmarkdown::render()
eventually .当您单击“Knit”按钮时, blogdown确实首先调用
rmarkdown::render_site()
,但是这个 function 实际上最终调用rmarkdown::render()
。
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 ).如果单击 Knit 按钮时它没有将您的 Rmd 文件编译为 PDF,请确保您使用的是最新版本的blogdown ,因为这听起来像是我几个月前修复的错误(如有疑问,请尝试更新软件包)。
If you install the current development version of blogdown (restart R after installation):如果安装当前开发版的blogdown (安装后重启R):
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 选项卡中看到编织的详细日志:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.