К основному содержанию
правило официальный · GitHub

r — правило для GitHub Copilot: стандарты кода на R

Задаёт Copilot правила генерации кода на R: стиль проекта, безопасность, работа с путями и данными. Пригодится, если в репозитории есть .R, .Rmd или .qmd файлы.

Установить в GitHub Copilot · Вручную: положить файлы Все способы и платформы ↓
.github/instructions/r.instructions.md
Открыть на GitHub

Что делает r

Правило описывает, каким Copilot должен видеть код на R. Сначала он смотрит на стиль файла: если в проекте уже используется tidyverse, подсказки идут в том же ключе, если base R — в base-стиле. Смешивать пайпы |> и %>% в одном скрипте правило не разрешает без технической причины.

Внутри — набор конвенций по неймингу (lower_snake_case, без точек в именах), по воспроизводимости (сиды через withr::with_seed()), по безопасности (никаких eval(parse()), непараметризованного SQL и незахардкоженных секретов) и по инструментам: styler, lintr, renv, roxygen2. Отдельные разделы — про Shiny, R Markdown и Quarto, обработку ошибок через rlang или tryCatch(), производительность с data.table, profvis и memoise.

Правило советует не вызывать setwd(), а строить пути от корня проекта. Это ломает привычку у скриптов, которые полагаются на смену рабочей директории, поэтому проверьте такие места перед тем, как включать правило в существующий проект. Файл подключается по маске applyTo, так что действует только на файлы R, Rmd и Quarto.

Какие задачи решает

  • держать единый стиль кода на R в команде
  • не дать Copilot смешать base R и tidyverse в одном файле
  • убрать хардкод секретов и непараметризованный SQL
  • сделать расчёты воспроизводимыми через локальные сиды
  • подсказать Copilot правильные пакеты для I/O и работы с путями

Примеры запросов агенту

  • допиши функцию для чтения CSV через readr с явными типами колонок
  • перепиши этот цикл на vapply, файл в base-стиле
  • добавь roxygen2-документацию к этой функции

Как установить r

Вручную: положить файлы

.github/instructions/r.instructions.md

Поле applyTo задаёт, к каким файлам применять

По шагам, с командами для каждой платформы: AGENTS.md и CLAUDE.md: что это, кто читает и какой выбрать

Вопросы о r

Как установить правило r в GitHub Copilot?

Создайте файл .github/instructions/r.instructions.md в репозитории и вставьте в него содержимое правила. Дальше Copilot подхватит его для файлов по маске applyTo.

Что делает правило r?

Оно направляет Copilot писать идиоматичный и безопасный код на R: следовать стилю файла, квалифицировать не-base функции, избегать опасных вызовов и держать пути относительными корня проекта.

На какие файлы распространяется правило?

Маска applyTo покрывает **/*.R, **/*.r, **/*.Rmd, **/*.rmd и **/*.qmd. На скрипты других языков оно не влияет.

Исходный текст

r.instructions.md · MIT
---
description: 'R language and document formats (R, Rmd, Quarto): coding standards and Copilot guidance for idiomatic, safe, and consistent code generation.'
applyTo: '**/*.R, **/*.r, **/*.Rmd, **/*.rmd, **/*.qmd'
---

# R Programming Language Instructions

## Purpose

Help GitHub Copilot generate idiomatic, safe, and maintainable R code across projects.

## Core Conventions

- **Match the project’s style.** If the file shows a preference (tidyverse vs. base R, `%>%` vs. `|>`), follow it.
- **Prefer clear, vectorized code.** Keep functions small and avoid hidden side effects.
- **Qualify non-base functions in examples/snippets**, e.g., `dplyr::mutate()`, `stringr::str_detect()`. In project code, using `library()` is acceptable when that’s the repo norm.
- **Naming:** `lower_snake_case` for objects/files; avoid dots in names.
- **Side effects:** Never call `setwd()`; prefer project-relative paths (e.g., `here::here()`).
- **Reproducibility:** Set seeds locally around stochastic operations using `withr::with_seed()`.
- **Validation:** Validate and constrain user inputs; use typed checks and allowlists where possible.
- **Safety:** Avoid `eval(parse())`, unvalidated shell calls, and unparameterized SQL.

### Pipe Operators

- **Native pipe `|>` (R ≥ 4.1.0):** Prefer in R ≥ 4.1 (no extra dependency).
- **Magrittr pipe `%>%`:** Continue using in projects already committed to magrittr or when you need features like `.`, `%T>%`, or `%$%`.
- **Be consistent:** Don't mix `|>` and `%>%` within the same script unless there's a clear technical reason.

## Performance Considerations

- **Large datasets:** consider `data.table`; benchmark with your workload.
- **dplyr compatibility:** Use `dtplyr` to write dplyr syntax that translates to data.table operations automatically for performance gains.
- **Profiling:** Use `profvis::profvis()` to identify performance bottlenecks in your code. Profile before optimizing.
- **Caching:** Use `memoise::memoise()` to cache expensive function results. Particularly useful for repeated API calls or complex computations.
- **Vectorization:** Prefer vectorized operations over loops. Use `purrr::map_*()` family or `apply()` family for remaining iteration needs.

## Tooling & Quality

- **Formatting:** `styler` (tidyverse style), two-space indents, ~100-char lines.
- **Linting:** `lintr` configured via `.lintr`.
- **Pre-commit:** consider `precommit` hooks to lint/format automatically.
- **Docs:** roxygen2 for exported functions (`@param`, `@return`, `@examples`).
- **Tests:** prefer small, pure, composable functions that are easy to unit test.
- **Dependencies:** manage with `renv`; snapshot after adding packages.
- **Paths:** prefer `fs` and `here` for portability.

## Data Wrangling & I/O

- **Data frames:** prefer tibbles in tidyverse-heavy files; otherwise base `data.frame()` is fine.
- **Iteration:** use `purrr` in tidyverse code. In base-style code, prefer type-stable, vectorized patterns such as `vapply()`
   (for atomic outputs) or `Map()` (for elementwise operations) instead of explicit `for` loops when they improve clarity or performance.
- **Strings & Dates:** use `stringr`/`lubridate` where already present; otherwise use clear base helpers (e.g., `nchar()`, `substr()`, `as.Date()` with explicit format).
- **I/O:** prefer explicit, typed readers (e.g., `readr::read_csv()`); make parsing assumptions explicit.

## Plotting

- Prefer `ggplot2` for publication-quality plots. Keep layers readable and label axes and units.

## Error Handling

- In tidyverse contexts, use `rlang::abort()` / `rlang::warn()` for structured conditions; in base-only code, use `stop()` / `warning()`.
- For recoverable operations:
- Use `purrr::possibly()` when you want a typed fallback value of the same type (simpler).
- Use `purrr::safely()` when you need to capture both results and errors for later inspection or logging.
- Use `tryCatch()` in base R for fine-grained control or compatibility with non-tidyverse code.
- Prefer consistent return structures—typed outputs for normal flows, structured lists only when error details are required.

## Security Best Practices

- **Command execution:** Prefer `processx::run()` or `sys::exec_wait()` over `system()`; validate and sanitize all arguments.
- **Database queries:** Use parameterized `DBI` queries to prevent SQL injection.
- **File paths:** Normalize and sanitize user-provided paths (e.g., `fs::path_sanitize()`), and validate against allowlists.
- **Credentials:** Never hardcode secrets. Use env vars (`Sys.getenv()`), config outside VCS, or `keyring`.

## Shiny

- Modularize UI and server logic for non-trivial apps. Use `eventReactive()` / `observeEvent()` for explicit dependencies.
- Validate inputs with `req()` and clear, user-friendly messages.
- Use connection pooling (`pool`) for databases; avoid long-lived global objects.
- Isolate expensive computations and prefer `reactiveVal()` / `reactiveValues()` for small state.

## R Markdown / Quarto

- Keep chunks focused; prefer explicit chunk options (`echo`, `message`, `warning`).
- Avoid global state; prefer local helpers. Use `withr::with_seed()` for deterministic chunks.

## Copilot-Specific Guidance

- If the current file uses tidyverse, **suggest tidyverse-first patterns** (e.g., `dplyr::across()` instead of superseded verbs). If base-R style is present, **use base idioms**.
- Qualify non-base calls in suggestions (e.g., `dplyr::mutate()`).
- Suggest vectorized or tidy solutions over loops when idiomatic.
- Prefer small helper functions over long pipelines.
- When multiple approaches are equivalent, prefer readability and type stability and explain the trade-offs.

---

## Minimal Examples

```r
# Base R variant
scores <- data.frame(id = 1:5, x = c(1, 3, 2, 5, 4))
safe_log <- function(x) tryCatch(log(x), error = function(e) NA_real_)
scores$z <- vapply(scores$x, safe_log, numeric(1))

# Tidyverse variant (if this file uses tidyverse)
result <- tibble::tibble(id = 1:5, x = c(1, 3, 2, 5, 4)) |>
dplyr::mutate(z = purrr::map_dbl(x, purrr::possibly(log, otherwise = NA_real_))) |>
dplyr::filter(z > 0)

# Example reusable helper with roxygen2 doc
#' Compute the z-score of a numeric vector
#' @param x A numeric vector
#' @return Numeric vector of z-scores
#' @examples z_score(c(1, 2, 3))
z_score <- function(x) (x - mean(x, na.rm = TRUE)) / stats::sd(x, na.rm = TRUE)
```

© github · MIT · github/awesome-copilot

Похожие правила IDE

Все правила IDE по теме «Языки программирования» →

Из того же набора732

Весь набор →
swift-mcp-server Правило задаёт Copilot шаблоны для сборки MCP-серверов на Swift через официальный SDK: инструменты, ресурсы, промпты и транспорты. ★ 40 418звёзд репо shell Правило, которое заставляет GitHub Copilot писать чистые и безопасные shell-скрипты. Применяется к файлам *.sh автоматически. ★ 40 418звёзд репо scala2 Правило задаёт Copilot стиль Scala по гайду Databricks: неизменяемость, чистые функции, именование. Пригодится, если пишете на Scala 2.12 или 2.13. ★ 40 418звёзд репо rust-mcp-server Правило задаёт Copilot ориентиры при написании MCP-серверов на Rust через официальный SDK rmcp. Пригодится, когда проект на Rust и нужен свой MCP-сервер. ★ 40 418звёзд репо qa-engineering-best-practices Правило для GitHub Copilot с набором инструкций по тестированию: пирамида тестов, именование, ассерты, баг-репорты. Пригодится, когда Copilot пишет тесты в вашем проекте. ★ 40 418звёзд репо powershell Правило для GitHub Copilot, которое заставляет его писать идиоматичные скрипты PowerShell по гайдлайнам Microsoft. Пригодится, если в проекте есть .ps1 и .psm1. ★ 40 418звёзд репо power-platform-connector Правило для GitHub Copilot, которое подсказывает, как писать JSON-схемы кастомных коннекторов Power Platform: Swagger 2.0, apiProperties и settings. ★ 40 418звёзд репо power-bi-devops-alm-best-practices Правило для GitHub Copilot: подсказывает агенту, как вести Power BI-проект в Git, собирать CI/CD и раскатывать отчёты по средам. ★ 40 418звёзд репо

Спонсоры проекта

Компании, которые помогают держать аналитику открытой