# packages & defs #### library(tidyverse) library(tidymodels) library(flexdashboard) library(shinyWidgets) library(bsicons) library(fontawesome) library(shiny) library(bslib) library(DT) library(shapviz) library(gh) library(thematic) library(xgboost) library(shinyBS) light <- bs_theme(bootswatch = "flatly") dark <- bs_theme(bootswatch = "darkly") thematic_shiny() # load scripts & data #### source("create_patient_module.R") source("about.R") source("funcs.R") dummy_df <- read_csv("dummy_df.csv",show_col_types = FALSE) # load git data #### git_token <- Sys.getenv("GITHUB_PAT") git_url <- Sys.getenv("GIT_URL") tmp = tempfile() gh(paste0('GET ', git_url), .destfile = tmp, .overwrite = TRUE, .token = git_token) model <- xgb.load(tmp) # ui #### ui <- page_navbar( title = "POP-Pe", selected = "Risk Calculator", collapsible = TRUE, fillable_mobile = TRUE, theme = light, nav_panel( title = "About", about_card ), nav_panel( title = "Risk Calculator", sidebarLayout( sidebarPanel( create_patient_UI("inputs"), card_footer( materialSwitch( inputId = "dark_mode", label = "Dark mode", value = FALSE, status = "primary" ), actionButton( inputId = "refresh", label = "Regenerate simulated patients", icon = icon("arrows-rotate") ) ) ), mainPanel( layout_column_wrap( width = 1/2, heights_equal = "row", uiOutput("risk_vb"),uiOutput("avg_vb") %>% tooltip("Estimated based on the original cohort") ), navset_card_underline( title = "Plots", # height = "800px", nav_panel("Effect of variables on prediction", plotOutput("shap_plot")), nav_panel("Simulated patients", plotOutput("syn_plot")) ) ) ) ) ) # server #### server <- function(input, output, session) { shiny::observe(session$setCurrentTheme( if (isTRUE(input$dark_mode)) dark else light )) inputData <- create_patient_Server("inputs") output$risk_vb <- renderUI({ p <- generate_model(inputData(), model) theme_zone <- case_when( p < 1.7 ~ "success", p < 10 ~ "warning", p >= 10 ~ "danger" ) app_icon <- case_when( p < 1.7 ~ "emoji-smile", p < 10 ~ "emoji-neutral", p >= 10 ~ "emoji-frown" ) bslib::value_box( title = "Risk for sustained use", value = paste(p,"%"), theme = theme_zone, showcase = bsicons::bs_icon(app_icon) ) }) output$avg_vb <- renderUI({ bslib::value_box( title = "Average risk for cohort", value = "1.7%", theme = "light", showcase = bsicons::bs_icon("person-arms-up") ) }) output$shap_plot <- renderPlot({ sv_waterfall(shapviz(model, X_pred = as.matrix(inputData())), row_id = 1, fill_colors = c( "#a52c60","#f7d13d"), annotation_size = 5) + scale_y_discrete(labels = vars_label) + theme_minimal(base_size = 20) + theme(axis.text.y = element_text(size = 15), legend.position = "none") }) output$syn_plot <- renderPlot({ syn_data <- syn_df(dummy_df, inputData(), model) plot_close_side(syn_data) }) observeEvent(reactiveValuesToList(input), { syn_data <- syn_df(dummy_df, inputData(), model) output$syn_plot <- renderPlot({ plot_close_side(syn_data) }) }) } shinyApp(ui, server)