You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rules/r-cursorrules-prompt-file-best-practices/.cursorrules
+13-12Lines changed: 13 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,24 @@
1
1
You are an R programming assistant, make sure to use the best practices when programming in R:
2
2
3
3
## Project Structure and File Organization
4
-
- Organize projects into clear directories: 'R/' (scripts), 'data/' (raw and processed), 'output/' (results, plots), 'docs/' (reports, markdowns), 'inst/' for external files used within the project (.csv, .css and so on)
4
+
- Organize projects into clear directories: 'R/' (scripts), 'data/' (raw and processed), 'output/' (results, plots), 'docs/' (reports). For R packages, use 'inst/' for external files; for non-packages, consider 'assets/'.
5
5
- Use an 'Rproj' file for each project to manage working directories and settings.
6
6
- Create reusable functions and keep them in separate script files under the 'R/' folder.
7
-
- Use RMarkdown or Quarto for reports and documentation. Prefer Quarto is available and already installed.
7
+
- Use RMarkdown or Quarto for reproducible reports combining code and results. Prefer Quarto if available and installed.
8
8
- Keep raw data immutable; only work with processed data in 'data/processed/'.
9
9
- Use 'renv' for dependency management and reproducibility. All the dependencies must be installed, synchronized, and locked.
10
10
- Version control all projects with Git and use clear commit messages.
11
11
- Give a snake_case consistent naming for the file names. The file names should not be too long.
12
-
- Avoid using unncessary dependencies, if a task can be achieved relatively easily using base R, just use base R and import other packages only if necessary. The imported package should for example be faster in terms of execution, more robust and can achieve the same tasks with fewer lines of code. Otherwise, just use base R.
12
+
- Avoid using unnecessary dependencies. If a task can be achieved relatively easily using base R, use base R and import other packages only when necessary (e.g., measurably faster, more robust, or fewer lines of code).
13
13
14
-
## Package structure
14
+
## Package Structure
15
15
- If the R project is an R package, make sure to mention the dependencies used inside the package within the 'DESCRIPTION' file. All dependencies must have their version number mentioned (e.g: R6 (>= 2.6.1))
16
16
- If the R project is an R package, make sure a 'LICENSE' file is available.
17
17
- If the R project is an R package, make sure a 'NEWS.md' file is available which should track the package's development changes.
18
18
- If the R project is an R package, make sure that each external file used inside the package is saved within the 'inst' folder. Reading the file should be done using the 'system.file' function.
19
19
- If the R project is an R package, Always use 'devtools::load_all' before testing the new functions.
20
-
- If the R project is an R package, make sure to always document the functions using 'roxygen' code. Use 'devtools::document' to create the corresponding and necessary documentation (.Rd files and NAMESPACE) file.
21
-
- If the R project is an R package, run 'devtools::check' to check if the packages has no issues. Notes are okay but warnings and errors should be avoided.
20
+
- If the R project is an R package, run 'devtools::check()' to ensure the package has no issues. Notes are okay; avoid warnings and errors.
21
+
- If the R project is an R package, document functions using roxygen2. Use 'devtools::document()' to generate the required documentation (.Rd files) and 'NAMESPACE' file.
22
22
23
23
## Naming Conventions
24
24
- snake_case: variables and functions (e.g., \`total_sales\`, \`clean_data()\`).
@@ -34,20 +34,21 @@ You are an R programming assistant, make sure to use the best practices when pro
34
34
- Use spaces around operators (\`a + b\`, not \`a+b\`).
35
35
- Keep line length <= 80 characters for readability.
36
36
- Use consistent indentation (2 spaces preferred).
37
-
- Use '#' for inline comments and section headers. Only comment if necessary (if a code is complex and need explanation), otherwise avoid commenting. The code should be selfexplanatory.
37
+
- Use '#' for inline comments and section headers. Comment only when necessary (e.g., complex code needing explanation). The code should be self‑explanatory.
38
38
- Write modular, reusable functions instead of long scripts.
39
39
- Prefer vectorized operations over loops for performance.
- When creating an empty element that will get values assigned in it, try to preallocate the type and memory in advance if possible, for example 'x <- character(length = 100)' instead of 'x <- c( )'.
41
+
- When creating an empty object to be filled later, preallocate type and length when possible (e.g., 'x <- character(length = 100)' instead of 'x <- c()').
42
42
- Always use <- for variables' assignment, except when working with 'R6' classes. The methods inside the 'R6' classes are assigned using '='
43
43
- When referencing a function from a package always use the '::' syntax, for example 'dplyr::select'
44
44
- Always use 'glue::glue' for string interpolation instead of 'paste0' or 'paste'
45
45
46
46
## Performance and Optimization
47
47
- Profile code with \`profvis\` to identify bottlenecks.
48
-
- Prefer vectorized functions and apply family (\`lapply\`, \`sapply\`, \`purrr\`) over explicit loops. When using loop, try to preallocate type and memory beforehands.
48
+
- Prefer vectorized functions and the apply family ('apply', 'lapply', 'sapply', 'vapply', 'mapply', 'tapply') or 'purrr' over explicit loops. When using loops, preallocate type and memory beforehand.
49
49
- Use data.table for large datasets when performance is critical and data can fit in memory.
50
-
- When reading a csv file, always prefer using the 'fread::read_csv' or 'readr::read_csv' depending on the codebase. If the codebase is 'tidyverse' oriented (it contains packages that are part of the tidyverse), prefer 'readr', use 'data.table' otherwise.
50
+
- When reading a CSV, prefer 'data.table::fread' or 'readr::read_csv' depending on the codebase. If the codebase is tidyverse‑oriented, prefer 'readr'; otherwise use 'data.table'.
51
+
51
52
- Use duckdb when data is out of memory.
52
53
- Avoid copying large objects unnecessarily; use references when possible.
53
54
@@ -88,11 +89,11 @@ You are an R programming assistant, make sure to use the best practices when pro
88
89
- Use CI/CD (GitHub Actions, GitLab CI) to test and deploy R projects.
89
90
90
91
## Dependencies
91
-
Have a preference for the following package when relying on a dependency:
92
+
Have a preference for the following packages when relying on dependencies:
92
93
- purrr for 'list' objects manipulation and functional programming
93
94
- shiny for web application development
94
95
- 'data.table' or 'dplyr' for in-memory data manipulation
95
-
- 'data.table' or 'dplyr' for in-memory data injection.
96
+
- 'data.table' or 'dplyr' for efficient data import (CSV/TSV, etc.).
96
97
- 'arrow' when dealing with 'parquet' files
97
98
- 'duckdb' when dealing with out of memory data sets.
0 commit comments