Giter Site home page Giter Site logo

nhs-r-community / demos-and-how-tos Goto Github PK

View Code? Open in Web Editor NEW
29.0 29.0 13.0 13.28 MB

A repo for community contributed demos and how-tos to get common stuff done in the R language

Home Page: https://nhs-r-community.github.io/demos-and-how-tos/

License: MIT License

R 4.35% HTML 95.65%

demos-and-how-tos's Introduction

Source code for NHS-R Community website

Following the values and commitment of the NHS-R Community to open source this website has been built in Quarto to be open in its code and further contribution.

Many of the blogs, which go back to the start of NHS-R Community in 2018, were first posted through a WordPress site and so we don't have a record in GitHub for their contributions, however, the author and the date they wrote the blog are in the source code.

New contributions and corrections can be made directly to this repository through a pull request. Details on how to contribute to an NHS-R Community GitHub repository can be found in the Statement on Tools technical appendix.

This website is not yet live!

Whilst this site is being built the current url www.nhsrcommunity.com is directed to the WordPress site. All the blog posts are being saved under the original titles so that when this site is redirected to the www.nhsrcommunity.com url they will be same as the WordPress.

demos-and-how-tos's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

demos-and-how-tos's Issues

Additional procedural generation of markdown reports

I was going to contribute a short example of procedurally generating markdown reports using params, but see this is already covered quite well.

The issue is whether the scripts I have add anything of sufficient additional value to the extant examples to be worth incorporating. I'm happy with any decision regarding this, including just closing the issue.

The materials are here; a subfolder 'outputs' would have to be added for the reports, but other than that (and ensuring the packages are loaded) it should run as is.

I think the .R + .rmd files add the following to the examples already in the repo:

  1. The construction of a function for calling the markdown template document, which is set up as per the render_one function example in the rmarkdown cookbook chapter on parameterised reports.
  2. The use of the walk function to 'loop' using tidyverse functional programming conventions, in addition to the more intuitive for loop pattern.

If the above provides enough added value I'm happy for the code in part or in entirety to be added (with attribution) to the relevant section of the repo.

ccg mergers - ICB update needed

when running the first code section it now only returns 142 records. Removing the requirement of CCG returns 248 records as per demo, however these still appear different than the original list.

NHSRtools::ods_get_ccgs() |>
select(name, org_id, last_change_date, status) |>
filter(str_detect(name, "^NHS"))

Can this be in Bookdown?

We had a cook book idea so should this be a way of collecting some of those ideas before being 'written up' for the cook book? What do you think @tomjemmett?

Add caseload size calculation

Using this script to produce example data (or submitting your own example data) please add a function that calculates the caseload size per team (or teams) by day (or range of days).

See the README for an explanation of the data.

We would love multiple solutions to this please- tidyverse, base, data.table, SQL, Python, you name it πŸ™‚

Add age band grouped data

From the Slack discussion in help-with-r, code suggestions were:
(contributions from @tomjemmett, https://github.com/sebastian-fox, @zx8754, @TimTaylor)

currently 5 styles of approach jump out:

  1. dplyr::recode()
  2. factor approaches (base or forcats)
  3. Named vector.
  4. Explicit joins (base merge, dplyr or data.table)
  5. dplyr::case_when() / data.table::fcase()
list_of_csv[['Lancaster']] %>%
  mutate(
    # Create categories
    age_group = dplyr::case_when(
      age == '00_04' | age == '05_09' | age == '10_14' | age == '15_19' ~ '0-19'
    ),
    # Convert to factor
    age_group = factor(
      age_group,
      level = c('0-19')
    )
  )

Doesn't work with character though:

 cut(0:25, c(0, 5, 10, 15, 20, Inf), c("0-4", "5-9", "10-14", "15-19", "20+"), right = TRUE)
 [1] <NA>  0-4   0-4   0-4   0-4   0-4   5-9   5-9   5-9   5-9   5-9   10-14 10-14 10-14 10-14 10-14 15-19 15-19 15-19 15-19 15-19 20+   20+  
[24] 20+   20+   20+  
Levels: 0-4 5-9 10-14 15-19 20+
list_of_csv[['Lancaster']] %>%
  mutate(
    # Create categories
    age_group = dplyr::case_when(
      age %in% c('00_04', '05_09', '10_14', '15_19') ~ '0-19',
      TRUE ~ 'Other age bands'
    ),
    # Convert to factor
    age_group = factor(
      age_group,
      level = c('0-19')
    )
  )
# lookup approach
lookup <- c(
    "00_04" = "00_19",
    "05_09" = "00_19",
    "10_14" = "00_19",
    "15_19" = "00_19",
    "20_29" = "20_29",
    "30_99" = "30_99"
)

# get some dummy data
(dat <- sample(names(lookup), size = 20L, replace = TRUE))
#>  [1] "15_19" "15_19" "10_14" "15_19" "30_99" "20_29" "05_09" "30_99" "10_14"
#> [10] "30_99" "10_14" "10_14" "05_09" "30_99" "05_09" "00_04" "20_29" "15_19"
#> [19] "20_29" "15_19"

# use lookup
data.frame(old = dat, new = lookup[dat])
#>      old   new
#> 1  15_19 00_19
#> 2  15_19 00_19
#> 3  10_14 00_19
#> 4  15_19 00_19
#> 5  30_99 30_99
#> 6  20_29 20_29
#> 7  05_09 00_19
#> 8  30_99 30_99
#> 9  10_14 00_19
#> 10 30_99 30_99
#> 11 10_14 00_19
#> 12 10_14 00_19
#> 13 05_09 00_19
#> 14 30_99 30_99
#> 15 05_09 00_19
#> 16 00_04 00_19
#> 17 20_29 20_29
#> 18 15_19 00_19
#> 19 20_29 20_29
#> 20 15_19 00_19
# example data using @TimTaylorUKHSA lookup 
set.seed(1); dat <- data.frame(age = sample(names(lookup), size = 10L, replace = TRUE))

library(forcats)
library(dplyr)

dat %>% 
  mutate(ageForcats = fct_collapse(age, `00_19` = c("00_04", "05_09", "10_14", "15_19")),
         ageFactor = factor(age,
                          levels = c("00_04", "05_09", "10_14", "15_19", "20_29", "30_99"),
                          labels = c("00_19", "00_19", "00_19", "00_19", "20_29", "30_99")))

#      age ageForcats ageFactor
# 1  00_04      00_19     00_19
# 2  15_19      00_19     00_19
# 3  00_04      00_19     00_19
# 4  05_09      00_19     00_19
# 5  20_29      20_29     20_29
# 6  10_14      00_19     00_19
# 7  30_99      30_99     30_99
# 8  05_09      00_19     00_19
# 9  10_14      00_19     00_19
# 10 10_14      00_19     00_19

Possibly opaque?

dat$age[ as.integer(factor(dat$age)) < 5 ] <- "00_19"

Heatmap from a table

People often want tables of numbers with heatmap style colouring on. Produce a generic solution for this using something like {reactable} or {kableExtra}

3 axis on a plot

Question posed on the Slack. Code was suggested but it also led to a question of when this can be misleading:

mtcars |>
  dplyr::mutate(ID = seq_along(mpg), .before = 1) |>
  ggplot2::ggplot() +
  ggplot2::geom_point(ggplot2::aes(x = ID, y = mpg, 
                                   col = "Miles/(US) gallon")) +
  ggplot2::geom_point(ggplot2::aes(x = ID, y = hp / 10, 
                                   col = "Gross horsepower")) +
  ggplot2::scale_y_continuous(
    name = "Miles/(US) gallon",
    breaks = scales::pretty_breaks(10),
    sec.axis = ggplot2::sec_axis(~.*10,
                                 name = "Gross horsepower",
                                 breaks = scales::pretty_breaks(10))
  ) +
  ggplot2::scale_colour_manual(name = NULL, 
                               values = c("Miles/(US) gallon" = "#CC2200",
                                          "Gross horsepower" = "black")
  )

Just note that in my example code, I β€œscaled” the axis by dividing the original values of the 2ndary variable by 10, ggplot2::aes(x = ID, y = hp / 10, then I multiplied the scale by 10 (so the axis labels have the original scale), ggplot2::sec_axis(~.*10. Try removing those, so you will see the difference and try playing with different scaling values.


If you mean 2 y-axes it's possible now https://ggplot2.tidyverse.org/reference/sec_axis.html but I'd caution using it.
I had a chart I showed people for years with 2 axes comparing the Trust deaths with the Region to show the seasonal pattern of deaths were the same - but not comparing numbers. I always mentioned it in meetings as it looked like the numbers of Trust deaths were higher than Region - which is impossible as we are part of the region. Turned out it had was still being read like that.

Same data but different types of charts: https://blog.datawrapper.de/dualaxis/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.