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

SQLite中如何处理单个ID需匹配多个ID的问题?

针对Shiny研究参与者追踪的CSV单表处理方案

作为数据库新手,用CSV单表搭建这个追踪系统完全可行,我给你梳理几个关键步骤和代码思路,帮你实现避免同时参与多项研究和检查研究间隔达标这两个核心需求:

一、先规范你的CSV单表结构

你给出的示例格式有点混乱,建议给CSV加上明确的表头,并且每条记录对应一个参与者的一次研究参与(如果同一个人在同一个研究里做多个任务,可以拆成单独行,或者把任务用逗号分隔)。规范后的参考结构如下:

study_titlecontact_persontasksfirst_namelast_namestudy_start_datestudy_end_date
MX9345-3John DoeOGTTMichaelSmith2024-01-152024-01-15
MX9345-3John DoePVTMichaelSmith2024-01-152024-01-15
AB1234-1Jane SmithMRIEmilyJohnson2024-02-202024-02-20

重点提示:一定要加上研究起止日期,这是判断间隔时长的核心依据;另外用first_name+last_name作为参与者的临时唯一标识(如果能拿到参与者ID/身份证号会更准确,避免重名问题)。

二、Shiny应用中的核心检查逻辑

1. 检测是否同时参与多项研究

我们可以通过参与者姓名(或ID)分组,判断是否存在重叠的研究时间段:

# 加载必要工具包
library(dplyr)
library(lubridate)
library(shiny)

# 读取CSV数据
participant_data <- read.csv("study_participants.csv", stringsAsFactors = FALSE)
# 转换日期格式为可计算的格式
participant_data <- participant_data %>%
  mutate(study_start_date = ymd(study_start_date),
         study_end_date = ymd(study_end_date))

# 定义检查重叠研究的函数
check_overlapping_studies <- function(data) {
  data %>%
    group_by(first_name, last_name) %>%
    arrange(study_start_date) %>%
    # 判断当前研究时间段是否和上一次研究重叠
    mutate(has_overlap = any(interval(study_start_date, study_end_date) %within% interval(lag(study_start_date), lag(study_end_date)))) %>%
    filter(has_overlap == TRUE) %>%
    ungroup()
}

# 获取存在重叠问题的记录
overlapping_records <- check_overlapping_studies(participant_data)

在Shiny的UI部分,你可以用DT::dataTableOutput把这些重叠记录展示出来,方便管理员快速定位问题。

2. 检测研究间隔是否达标

假设我们要求两次研究的间隔至少30天,可以用下面的逻辑计算同一参与者两次研究的间隔天数:

# 定义检查间隔时长的函数
check_interval <- function(data, min_required_days = 30) {
  data %>%
    group_by(first_name, last_name) %>%
    arrange(study_start_date) %>%
    # 计算和上一次研究的间隔天数
    mutate(days_since_last_study = as.numeric(difftime(study_start_date, lag(study_start_date), units = "days"))) %>%
    # 筛选出间隔不达标的记录
    filter(days_since_last_study < min_required_days & !is.na(days_since_last_study)) %>%
    ungroup()
}

# 获取间隔不达标的记录
short_interval_records <- check_interval(participant_data)

同样,你可以在Shiny界面里把这些记录高亮展示,或者在用户录入新数据时实时触发检查,给出即时提示。

三、Shiny实时录入检查的实用技巧

在用户添加新参与者记录时,可以提前拦截问题,避免后续清理数据的麻烦:

server <- function(input, output, session) {
  # 监听提交按钮,实时验证新记录
  observeEvent(input$submit_new_record, {
    # 整理用户输入的新记录
    new_record <- tibble(
      study_title = input$study_title,
      contact_person = input$contact_person,
      tasks = input$tasks,
      first_name = input$first_name,
      last_name = input$last_name,
      study_start_date = ymd(input$start_date),
      study_end_date = ymd(input$end_date)
    )
    
    # 查询该参与者的已有记录
    existing_records <- participant_data %>%
      filter(first_name == new_record$first_name, last_name == new_record$last_name)
    
    if(nrow(existing_records) > 0) {
      # 检查是否有研究时间段重叠
      overlap_check <- any(interval(new_record$study_start_date, new_record$study_end_date) %within% interval(existing_records$study_start_date, existing_records$study_end_date))
      
      if(overlap_check) {
        showModal(modalDialog(
          title = "⚠️ 警告",
          "该参与者当前正在参与其他研究,无法添加此记录!",
          easyClose = TRUE
        ))
        return()
      }
      
      # 检查间隔是否达标
      last_study_end <- max(existing_records$study_end_date)
      interval_days <- as.numeric(difftime(new_record$study_start_date, last_study_end, units = "days"))
      if(interval_days < 30) {
        showModal(modalDialog(
          title = "⚠️ 警告",
          paste("该参与者距离上次研究仅", interval_days, "天,未达30天间隔要求!"),
          easyClose = TRUE
        ))
        return()
      }
    }
    
    # 所有检查通过,添加记录到CSV
    participant_data <<- bind_rows(participant_data, new_record)
    write.csv(participant_data, "study_participants.csv", row.names = FALSE)
    showModal(modalDialog(
      title = "✅ 成功",
      "记录已成功添加!",
      easyClose = TRUE
    ))
  })
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:38:53