install.packages("readr")library(readr)data <-read_csv("LOS.csv")### REST OF SCRIPT UNCHANGED
Example 2: Organising a script
Restructuring scripts
Adding sections
Renaming variables
Namespacing
See example
messy_example_script.R
# Load packages -----------------------------------------------------------library(gtsummary)library(ggcorrplot)library(tidyverse)library(forcats)# Load LOS ---------------------------------------------------------------LOS <-read_csv("C:/Users/username/OneDrive/R code/LOS.csv")LOS <- LOS |>mutate(Organisation =factor(Organisation), Organisation =fct_relevel(Organisation, "Trust10", after =Inf))# Exploratory plots -------------------------------------------------------ggplot(LOS) +geom_histogram(aes(Age))ggplot(LOS) +geom_histogram(aes(LOS))ggplot(LOS) +geom_histogram(aes(Age)) +facet_wrap(~Organisation)# Summary statistics ------------------------------------------------------mean(LOS$Age) mean(LOS$LOS) mean(LOS$Death)sd(LOS$Age)table(LOS$organisation)# Statistical tests -------------------------------------------------------age1 <- LOS |>filter(Age <=50)age2 <- LOS |>filter(Age >50)t.test(age1$LOS, age2$LOS, var.equal = T)t.test(age1$LOS, age2$LOS, var.equal = T)$p.value# Modelling ---------------------------------------------------------------mod1 <-glm(Death~Age, data = LOS, family ="binomial")summary(mod1)mod2 <-glm(Death~Age+LOS, data=LOS, family ="binomial")summary(mod2)mod3 <-glm(Death~Age+LOS+Organisation, data = LOS, family="binomial")summary(mod3)mod4 <-glm(Death~LOS, data = LOS, family ="binomial")summary(mod4)# Results -----------------------------------------------------------------modelTable =tbl_regression(mod3)modelTable |>as_gt() |>gtsave("table.docx")
Example 3: Styling scripts
Linting
Styling
See example
messy_example_script.R
# Load packages -----------------------------------------------------------library(gtsummary)library(ggcorrplot)library(tidyverse)library(forcats)# Load LOS ---------------------------------------------------------------LOS <-read_csv("LOS.csv") # nolintLOS <- LOS |># nolintmutate(Organisation =factor(Organisation),Organisation =fct_relevel(Organisation, "Trust10", after =Inf) )# Exploratory plots -------------------------------------------------------ggplot(LOS) +geom_histogram(aes(Age))ggplot(LOS) +geom_histogram(aes(LOS))ggplot(LOS) +geom_histogram(aes(Age)) +facet_wrap(~Organisation)# Summary statistics ------------------------------------------------------mean(LOS$Age)sd(LOS$Age)mean(LOS$LOS)mean(LOS$Death)table(LOS$organisation)# Statistical tests -------------------------------------------------------age1 <- LOS |>filter(Age <=50)age2 <- LOS |>filter(Age >50)t.test(age1$LOS, age2$LOS, var.equal =TRUE)t.test(age1$LOS, age2$LOS, var.equal =TRUE)$p.value# Modelling ---------------------------------------------------------------mod1 <-glm(Death ~ Age, data = LOS, family ="binomial")summary(mod1)mod2 <-glm(Death ~ Age + LOS, data = LOS, family ="binomial")summary(mod2)mod3 <-glm(Death ~ Age + LOS + Organisation, data = LOS, family ="binomial")summary(mod3)mod4 <-glm(Death ~ LOS, data = LOS, family ="binomial")summary(mod4)# Results -----------------------------------------------------------------model_table <-tbl_regression(mod3)model_table |>as_gt() |>gtsave("regression_table.docx")
Sometimes you’ll see me write e.g. dplyr::filter() rather than just filter(). It means use the filter() function from the dplyr package, and saves you having to run library(dplyr). It also avoid confusion between two functions from different packages with the same name.
source("R/01_load_data.R")# Modelling ---------------------------------------------------------------mod1 <-glm(Death ~ Age, data = LOS, family ="binomial")summary(mod1)mod2 <-glm(Death ~ Age + LOS, data = LOS, family ="binomial")summary(mod2)mod3 <-glm(Death ~ Age + LOS + Organisation, data = LOS, family ="binomial")summary(mod3)mod4 <-glm(Death ~ LOS, data = LOS, family ="binomial")summary(mod4)# Results -----------------------------------------------------------------model_table <-tbl_regression(mod3)model_table |>as_gt() |>gtsave("outputs/regression_table.docx")
Example 5: Other useful tips
See example
This code returns an error:
mean(LOS$Organisation)
If we just write the code mean(LOS$Organisation) returns an error, the person we’re asking for help doesn’t know what LOS looks like, what the Organisation column is, or which version of R or any packages you’re using. We can instead create a reproducible example to send them.
library(reprex)reprex()
If we can share the data, you can copy in the results of dput(LOS). If you can’t share the data, you’ll need to make a synthetic data set or use a built-in one for the example.
Collaborating on code with Git and GitHub for R users