Data Ethics & Best Practices
R Programming & Data Analytics / Data Ethics & Best Practices

Data Ethics & Best Practices

All Levels 5 hrs 2 Concepts
M1

Code Quality

Concept 1

Style Guide and lintr

Consistent style makes code maintainable. R uses tidyverse style: snake_case names, spaces around operators, 80-char line limit.

R
# 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)
Solved Examples
Example 1 Apply the concept of Style Guide and lintr to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. What is the primary purpose of style guide and lintr?
Q2. Which R package is most relevant for this topic?
M2

Documentation and Reproducibility

Concept 1

roxygen2 Function Documentation

Document functions with #' comments using roxygen2 syntax. Run devtools::document() to generate man pages.

R
#' 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'
  )
}
Solved Examples
Example 1 Apply the concept of roxygen2 Function Documentation to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. What is the primary purpose of roxygen2 function documentation?
Q2. Which R package is most relevant for this topic?
Shiny Web Applications Capstone Project