Data Ethics & Best Practices
Code Quality
Style Guide and lintr
Consistent style makes code maintainable. R uses tidyverse style: snake_case names, spaces around operators, 80-char line limit.
# styler auto-formats your code
library(styler); style_file('analysis.R')
# lintr checks style violations
library(lintr); lint('analysis.R')
# Good style:
mean_score <- mean(scores, na.rm = TRUE)
# Bad style:
meanScore<-mean(scores,na.rm=TRUE)
# See the code example above and adapt it to your data. # Always check your output with str() and head().
Documentation and Reproducibility
roxygen2 Function Documentation
Document functions with #' comments using roxygen2 syntax. Run devtools::document() to generate man pages.
#' Compute student grade
#' @param score Numeric score (0-100)
#' @return Character: 'A','B','C', or 'F'
#' @examples
#' get_grade(85) # 'B'
get_grade <- function(score){
dplyr::case_when(
score >= 90 ~ 'A',
score >= 80 ~ 'B',
score >= 65 ~ 'C',
TRUE ~ 'F'
)
}
# See the code example above and adapt it to your data. # Always check your output with str() and head().