如何在Quarto页面共享RMarkdown脚本并避免其被自动渲染?
在Quarto网站中共享RMarkdown源文件而非渲染后的页面
问题场景
我想在公开Quarto页面里共享R和RMarkdown脚本,用quarto_render()渲染页面,脚本放在documents文件夹里。但quarto_render()会自动渲染RMarkdown文件,点击链接打开的是渲染后的网页,不是源文件下载;而R脚本可以正常下载。需要禁用指定Rmd文件的渲染,让它能像R脚本一样被下载。
最小示例
待共享文件位于子文件夹documents/:
documents/example.Rmd(不应被渲染)
--- title: My First RMarkdown Document output: html_document: toc: true toc_float: true self_contained: true --- # Basics ## The Header The **head** is *written* in `yaml` language. ## Hyperlink [Tutorial for Markdown](https://www.markdowntutorial.com/) ## Picture 
documents/example.R(预期行为参考)
# Explore the content of a data frame head(iris) tail(iris) str(iris) summary(iris)
index.qmd
--- title: "My Homepage" --- # Distributed Resources In this page you can download [this R script](documents/example.R) and [this RMarkdown script](documents/example.Rmd).
_quarto.yml(页面设置)
project: type: website output-dir: docs preview: port: 4200 browser: true website: title: "" format: html: theme: litera toc: true
RStudio会话中,使用以下代码渲染并预览页面:
library(quarto) quarto_render() quarto_preview()
注:主页链接可正常下载R脚本,但RMarkdown文件会被渲染成页面,希望像共享R脚本一样共享RMarkdown源文件。
解决方案
方法1:在_quarto.yml中排除指定文件
在项目配置文件_quarto.yml的project字段下添加exclude,指定要跳过渲染的文件或规则:
project: type: website output-dir: docs preview: port: 4200 browser: true exclude: - documents/example.Rmd # 排除单个文件 # 也可以用通配符排除整个目录的Rmd文件:- documents/*.Rmd website: title: "" format: html: theme: litera toc: true
配置后Quarto会跳过指定的Rmd文件,不进行渲染,点击链接即可直接下载源文件。
方法2:使用resources目录存放共享脚本
Quarto默认会将resources目录下的所有文件原样复制到输出目录,不会渲染其中的Rmd文件。操作步骤:
- 创建
resources/documents目录,将example.Rmd和example.R移动到该目录下 - 修改
index.qmd中的链接路径:
--- title: "My Homepage" --- # Distributed Resources In this page you can download [this R script](resources/documents/example.R) and [this RMarkdown script](resources/documents/example.Rmd).
无需额外配置,文件会被直接复制到输出目录,点击链接就能下载源文件。
方法3:临时修改文件扩展名(应急方案)
如果只是临时需求,可以将example.Rmd重命名为example.Rmd.txt或example.txt,然后修改index.qmd中的链接路径。用户下载后可以手动改回.Rmd扩展名。这种方法不够优雅,适合临时场景。
内容的提问来源于stack exchange,提问作者Miguel Alvarez
相关产品推荐
相关产品推荐

