ClevelandClinicQHS / riskcalc

An R package for building risk calculators
https://clevelandclinicqhs.github.io/riskcalc/
Other
2 stars 0 forks source link

riskcalc

test-coverage R-CMD-check CRAN
status CRAN_Download_Counter Lifecycle:
experimental

The goal of riskcalc is to accelerate building shiny-based risk calculators for https://riskcalc.org/. It enables you to export an executable application template into a directory containing the pre-populated R scripts to run your app using the standard formatting. You can get a blank template to start from scratch, or provide model objects to the call to populate inputs/outputs for you. From there, you can make the necessary tweaks to complete your application.

Installation

You can install the development version of riskcalc from GitHub with:

# install.packages("devtools")
devtools::install_github("ClevelandClinicQHS/riskcalc")

Examples

Here we’ll go through a couple examples. First, load the package.

# Load the package
library(riskcalc) 

Blank app

The risk_calulator function by default will export a blank application template to your working directory with a default name. You can optionally specify the location to place the app, as well as its name.

risk_calculator(
  app_directory = "/PATH_TO_FOLDER",
  app_name = "BlankRiskCalculator"
)

Execution of the above code will create a new directory in /PATH_TO_FOLDER/BlankRiskCalculator with the following contents:

In RStudio, we can then open (one of) these files and click Run App, as it is a functioning shiny application. It looks like this:

It provides the default app themes and information panels, as well as creates the necessary source code link for when the R code gets pushed to the website’s GitHub repository. The ui.R and server.R files have various placeholders for you to go in and add titles, variable inputs, etc., as needed.

Model populated app

You can also provide a glm or coxph object to the risk_calculator function call to get the scripts for a functioning risk calculator with all of the inputs and prediction calculation setup for you, along with other arguments for various formatting preferences. We’ll go through an example of a creating an application that mimics the appearance and functionality of the bladder cancer app:


# Load packages
library(riskcalc) 
library(survival)

# Build the model
mod <- 
  coxph(
    formula = Surv(Time, Recurrence) ~ .,
    data = bladderCancer
  )

mod |>

  # Build the risk calculator
  risk_calculator(
    time = 5, # Produce 5-year survival probabilities
    app_directory = "/PATH_TO_FOLDER",
    app_name = "bladderCancer",
    title = "Predicting 5-Year Recurrence-Free Survival after Radical Cystectomy for Bladder Cancer",
    citation = 
      htmltools::HTML("<p>[1] International Bladder Cancer Nomogram Consortium, Bochner BH, Kattan MW, Vora KC.
      <a href = 'http://jco.ascopubs.org/content/24/24/3967.full.pdf'> Postoperative nomogram predicting risk of recurrence after radical cystectomy for bladder cancer</a>
      . J Clin Oncol. 2006 Aug 20;24(24):3967-72. Epub 2006 Jul 24. Erratum in: J Clin Oncol. 2007 Apr 10;25(11):1457</p>"),
    label = "Percentage of 5-Year Recurrence-Free Survival",
    value_header = "Probability",
    format = function(x) paste0(round(100 * x), "%"),
    labels =
      c(
        Age = "Age (Years)",
        RCTumorPathology = "RC Tumor Pathology",
        RCTumorHistology = "RC Tumor Histology",
        RCTumorGrade = "RC Tumor Grade",
        LymphNodeStatus = "LymphNodeStatus",
        DaysBetweenDXRC = "Days Between Dx and RC (Days)"
      ),
    levels = 
      list(
        Gender = 
          c(
            M = "Male",
            `F` = "Female"
          )
      ),
    placeholders = c(Age = "20-100")
  )

This creates a new directory in /PATH_TO_FOLDER/bladderCancer with the following contents:

In RStudio, we can then open (one of) these .R files and click Run App, as it is a functioning shiny application. It looks like this after the inputs are entered and the “Run Calculator” button is pressed:

Some notes on select arguments: