9  医学研究报告生成

代码
# 设置CRAN镜像为中国合肥镜像
options(repos = c(CRAN = "https://mirrors.ustc.edu.cn/CRAN/"))

# 检查并安装所需的R包
required_packages <- c(
  "tidyverse",     # 数据处理和可视化
  "rmarkdown",     # 报告生成
  "knitr",         # 文档编织
  "gt",            # 表格生成
  "gtsummary",     # 统计表格
  "medicaldata",   # 医学数据集
  "flextable",     # 表格格式化
  "plotly",        # 交互式图形
  "DT"             # 交互式表格
)

# 检查并安装缺失的包
for(pkg in required_packages) {
  if(!require(pkg, character.only = TRUE)) {
    install.packages(pkg)
    library(pkg, character.only = TRUE)
  }
}

9.0.1 结果自动更新机制

代码
# 创建模拟患者数据
set.seed(123)
n <- 200
patient_data <- data.frame(
  age = rnorm(n, mean = 45, sd = 15),
  gender = factor(sample(c("男", "女"), n, replace = TRUE)),
  temp = rnorm(n, mean = 37, sd = 0.5),
  group = factor(sample(c("阳性", "阴性"), n, replace = TRUE, prob = c(0.3, 0.7)))
)

# 创建基线特征表
baseline_table <- patient_data %>%
  select(age, gender, temp, group) %>%
  tbl_summary(
    by = group,
    missing = "no",
    label = list(
      age ~ "年龄(岁)",
      gender ~ "性别",
      temp ~ "体温(℃)"
    ),
    statistic = list(
      all_continuous() ~ "{mean} ({sd})",
      all_categorical() ~ "{n} ({p}%)"
    )
  ) %>%
  add_p() %>%
  add_n() %>%
  modify_header(label = "**特征**") %>%
  modify_spanning_header(c("stat_1", "stat_2") ~ "新冠检测结果")

# 展示表格
baseline_table
特征 N
新冠检测结果
p-value
阳性
N = 561
阴性
N = 1441
年龄(岁) 200 45 (13) 45 (14)
性别 200


    男
25 (45%) 68 (47%)
    女
31 (55%) 76 (53%)
体温(℃) 200 37.05 (0.51) 37.00 (0.51)
1 Mean (SD); n (%)

9.1 Quarto高级功能

9.1.1 交互式可视化报告

代码
library(plotly)
library(DT)

# 创建交互式散点图
p <- ggplot(patient_data, aes(x = age, y = temp, color = group)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm") +
  labs(title = "年龄与体温的关系",
       x = "年龄 (岁)",
       y = "体温 (°C)")

interactive_plot <- ggplotly(p)

# 创建交互式表格
dt <- datatable(
  patient_data %>%
    select(age, gender, temp, group),
  options = list(
    pageLength = 10,
    searchHighlight = TRUE
  ),
  caption = "患者详细数据"
)

# 展示交互式内容
interactive_plot
代码
dt

9.1.2 多格式输出(Word/PDF/HTML)

代码
# Word格式输出设置
word_output <- function(data) {
  ft <- flextable(data)
  ft <- autofit(ft)
  ft <- theme_booktabs(ft)
  return(ft)
}

# PDF格式输出设置
pdf_output <- function(data) {
  kable(data, format = "latex", booktabs = TRUE)
}

# HTML格式输出设置
html_output <- function(data) {
  gt(data) %>%
    tab_header(title = "研究结果") %>%
    tab_options(table.width = pct(100))
}

9.2 论文可重复性实践

9.2.1 代码版本控制

代码
# 项目初始化
renv::init()

# 记录包依赖
renv::snapshot()

# Git版本控制
# git init
# git add .
# git commit -m "Initial analysis setup"

9.2.2 数据脱敏处理

代码
# 创建数据脱敏函数
anonymize_data <- function(data) {
  data %>%
    mutate(
      id = paste0("P", sprintf("%04d", row_number())),
      age_group = cut(age, 
                     breaks = c(0, 40, 60, Inf),
                     labels = c("青年", "中年", "老年")),
      gender = factor(gender)
    ) %>%
    select(-matches("name|address|phone|email"))
}

# 示例:处理敏感数据
secure_data <- anonymize_data(patient_data)
研究报告最佳实践
  1. 使用标准化的报告模板
  2. 确保结果可重复性
  3. 妥善处理敏感数据
  4. 使用版本控制管理代码
  5. 保持分析流程的透明性

9.3 本章小结

在本章中,我们学习了:

  1. 如何使用Quarto创建专业的医学研究报告
  2. 自动化报告生成和结果更新机制
  3. 交互式可视化技术
  4. 确保研究可重复性的方法

下一章,我们将学习生物信息学分析的基础知识。