You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为参数化qmd报告构建Quarto网站导航?

为参数化Quarto报告自动生成网站导航的方案

方法一:利用Quarto的listing自动生成导航(推荐)

Quarto网站项目支持通过listing配置自动匹配指定目录下的文件,无需手动逐个添加报告路径,步骤如下:

  1. 批量生成标准化命名的患者报告
    用R或Python脚本循环调用quarto render,传入不同patient参数,将输出文件统一存放到指定目录(如reports/),并保持命名规律(如patient_A.html)。

    示例R脚本:

    patients <- c("A", "B", "C")
    dir.create("reports", showWarnings = FALSE)
    for (p in patients) {
      quarto::quarto_render(
        input = "patient_report.qmd",
        output_file = file.path("reports", paste0("patient_", p, ".html")),
        params = list(patient = p)
      )
    }
    
  2. 配置_quarto.yml实现自动导航
    在网站导航配置中添加listing项,匹配目标目录下的报告文件,系统会自动生成子菜单,新生成的报告只要符合命名规则就会自动加入导航:

    project:
      type: website
    
    website:
      title: "患者报告系统"
      nav:
        - text: "首页"
          href: index.qmd
        - text: "患者报告"
          menu:
            - listing:
                contents: reports/patient_*.html
                sort: alphabetical
                title: "按患者ID排序"
    

方法二:用脚本动态生成定制化导航条目

如果需要更个性化的导航文本(比如显示患者全名而非ID),可以通过脚本批量生成导航配置并更新_quarto.yml:

  1. 准备患者元数据
    创建包含患者ID和全名的CSV文件patients.csv:

    id,name
    A,张三
    B,李四
    C,王五
    
  2. 编写脚本生成导航代码
    示例Python脚本,读取患者信息并替换_quarto.yml中的导航占位符:

    import pandas as pd
    
    # 读取患者数据
    df = pd.read_csv("patients.csv")
    nav_entries = []
    for _, row in df.iterrows():
        nav_entries.append(f"  - text: '{row['name']}的报告'")
        nav_entries.append(f"    href: 'reports/patient_{row['id']}.html'")
    
    # 更新_quarto.yml中的导航部分
    with open("_quarto.yml", "r") as f:
        content = f.read()
    
    # 替换预先设置的占位符
    new_content = content.replace(
        "# PATIENT_REPORTS_NAV",
        "\n".join(nav_entries)
    )
    
    with open("_quarto.yml", "w") as f:
        f.write(new_content)
    
  3. 在_quarto.yml中预留占位

    website:
      nav:
        - text: "首页"
          href: index.qmd
        - text: "患者报告"
          menu:
            # PATIENT_REPORTS_NAV
    

内容的提问来源于stack exchange,提问作者TinyHeero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 18:32:22