gpt4 book ai didi

r - ifelse Action 取决于rmarkdown中的文档类型

转载 作者:行者123 更新时间:2023-12-04 03:43:44 26 4
gpt4 key购买 nike

使用rmarkdown:http://rmarkdown.rstudio.com/准备报告时,您可能希望根据文档类型来呈现不同的文档。例如,如果要渲染的文档是html文件,那么我可能想要嵌入youtube视频,而好像它是pdf或MS Word,则我希望使用超链接的URL。

有没有办法告诉rmarkdown这样的事情:

if (html) {
<iframe width="640" height="390" src="https://www.youtube.com/embed/FnblmZdTbYs? feature=player_detailpage" frameborder="0" allowfullscreen></iframe>
} else {
https://www.youtube.com/watch?v=ekBJgsfKnlw
}

代码
devtools::install_github("rstudio/rmarkdown")
library(rmarkdown)
render("foo.Rmd", "all")

foo.Rmd
---
title: "For Fun"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
toc: true
theme: journal
number_sections: true
pdf_document:
toc: true
number_sections: true
word_document:
fig_width: 5
fig_height: 5
fig_caption: true
---

## Good times

<iframe width="640" height="390" src="https://www.youtube.com/embed/FnblmZdTbYs?feature=player_detailpage" frameborder="0" allowfullscreen></iframe>

最佳答案

是的,您可以通过knitr::opts_knit$get("rmarkdown.pandoc.to")访问输出格式。这将返回具有目标输出格式的字符串。这是一个例子:

---
title: "Untitled"
output: html_document
---

```{r}
library(knitr)
opts_knit$get("rmarkdown.pandoc.to")
```

对于html_document,返回“html”,对于word_document,返回“docx”,对于pdf_document,返回“latex”。因此,要回答您的问题,您可以执行以下操作:
html <- knitr::opts_knit$get("rmarkdown.pandoc.to") == "html"

关于r - ifelse Action 取决于rmarkdown中的文档类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25528067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com