yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

hook_purl() writes filename to the md file #2348

Closed fenguoerbian closed 4 weeks ago

fenguoerbian commented 4 weeks ago

After setting knitr::knit_hooks$set(purl = knitr::hook_purl), the filename of the rscript is also included in the knitted md file. I'm not sure if this is a WIP feature or newly introduced bug or something that's setted wrong in my machine.

An example of Rmd file

---
title: "Template Title"
author: "Your Name"
date: "Last compiled on `r format(Sys.time(), '%d %B, %Y')`"
output: 
  html_document:
    toc: yes
    toc_float: yes
    number_section: yes
    keep_md: TRUE
---

```{r setup, include=FALSE}
knitr::knit_hooks$set(purl = knitr::hook_purl)
1 + 1
getwd()
sessioninfo::session_info()

and the rendered .md file is 

title: "Template Title" author: "Your Name" date: "Last compiled on 06 June, 2024" output: html_document: toc: yes toc_float: yes number_section: yes keep_md: TRUE

1 + 1
## [1] 2

D:\playground\rmarkdown\knitr_purl_hook\knitr_purl_hook.R

getwd()
## [1] "D:/playground/rmarkdown/knitr_purl_hook"

D:\playground\rmarkdown\knitr_purl_hook\knitr_purl_hook.R

sessioninfo::session_info()
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value
##  version  R version 4.4.0 (2024-04-24 ucrt)
##  os       Windows 10 x64 (build 19044)
##  system   x86_64, mingw32
##  ui       RTerm
##  language (EN)
##  collate  English_United States.utf8
##  ctype    English_United States.utf8
##  tz       Asia/Shanghai
##  date     2024-06-06
##  pandoc   3.2 @ C:/Users/yj220158/scoop/shims/ (via rmarkdown)
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package     * version date (UTC) lib source
##  bslib         0.7.0   2024-03-29 [1] CRAN (R 4.4.0)
##  cachem        1.1.0   2024-05-16 [1] CRAN (R 4.4.0)
##  cli           3.6.2   2023-12-11 [1] CRAN (R 4.4.0)
##  digest        0.6.35  2024-03-11 [1] CRAN (R 4.4.0)
##  evaluate      0.23    2023-11-01 [1] CRAN (R 4.4.0)
##  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.4.0)
##  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
##  jquerylib     0.1.4   2021-04-26 [1] CRAN (R 4.4.0)
##  jsonlite      1.8.8   2023-12-04 [1] CRAN (R 4.4.0)
##  knitr         1.47    2024-05-29 [1] CRAN (R 4.4.0)
##  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.4.0)
##  R6            2.5.1   2021-08-19 [1] CRAN (R 4.4.0)
##  rlang         1.1.3   2024-01-10 [1] CRAN (R 4.4.0)
##  rmarkdown     2.27    2024-05-17 [1] CRAN (R 4.4.0)
##  rstudioapi    0.16.0  2024-03-24 [1] CRAN (R 4.4.0)
##  sass          0.4.9   2024-03-15 [1] CRAN (R 4.4.0)
##  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
##  xfun          0.44    2024-05-15 [1] CRAN (R 4.4.0)
##  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.4.0)
## 
##  [1] C:/R/MyLibrary
##  [2] C:/R/R-4.4.0/library
## 
## ──────────────────────────────────────────────────────────────────────────────

D:\playground\rmarkdown\knitr_purl_hook\knitr_purl_hook.R


<!--
Please keep the below portion in your issue. Your issue will be closed if any of the boxes is not checked (i.e., replace `[ ]` by `[x]`). In certain (rare) cases, you may be exempted if you give a brief explanation (e.g., you are only making a suggestion for improvement). Thanks!
-->

---

By filing an issue to this repo, I promise that

- [x] I have fully read the issue guide at https://yihui.org/issue/.
- [x] I have provided the necessary information about my issue.
    - If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    - If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included `xfun::session_info('knitr')`. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: `remotes::install_github('yihui/knitr')`.
    - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [x] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.
yihui commented 4 weeks ago

It's a bug caused by https://github.com/yihui/xfun/commit/27484dd048349f296b4af61d9719ae6c41476076. Fixed now. Thanks for the report!