moai-lang-r

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Quick Reference (30 seconds)

快速参考(30秒)

R 4.4+ Development Specialist - tidyverse, ggplot2, Shiny, renv, and modern R patterns.
Auto-Triggers: Files with .R extension, .Rmd, .qmd, DESCRIPTION, renv.lock, Shiny or ggplot2 discussions
Core Capabilities:
  • R 4.4 Features: Native pipe operator, lambda syntax with backslash, improved error messages
  • Data Manipulation: dplyr, tidyr, purrr, stringr, forcats
  • Visualization: ggplot2, plotly, scales, patchwork
  • Web Applications: Shiny, reactivity, modules, bslib
  • Testing: testthat 3.0, snapshot testing, mocking
  • Package Management: renv, pak, DESCRIPTION
  • Reproducible Reports: R Markdown, Quarto
  • Database: DBI, dbplyr, pool
R 4.4+开发专家 - 涵盖tidyverse、ggplot2、Shiny、renv及现代R开发模式。
自动触发条件:扩展名为.R、.Rmd、.qmd的文件,DESCRIPTION、renv.lock文件,以及涉及Shiny或ggplot2的讨论
核心能力:
  • R 4.4特性:原生管道操作符、反斜杠语法的lambda表达式、优化的错误提示
  • 数据处理:dplyr、tidyr、purrr、stringr、forcats
  • 可视化:ggplot2、plotly、scales、patchwork
  • Web应用:Shiny、响应式编程、模块化开发、bslib
  • 测试:testthat 3.0、快照测试、模拟测试
  • 包管理:renv、pak、DESCRIPTION
  • 可复现报告:R Markdown、Quarto
  • 数据库:DBI、dbplyr、pool

Quick Patterns

快速模式示例

dplyr Data Pipeline Pattern:
Load tidyverse library. Create result by piping data through filter for year 2020 or later, mutate adding revenue_k as revenue divided by 1000 and growth as current minus lagged revenue divided by lagged revenue, group_by category, then summarise with total_revenue as sum, avg_growth as mean with na.rm TRUE, and groups set to drop.
ggplot2 Visualization Pattern:
Load ggplot2 library. Create ggplot with data and aes mapping x to date, y to value, and color to category. Add geom_line with linewidth 1 and geom_point with size 2. Apply scale_color_viridis_d for color scale. Add labs for title, axis labels, and color legend. Apply theme_minimal for clean appearance.
Shiny Basic App Pattern:
Load shiny library. Create ui using fluidPage with selectInput for variable selection from mtcars column names and plotOutput for plot. Create server function with input, output, and session parameters. In server, assign renderPlot to output plot using ggplot with mtcars and aes using .data pronoun with input variable for histogram. Create app with shinyApp passing ui and server.

dplyr数据流水线模式:
加载tidyverse库。通过管道操作对数据进行过滤,筛选2020年及以后的数据,使用mutate添加revenue_k字段(将revenue除以1000)和growth字段(当前收入减去滞后收入再除以滞后收入),按category分组,然后使用summarise计算total_revenue(求和)、avg_growth(平均值,忽略NA值),并设置groups为drop以取消分组。
ggplot2可视化模式:
加载ggplot2库。基于数据创建ggplot对象,将x映射为date、y映射为value、color映射为category。添加geom_line(线宽为1)和geom_point(大小为2)。应用scale_color_viridis_d作为颜色刻度。添加labs设置标题、坐标轴标签和颜色图例。应用theme_minimal主题以获得简洁外观。
Shiny基础应用模式:
加载shiny库。使用fluidPage创建UI,包含用于选择mtcars列名的selectInput和用于显示图表的plotOutput。创建包含input、output、session参数的server函数。在server中,将renderPlot赋值给输出的plot,使用ggplot基于mtcars数据,通过.data代词引用input选择的变量绘制直方图。使用shinyApp传入ui和server创建应用。

Implementation Guide (5 minutes)

实现指南(5分钟)

R 4.4 Modern Features

R 4.4现代特性

Native Pipe Operator:
Create result by piping data through filter removing NA values, mutate adding log_value as log of value, and summarise computing mean_log. For non-first argument position, use underscore placeholder in lm formula call with data parameter.
Lambda Syntax with Backslash:
Use map with data and backslash x syntax for x squared. Use map2 with two lists and backslash x y for x plus y. In dplyr contexts, use mutate with across on numeric columns applying backslash x for scale function extracting first column.
原生管道操作符:
通过管道操作对数据进行过滤(移除NA值),使用mutate添加log_value字段(value的对数),然后使用summarise计算mean_log。对于非首个参数的位置,在lm公式调用中使用下划线占位符并指定data参数。
反斜杠语法的Lambda表达式:
使用map处理数据,结合反斜杠x语法计算x的平方。使用map2处理两个列表,结合反斜杠x y语法计算x加y。在dplyr上下文里,使用mutate结合across对数值列应用反斜杠x语法的scale函数,提取第一列结果。

tidyverse Data Manipulation

tidyverse数据处理

dplyr Core Verbs:
Load dplyr library. Create processed by piping raw_data through filter for active status and positive amount, select for specific columns, mutate adding month using floor_date and amount_scaled dividing by max, then arrange descending by date. For grouped summaries, pipe through group_by, summarise with n for count, sum and mean for aggregations, and groups drop.
tidyr Reshaping Pattern:
Load tidyr library. For wide to long transformation, use pivot_longer with cols starting with year prefix, names_to for column name, names_prefix to strip, and values_to for values. For long to wide transformation, use pivot_wider with names_from and values_from, adding values_fill for missing value handling.
purrr Functional Programming:
Load purrr library. Use map with files and lambda to read_csv each file. Use map_dfr for row-binding with id parameter for source column. Use map_dbl for extracting numeric results with mean and na.rm TRUE. For error handling, create safe_read using safely wrapper on read_csv. Map files through safe_read, extract results, and use compact to filter successes.
dplyr核心动词:
加载dplyr库。通过管道操作对raw_data进行过滤(筛选活跃状态和正金额的数据),选择特定列,使用mutate添加month字段(通过floor_date生成)和amount_scaled字段(除以最大值),然后按date降序排列。对于分组汇总,通过管道操作进行group_by,使用summarise计算n(计数)、sum和mean(聚合),并设置groups为drop取消分组。
tidyr数据重塑模式:
加载tidyr库。宽表转长表时,使用pivot_longer,指定cols为以year开头的列,names_to设置列名,names_prefix用于去除前缀,values_to设置值字段。长表转宽表时,使用pivot_wider,指定names_from和values_from,添加values_fill处理缺失值。
purrr函数式编程:
加载purrr库。使用map处理文件列表,结合lambda表达式读取每个csv文件。使用map_dfr进行行绑定,指定id参数添加来源列。使用map_dbl提取数值结果并计算平均值(忽略NA值)。对于错误处理,使用safely包装read_csv创建safe_read函数。使用map处理文件列表并调用safe_read,提取结果,使用compact过滤成功的结果。

ggplot2 Visualization Patterns

ggplot2可视化模式

Complete Plot Structure:
Load ggplot2 and scales libraries. Create p using ggplot with data and aesthetics for x, y, and color by group. Add geom_point with alpha and size, geom_smooth with lm method and standard error. Apply scale_x_continuous with comma labels, scale_y_log10 with dollar labels, and scale_color_brewer with Set2 palette. Add facet_wrap by category with free_y scales. Add labs for title, subtitle, and axis labels. Apply theme_minimal with base_size and theme for legend position. Save with ggsave specifying filename, plot, dimensions, and dpi.
Multiple Plots with patchwork:
Load patchwork library. Create p1 with histogram, p2 with scatter plot, and p3 with boxplot. Combine using pipe and parentheses for layout with p1 beside p2 over p3. Add plot_annotation for title and tag_levels.
完整图表结构:
加载ggplot2和scales库。基于数据创建ggplot对象p,将x、y、color映射为group。添加geom_point(设置alpha和size)和geom_smooth(使用lm方法并显示标准误差)。应用scale_x_continuous(使用逗号格式标签)、scale_y_log10(使用美元格式标签)和scale_color_brewer(使用Set2调色板)。添加facet_wrap按category分面,设置free_y刻度。添加labs设置标题、副标题和坐标轴标签。应用theme_minimal主题并设置base_size,调整theme中的图例位置。使用ggsave保存图表,指定文件名、plot对象、尺寸和dpi。
使用patchwork组合多图:
加载patchwork库。创建p1(直方图)、p2(散点图)和p3(箱线图)。通过管道和括号组合布局,将p1和p2并排,下方放置p3。添加plot_annotation设置标题和标签层级。

Shiny Application Patterns

Shiny应用模式

Modular Shiny App:
Create dataFilterUI function taking id parameter. Use NS function for namespace. Return tagList with selectInput for category with NULL initial choices and sliderInput for range. Create dataFilterServer function taking id and data reactive. Use moduleServer with inner function. In observe block, extract unique categories and updateSelectInput. Return reactive filtering data by category and range inputs using req for input validation.
Reactive Patterns:
In server function, create processed_data as reactive caching filtered data by input year. Create counter as reactiveVal initialized to 0. Use observeEvent on input increment to update counter. Create analysis as eventReactive on input run_analysis for expensive computation. Apply debounce with 300 milliseconds on search input reactive for rapid input handling.
模块化Shiny应用:
创建dataFilterUI函数,接收id参数。使用NS函数处理命名空间。返回tagList,包含用于选择category的selectInput(初始选项为NULL)和用于选择范围的sliderInput。创建dataFilterServer函数,接收id和data响应式对象。使用moduleServer包裹内部函数。在observe块中,提取唯一的category并更新selectInput。返回响应式对象,根据category和range输入过滤数据,使用req验证输入。
响应式模式:
在server函数中,创建processed_data作为响应式对象,缓存按input年份过滤的数据。创建counter作为reactiveVal,初始值为0。使用observeEvent监听input的increment事件以更新counter。创建analysis作为eventReactive,监听input的run_analysis事件以执行耗时计算。对搜索输入的响应式对象应用debounce(300毫秒)以处理快速输入。

testthat Testing Framework

testthat测试框架

Test Structure Pattern:
Load testthat library. Create test_that block for calculate_growth with tibble of years and values. Call function and store result. Use expect_equal for row count, expect_equal for growth value with tolerance, and expect_true for NA check.
测试结构模式:
加载testthat库。创建test_that块测试calculate_growth函数,传入包含years和values的tibble。调用函数并存储结果。使用expect_equal验证行数,使用expect_equal验证growth值(设置容差),使用expect_true检查NA值。

renv Dependency Management

renv依赖管理

Project Setup:
Call renv::init for initialization. Call renv::install for tidyverse and shiny packages. Call renv::snapshot to record state. Call renv::restore to restore from lockfile.

项目设置:
调用renv::init进行初始化。调用renv::install安装tidyverse和shiny包。调用renv::snapshot记录当前状态。调用renv::restore从锁文件恢复环境。

Advanced Implementation (10+ minutes)

高级实现(10分钟以上)

For comprehensive coverage including:
  • Advanced Shiny patterns for async, caching, and deployment
  • Complex ggplot2 extensions and custom themes
  • Database integration with dbplyr and pool
  • R package development patterns
  • Performance optimization techniques
  • Production deployment with Docker and Posit Connect
See:
  • modules/advanced-patterns.md for complete advanced patterns guide

如需了解以下全面内容:
  • 异步、缓存和部署相关的高级Shiny模式
  • 复杂ggplot2扩展和自定义主题
  • 基于dbplyr和pool的数据库集成
  • R包开发模式
  • 性能优化技巧
  • 基于Docker和Posit Connect的生产环境部署
请查看:
  • modules/advanced-patterns.md 获取完整的高级模式指南

Context7 Library Mappings

Context7库映射

  • tidyverse/dplyr for data manipulation verbs
  • tidyverse/ggplot2 for grammar of graphics visualization
  • tidyverse/purrr for functional programming toolkit
  • tidyverse/tidyr for data tidying functions
  • rstudio/shiny for web application framework
  • r-lib/testthat for unit testing framework
  • rstudio/renv for dependency management

  • tidyverse/dplyr:数据处理动词
  • tidyverse/ggplot2:图形语法可视化
  • tidyverse/purrr:函数式编程工具包
  • tidyverse/tidyr:数据整理函数
  • rstudio/shiny:Web应用框架
  • r-lib/testthat:单元测试框架
  • rstudio/renv:依赖管理工具

Works Well With

兼容工具

  • moai-lang-python for Python and R interoperability with reticulate
  • moai-domain-database for SQL patterns and database optimization
  • moai-workflow-testing for DDD and testing strategies
  • moai-essentials-debug for AI-powered debugging
  • moai-foundation-quality for TRUST 5 quality principles

  • moai-lang-python:通过reticulate实现Python与R的互操作
  • moai-domain-database:SQL模式和数据库优化
  • moai-workflow-testing:领域驱动设计(DDD)和测试策略
  • moai-essentials-debug:AI驱动的调试工具
  • moai-foundation-quality:TRUST 5质量原则

Troubleshooting

故障排除

Common Issues:
R Version Check:
Call R.version.string in R console for version 4.4 or later. Call packageVersion with package name to check installed package versions.
Native Pipe Not Working:
  • Ensure R version is 4.1 or later for native pipe operator
  • Check RStudio settings under Tools, Global Options, Code for Use native pipe option
renv Issues:
Call renv::clean to remove unused packages. Call renv::rebuild to rebuild package library. Call renv::snapshot with force TRUE to force snapshot update.
Shiny Reactivity Debug:
Set options shiny.reactlog to TRUE. Call reactlog::reactlog_enable to enable logging. Call shiny::reactlogShow to display reactive log visualization.
ggplot2 Font Issues:
Load showtext library. Call font_add_google with font name and family. Call showtext_auto to enable for all graphics devices.

Last Updated: 2026-01-11 Status: Active (v1.1.0)
常见问题:
R版本检查:
在R控制台中调用R.version.string检查版本是否为4.4或更高。调用packageVersion并传入包名检查已安装包的版本。
原生管道操作符无法使用:
  • 确保R版本为4.1或更高(原生管道操作符的最低要求)
  • 检查RStudio设置:依次进入Tools > Global Options > Code,确认“Use native pipe”选项已启用
renv相关问题:
调用renv::clean移除未使用的包。调用renv::rebuild重建包库。调用renv::snapshot并设置force=TRUE强制更新快照。
Shiny响应式调试:
设置options(shiny.reactlog = TRUE)。调用reactlog::reactlog_enable启用日志。调用shiny::reactlogShow显示响应式日志可视化。
ggplot2字体问题:
加载showtext库。调用font_add_google传入字体名称和family。调用showtext_auto为所有图形设备启用该字体。

最后更新时间:2026-01-11 状态:活跃(v1.1.0)