Skip to content

Commit

Permalink
shiny app 'Survival probabilities'
Browse files Browse the repository at this point in the history
  • Loading branch information
stla committed Oct 31, 2020
1 parent e447d89 commit 775c416
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 3 deletions.
6 changes: 3 additions & 3 deletions R/amStackedBarChart.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ amStackedBarChart <- function(
elementId = NULL
) {

series <- do.call(append, stacks)
series <- do.call(c, stacks)

if(is.null(yLimits)){
dats <- lapply(stacks, function(stack){
as.matrix(data[stack])
})
sums <- do.call(append, lapply(dats, function(dat){
sums <- do.call(c, lapply(dats, function(dat){
apply(dat, 1L, sum)
}))
yLimits <- c(0, max(pretty(sums)))
Expand All @@ -228,7 +228,7 @@ amStackedBarChart <- function(
}

stacks <- setNames(as.list(
t(do.call(append, lapply(lengths(stacks), function(stacklength){
t(do.call(c, lapply(lengths(stacks), function(stacklength){
out <- rep(TRUE, stacklength)
out[1L] <- FALSE
out
Expand Down
82 changes: 82 additions & 0 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,88 @@ renderAmChart4 <- function(expr, env = parent.frame(), quoted = FALSE) {
#' if(interactive()){
#' shinyApp(ui, server)
#' }
#'
#'
#' # Survival probabilities ####
#' library(shiny)
#' library(rAmCharts4)
#'
#' probs <- c(control = 30, treatment = 75) # initial probabilities
#'
#' ui <- fluidPage(
#' br(),
#' sidebarLayout(
#' sidebarPanel(
#' wellPanel(
#' tags$fieldset(
#' tags$legend("Survival probability"),
#' sliderInput(
#' "control",
#' "Control group",
#' min = 0, max = 100, value = probs[["control"]], step = 1
#' ),
#' sliderInput(
#' "treatment",
#' "Treatment group",
#' min = 0, max = 100, value = probs[["treatment"]], step = 1
#' )
#' )
#' )
#' ),
#' mainPanel(
#' amChart4Output("barchart", width = "500px", height = "400px")
#' )
#' )
#' )
#'
#' server <- function(input, output, session){
#'
#' dat <- data.frame(
#' group = c("Control", "Treatment"),
#' alive = c(probs[["control"]], probs[["treatment"]]),
#' dead = 100 - c(probs[["control"]], probs[["treatment"]])
#' )
#' stacks <- list(
#' c("alive", "dead")
#' )
#' seriesNames <- list(
#' alive = "Alive",
#' dead = "Dead"
#' )
#'
#' output[["barchart"]] <- renderAmChart4({
#' amStackedBarChart(
#' dat,
#' category = "group",
#' stacks = stacks,
#' seriesNames = seriesNames,
#' yLimits = c(0, 100),
#' chartTitle = amText(
#' "Survival probabilities",
#' fontFamily = "Trebuchet MS",
#' fontSize = 30,
#' fontWeight = "bold"
#' ),
#' xAxis = "Group",
#' yAxis = "Probability",
#' theme = "dataviz"
#' )
#' })
#'
#' observeEvent(list(input[["control"]], input[["treatment"]]), {
#' newdat <- data.frame(
#' group = c("Control", "Treatment"),
#' alive = c(input[["control"]], input[["treatment"]]),
#' dead = 100 - c(input[["control"]], input[["treatment"]])
#' )
#' updateAmBarChart(session, "barchart", newdat)
#' })
#'
#' }
#'
#' if(interactive()){
#' shinyApp(ui, server)
#' }
updateAmBarChart <- function(session, outputId, data){
stopifnot(is.data.frame(data))
session$sendCustomMessage(paste0(outputId, "bar"), data)
Expand Down
82 changes: 82 additions & 0 deletions man/updateAmBarChart.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 775c416

Please sign in to comment.