Examples

Data

Length of Stay Model Data

The LOS dataset is a simulated hospital length-of-stay dataset, available through the {NHSRdatasets} R package. The dataset is licensed under creativecommons.org/publicdomain/zero/1.0.

Download CSV: LOS.csv

R Script

The R script used in the examples can be downloaded below.

Download .R Script: example-analysis.R

See R script
example-analysis.R
# Load packages -----------------------------------------------------------

library(gtsummary)
library(ggcorrplot)
library(tidyverse)
library(forcats)


# Load LOS ---------------------------------------------------------------

LOS <- read_csv("data/LOS.csv") # nolint

LOS <- LOS |> # nolint
  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)
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")

Examples

This section includes code for the examples shown. These may differ slightly from the examples shown in the live demonstration.

Example 1: Introduction to Git and GitHub for R

See example
  • Make sure git is talking to R

  • Create a token

  • Set the token

In the terminal:

git --version

Configure git with your GitHub username, and the email associated with your GitHub account:

library(usethis)
use_git_config(
  user.name = "username",
  user.email = "username@example.org"
)

Then run create_github_token() which will open up a web browser:

The click generate.

Paste in the token.

Example 2: Creating and cloning a repository

See example

From your GitHub profile page, click Repositories then click New:

Go to File –> New ProjectVersion Control –> Git

Copy the URL from GitHub (ending with .git):

into the Repository URL and select where you want the project to be.

Exercise 3: Committing and pushing changes

See example

After adding or making some edits to files, from the Git tab in RStudio, click the check boxes of the files you want to commit:

Then click Commit and a pop-up window should appear. Enter an (informative) commit message. The click Commit again.

You should then see (another popup) which you can Close. Copy and paste the files from his mornings session into the git folder.You should see them appear in the Git tab:

Open up the .gitignore file in RStudio (you might need to set Show hidden files). Add the line data/ to the .gitignore file:

.Rproj.user
.Rhistory
.RData
.Ruserdata
data/

The data folder should no longer show in the Git tab, and the .gitignore file should show it has been modified.

Commit the changes as you did before. Then click Push.

Go back to the repository on github.com. You should see your R files, but not your data.

Exercise 4: Working in branches

See example

In the Git tab, click New Branch and give it a useful name. Then click Create.

Make an edit and commit your changes (as you did in the previous exercise). Push your changes.

Go back to the repository on github.com. You should see a box to Compare & pull request. Click it.

Add a description, then click Create pull request.

You can click Files changed to see what changes have been made.

Exercise 5: Handling git conflicts

See example
  • From the Code tab on the GitHub website, click through to the file you want to edit.

  • Make a change, then click Commit changes… in the top right.

  • Return to the Pull requests tab. You might see a message that says This branch has conflicts that must be resolved.

  • If not, go ahead and click Merge pull request.

  • If you have a conflict, there are different ways to resolve it. For small conflicts, it can be easiest to click *Use web editor**. You’ll see the two different versions of the code.

  • Edit the file, and select (or further edit) the code you want to keep.

  • The click Mark as resolved, then Commit merge.

  • You can then return to the Pull requests page, and Confirm merge:

  • Remember to delete the branch if you’re finished with it!

  • In RStudio, change the branch back to main, and Pull the changes.

Example 6: Sharing and reviewing

See example
  • An example of a README.md file

See: github.com/nrennie/PrettyCols

<!-- badges: start -->
  [![R-CMD-check](https://github.com/nrennie/PrettyCols/workflows/R-CMD-check/badge.svg)](https://github.com/nrennie/PrettyCols/actions)
  [![codecov](https://codecov.io/gh/nrennie/PrettyCols/branch/main/graph/badge.svg)](https://app.codecov.io/gh/nrennie/PrettyCols?branch=main)
  [![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/PrettyCols)](https://cran.r-project.org/package=PrettyCols)

<!-- badges: end -->

# PrettyCols <img src="man/figures/logo.png" align="right" width="120" />

{PrettyCols} is an R package containing aesthetically pleasing colour palettes. 

The structure of the package is based on code from the [{PNWColors}](https://github.com/jakelawlor/PNWColors),  [{wesanderson}](https://github.com/karthik/wesanderson), and [{MetBrewer}](https://github.com/BlakeRMills/MetBrewer) packages. 

## Installation

{PrettyCols} is available on CRAN. Install using:
``` r
install.packages("PrettyCols")
```
or install the development version from GitHub:
``` r
remotes::install_github("nrennie/PrettyCols")
```

See [nrennie.rbind.io/PrettyCols](https://nrennie.rbind.io/PrettyCols/) for full documentation.

If you have a suggestion of an additional feature, or find a bug, please file an issue on the [GitHub repository](https://github.com/nrennie/ggflowchart/issues).

## Using {PrettyCols} with other software.

* A Python implementation of this package can be found at [github.com/nrennie/PrettyPyCols](https://github.com/nrennie/PrettyPyCols).

* QGIS users can use {PrettyCols} through QGIS Style at [plugins.qgis.org/styles/192](https://plugins.qgis.org/styles/192/). Thanks to [@Heed725](https://github.com/Heed725) for the implementation!

## Contributor guidelines

If you'd like to contribute to {PrettyCols}, I'd welcome your help. If you're making a PR, please follow the guidelines below, to make the collaboration easier:

- You have updated the NEWS and version number in DESCRIPTION.
- You have checked that R CMD check passes with no ERRORs or WARNINGs. If there is a NOTE - please outline what it is in the PR.
- You have checked that `lintr::lint_package()` passes.
- You have checked the list of packages in Imports is still in alphabetical order to enable better tracking of dependencies as the package grows.
- You have not used the base R `|>` pipe (we're not quite ready to specify R 4.1 or higher as a dependency yet!).
- If this is a feature request PR (not a bug fix) please make sure it relates to an issue that has not been assigned to someone else (and tag the issue in the PR description).

If these checks fail, and there is no response from the PR author for 1 month, the PR will be automatically closed.
  • Within the GitHub repository, go to Settings -> Collaborators, Click Add people and add the person next to you as a collaborator using their username.

  • Request a review from your new collaborator.