# Read in data for current and previous year
<- readr::read_csv(
schools ::here("data", "2023-03-31_schools.csv")
here
)
library(readr)
<- read_csv(
schools_prev ::here("data", "2022-03-31_schools.csv")
here )
Scottish Government R User Day
Alice Byers
Data Division, Scottish Government
6 December 2023
Opinion based on experience
One size doesn’t fit all
Keeps all related files together in one folder
Sets working directory so file paths can be relative to project
Facilitates use of RStudio Git integration
In RStudio, go to File -> New Project…
Create a new project or add a project to an existing directory
Creating an RStudio Project adds an .Rproj
file to your directory
Open this file to open your project in RStudio
Working directory automatically set to location of my-project.Rproj
The ‘Files’ pane in RStudio will show your project directory
Where possible, store all files related to your project within your RStudio Project directory
Absolute file paths are difficult to manage and are likely to break if you move your project to a different folder or ask somebody else to run your code.
Use the here package to write file paths relative to your project directory.
Loading all packages in one place makes it easy to see what is required to run the code
Defining all variables / parameters in one place reduces the number of places in the code you need to edit to make a change
Makes code easier to write
Makes code easier to understand
Makes code easier to debug
Naming files
Naming objects
Spacing
Using pipes (%>%
)
and lots more
lintr runs style checks on existing code and reports back any issues (but doesn’t change any of your code).
[infix_spaces_linter] Put spaces around all infix operators.
[commas_linter] Commas should always have a space after.
[infix_spaces_linter] Put spaces around all infix operators.
[commas_linter] Commas should always have a space after.
[infix_spaces_linter] Put spaces around all infix operators.
[line_length_linter] Lines should not be more than 80 characters.
styler re-styles existing code following the tidyverse style guide
Help somebody else understand the code you’ve written
Help future you understand the code you’ve written
Provide context
Section your code to provide structure and easier navigation
Start a comment line with a single #
followed by a space
Help other people (and yourself in the future) understand how and why the code has been written in a particular way
Repeating what is already obvious from the code
Hard coded values
Commenting out code to save for later or to be conditionally run
A comment line followed by at least four dashes (-), equal signs (=), or hash signs (#)
Minimises repetition
Simplifies code
Ensures consistent methods across code
Easier to maintain
code/01_read-data.R
# Run setup script ----
source(here::here("code", "00_setup.R"))
# Source functions ----
source(here::here("functions", "fin_year.R"))
# Read in data for current and previous year ----
schools <- read_csv(
here("data", paste0(fin_year(month_end), "_schools.csv"))
)
schools_prev <- read_csv(
here("data", paste0(fin_year(prev_year), "_schools.csv"))
)
RStudio Projects
Use a setup script
Code style
Code comments
Functions
Email: alice.hannah@gov.scot
GitHub: alice-hannah