rmarkdown의 YAML 현재 날짜
.rmd
처리 할 문서 knitr
와 rmarkdown
패키지 의 YAML 전면에 현재 날짜를 입력하는 트릭이 있는지 궁금 합니다. 예전에 위키 페이지 상단에 다음 줄이있었습니다.
_baptiste, `r format(Sys.time(), "%d %B, %Y")`_
html 출력으로 2014 년 5 월 3 일 baptiste 로 변환됩니다 . 이제는에서 제공하는 고급 pandoc 래퍼를 활용하고 싶지만 rmarkdown
YAML 헤더에 r 코드가 있으면 작동하지 않는 것 같습니다.
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
date: `r format(Sys.time(), "%d %B, %Y")`
author: baptiste
---
Error in yaml::yaml.load(front_matter) :
Scanner error: while scanning for the next token at line 6, column 7
found character that cannot start any token at line 6, column 7
Calls: <Anonymous> ... output_format_from_yaml_front_matter ->
parse_yaml_front_matter -> <Anonymous> -> .Call
해결 방법이 있습니까?
이것은 약간 까다 롭지 만 date
인라인 R 표현식을 인용하여 YAML 에서 필드를 유효 하게 만들어야합니다.
date: "`r format(Sys.time(), '%d %B, %Y')`"
그런 다음 구문 분석 오류가 사라지고 Pandoc이의 값을 사용할 수 있도록 마크 다운 출력에 날짜가 생성됩니다 Sys.time()
.
@Yihui를 팔로우합니다. 이상하게도 나는 그것을 발견했다.
'`r format(Sys.Date(), "%B %d, %Y")`'
다음보다 잘 작동합니다.
"`r format(Sys.Date(), '%B %d, %Y')`"
후자의 경우 RStudio '
는 HTML과 PDF 출력간에 전환 할 때마다 외부 인용 부호를 변경하여 코드를 손상시킵니다 .
또는 작은 따옴표로 큰 따옴표를 그 반대의 경우도 마찬가지입니다.
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
date: '`r format(Sys.time(), "%d %B, %Y")`'
author: baptiste
---
한 가지 해결 방법은 brew
패키지 를 사용하고 YAML 서문을 brew
템플릿 으로 작성하는 것 입니다.
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
date: <%= format(Sys.time(), "%d %B, %Y") %>
author: baptiste
---
이제를 사용 brew_n_render
하여 문서를 사전 처리 한 다음를 brew
통해 실행 하는 함수를 사용할 수 있습니다 rmarkdown
.
brew_n_render <- function(input, ...){
output_file <- gsub("\\.[R|r]md$", ".html", input)
brew::brew(input, 'temp.Rmd'); on.exit(unlink('temp.Rmd'))
rmarkdown::render('temp.Rmd', output_file = output_file)
}
To make this work with the KnitHTML
button in RStudio, you can write a custom output format that will automatically use brew
as the preprocessor. Using brew
to preprocess ensures that the knitr
code chunks in your document are untouched during the preprocessing stage. Ideally, the rmarkdown
package should expose the metadata in its API and allow users to run it through a custom function.
or, perhaps something like the following, see R Markdown Parameterized Reports
params:
reportDate:
input: date
label: 'Report Date:'
value: as.POSIXct(Sys.Date())
For the same problem for me. I solve it using this code .
---
title: "bla bla"
author: "My name"
date: \`r format(Sys.Date(), "%B %d, %Y")`\
output: html_document
---
Update You can use another format too .
---
title: "bla bla"
author: "My name"
date: \`r format(Sys.Date(), "%m %d,%Y")`\
output: html_document
---
Best.
I was bitten by this today. I had
date: "`r format(Sys.Date(), "%B %d, %Y")`"
and got more or less the same error as the OP, but only when knitting to word. Knitting to pdf was fine before I tried knitting to Word. Afterwards it didn't work either.
Error in yaml::yaml.load(front_matter) :
Scanner error: while scanning for the next token at line 3, column 31
found character that cannot start any token at line 3, column 31
Calls: <Anonymous> ... output_format_from_yaml_front_matter ->
parse_yaml_front_matter -> <Anonymous> -> .Call`
Position 31 is the first % sign
Replacing this with
date: '`r format(Sys.Date(), "%B %d, %Y")`'
as advised by MLaVoie, worked fine.
I have no idea why this happened, and I don't have time to go digging - reports to finish.
참고URL : https://stackoverflow.com/questions/23449319/yaml-current-date-in-rmarkdown
'Programing' 카테고리의 다른 글
두 목록을 연결- '+ ='와 extend ()의 차이점 (0) | 2020.04.30 |
---|---|
AtomicInteger의 실제 사용 (0) | 2020.04.30 |
JAX-RS / Jersey 오류 처리를 사용자 정의하는 방법은 무엇입니까? (0) | 2020.04.30 |
현지화 된 스토리 보드 문자열을 업데이트 할 수 있습니까? (0) | 2020.04.30 |
jQuery를 사용한 소문자와 대문자 (0) | 2020.04.30 |