Skip to content

Instantly share code, notes, and snippets.

@xni7
Created October 6, 2021 20:05
Show Gist options
  • Save xni7/8bfa91f4d7f809d0d3d7c1954c360957 to your computer and use it in GitHub Desktop.
Save xni7/8bfa91f4d7f809d0d3d7c1954c360957 to your computer and use it in GitHub Desktop.
command line script to launch a QT app in SafetyGraphics V2 framework
# QT Shiny modules
# https://github.com/SafetyGraphics/safetyGraphics/wiki/ChartConfiguration#example-2---static-outlier-explorer
# Use sample clinical trial data sets from the {safetyData} package
library(safetyData)
library(safetyGraphics)
library(ggplot2)
library(dplyr)
library(yaml)
library(plotly)
library(forcats)
# Sample ADEG data
# https://physionet.org/content/ecgcipa/1.0.0/
# https://physionet.org/content/ecgcipa/1.0.0/adeg.csv
adeg <- readr::read_csv("https://physionet.org/files/ecgcipa/1.0.0/adeg.csv?download") %>%
mutate(ATPTFCT = forcats::fct_reorder(ATPT, .x = ATPTN, .fun = min)) %>%
mutate(ANRHI=0, ANRLO=0)
# QT module
safetyOutlierExplorerQT_ui <- function(id) {
ns <- NS(id)
sidebar<-sidebarPanel(
uiOutput(ns("selectMeasures"))
)
main<-mainPanel(
tabsetPanel(
tabPanel("QT Data Info", verbatimTextOutput(ns("info"))),
tabPanel("QT Vis", plotlyOutput(ns("outlierExplorerQT"), height = 800))
)
)
ui<-fluidPage(
sidebarLayout(
sidebar,
main,
position = c("right"),
fluid=TRUE
)
)
return(ui)
}
safety_outlier_explorerQT <- function(data, settings)
{
# rich graph
hline <- function(y = 0, color = "blue") {
list(
type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = y,
y1 = y,
line = list(color = color, width= 2, dash = 'dash')
)
}
(
fig <- data %>%
filter(!!sym(settings$measure_col) %in% settings$measure_values) %>%
mutate(Y450 = 450-BASE, Y480=480-BASE, Y500=500-BASE) %>%
plot_ly(
x = ~BASE,
y = ~CHG,
size = ~CHG,
color = ~TRTA,
frame = ~paste0(sprintf("%02d", ATPTN), " - ", ATPT),
text = ~paste0(PARAM, "<br>Time point: ", ATPT, "<br>Treatment: ",
TRTA, "<br>Baseline:", BASE, "<br>Change: ", CHG),
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
) %>%
animation_slider(
currentvalue = list(prefix = "Time Point: ")
)
)
fig %>%
layout(shapes = list(hline(0), hline(30), hline(60),
list(type="line", width= 2, line = list(dash = 'dash',color = "red"),
x0=0, x1=450, y0=450, y1=0)))
}
safetyOutlierExplorerQT_server <- function(input, output, session, params) {
#browser()
ns <- session$ns
output$selectMeasures <- renderUI({
measure_col <- params()$settings$measure_col
measures <- unique(params()$data[[measure_col]])
selectizeInput(
ns("measures"),
"Select Measures",
multiple=TRUE,
choices=measures,
selected = "QTcF"
)
})
# Populate control with measures and select all by default
# customize selected measures based on input
settingsR <- reactive({
settings <- params()$settings
settings$measure_values <- input$measures
return(settings)
})
# data info
output$info <- renderPrint({
params()$data %>% count(APERIOD, ATPTFCT, sort=FALSE) %>% data.frame
})
#draw the chart
output$outlierExplorerQT <- renderPlotly({
req(input$measures)
safety_outlier_explorerQT(params()$data, settingsR())
})
}
outlierQTMod_yaml <- '
env: safetyGraphics
label: QT Outlier Explorer - Module
type: module
package: safetyCharts
domain:
- labs
workflow:
ui: safetyOutlierExplorerQT_ui
server: safetyOutlierExplorerQT_server
links:
safetyCharts: https://github.com/SafetyGraphics/safetycharts
'
QTcharts <- makeChartConfig() %>% purrr::keep(~(max(.x$domain == "labs")>0))
QTcharts$outlierQTMod<-prepareChart(read_yaml(text=outlierQTMod_yaml))
mappings_QT <- read_yaml(text=
"
labs:
id_col: USUBJID
value_col: AVAL
measure_col: PARAM
measure_values:
ALT: ''
AST: ''
TB: ''
ALP: ''
normal_col_low: ANRLO
normal_col_high: ANRHI
studyday_col: ADY
visit_col: ATPT
visitn_col: ATPTN
unit_col: EGSTRESU
baseline_flag_col: ABLFL
baseline_flag_values: 'Y'
analysis_flag_col: ''
analysis_flag_values: ''
")
safetyGraphicsApp(charts=QTcharts, domainData = list(labs=adeg), mapping = mappings_QT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment