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

如何在R Markdown自动化报告封面从参数填充区域专属数据

问题解决:R Markdown报告封面专属区域数据填充错误

我是R语言新手,已实现R Markdown报告自动化流程,但封面无法正确填充对应区域的专属数据。期望封面格式如下:

REGION: Western
FACILITY (IES): 3
Total Number OF DOCTORS (S): 3
REPORTING PERIOD:
MONTH: January
YEAR:2021

当前运行代码后,封面显示全区域表格数据,而非对应区域的设施总数和医生总数。以下是修改后的解决方案:


修改后的R脚本

library(readxl)
library(dplyr)

dataa<- data.frame(region = c("Coast", "Coast", "Coast", "Eastern", "Eastern","Eastern","Nairobi","Nairobi", "Rift", "Nyanza", "Nyanza", "Nyanza",
                              "Rift","Rift", "Western","Western", "Western", "North Estern"),
                  doc_id = c(1:18),
                  pat_id = c(111:128),
                  mon = rep("January", 18),
                  category = c("treated", "untreated","treated", "untreated","treated", "untreated","treated", "untreated","treated", "untreated","treated", "untreated",
                                 "treated", "untreated", "treated", "untreated", "treated", "untreated"),
                  subregion= c("Lamu","Lamu", "Mombasa","Lawi","Meru", "Maua","Kibera","Ryosambu","Migori", "Migori", "Migori","Bomet", "Nakuru", "Nakuru","Bungoma","Bungoma","Kakamega ", "Njere"),
                  subcounty = c("Lamu1","Lamu1","Kijiji","Lala","Limbo","Limbos","Kibs","Boxa","Suna East", "Suna East","Suna East", "Tenwek","Mbola","Basi", "Busi","Kak","Kibs", "Ndira"),
                  facility = c("Raburo", "God Jope", "Mura", "Township Hospital", "Kita", "Kilwe","Kihoro", "Ladu", "Kacha","Naimo", "Central","Lato","Licha","Lichas","Nayobi", "Dala","Odhus","Nyuse"),
                  Age = c("20-24","Below 15","15-19", "20-24","Below 15", "15-19","20-24","Below 15","15-19", "20-24","Below 15", "15-19", "20-24","Below 15","15-19","20-24","Below 15","15-19"))


data_1<- dataa %>% select(doc_id, region, mon, category, Age, facility, subregion, subcounty) %>% mutate(month = mon) %>% 
  filter(!category %in% NA, month %in% "January") %>% group_by(region, category, Age)%>% arrange(category, Age) %>% summarise(Quantity = n())  


library(tinytex)
library(flextable)
library(kableExtra)
library(officedown)
library(knitr)
library(pander) 
library(htmlTable)
library(officedown)
library(officer)

# 统计各区域的设施数和医生数,修改列名避免混淆
facilities_by_region <- dataa %>% distinct(facility, .keep_all = T) %>% group_by(region) %>% summarise(facility_count = n())
doctors_by_region <- dataa %>% distinct(doc_id, .keep_all = T) %>% group_by(region) %>% summarise(doctor_count = n()) 
month<- "January"
year <- 2021

# 循环生成每个区域的报告
for (i in unique(data_1$region)) {
  # 筛选当前区域的设施数和医生数,传递单个数值
  current_facility_count <- facilities_by_region %>% filter(region == i) %>% pull(facility_count)
  current_doctor_count <- doctors_by_region %>% filter(region == i) %>% pull(doctor_count)
  
  rmarkdown::render("data1.Rmd", 
                    params = list(region = i, 
                                  doctor_count = current_doctor_count, 
                                  facility_count = current_facility_count, 
                                  month = month, 
                                  year = year),
                    output_file=paste0(i, ".docx"))
}

修改后的Rmd文件

---
title: "data query"
output:
  word_document: default
  pdf_document: default
date: "2022-10-29"
params:
  region: Nyanza
  facility_count: 0
  doctor_count: 0
  month: January
  year: 2021
show_code: no
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = params$show_code)
library(knitr)
library(tinytex)
library(blogdown)

# 提取参数
region <- params$region
facility_count <- params$facility_count
doctor_count <- params$doctor_count
month <- params$month
year <- params$year

REGION: r region
FACILITY(IES): r facility_count
Total Number OF DOCTORS (S): r doctor_count
REPORTING PERIOD:
MONTH:r month
YEAR:r year

data_1 %>% filter(region == params$region) %>% select(-region)%>% flextable(col_keys = c("category", "Age", "Quantity")) %>% theme_box()  %>% set_caption(" ") %>% width(width = 1.5) %>% height(height = .25) %>% hrule(rule = "exact", part = "all") %>% fontsize(size = 8, part = "body")
---

## 核心修改点
1. **R脚本**:
   - 重命名统计列名,避免参数混淆
   - 循环内部筛选当前区域的单个数值,而非传递整个数据框
   - 同步更新`render`函数的参数名,匹配Rmd定义

2. **Rmd文件**:
   - 将参数类型从数据框改为单个数值
   - 封面直接调用数值参数,不再输出完整表格
   - 表格部分增加区域筛选,确保仅显示当前区域数据(可选优化)

内容的提问来源于stack exchange,提问作者Musa Mosee
相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 01:01:01