-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example shiny app as a sandbox #3
Open
jonocarroll
wants to merge
13
commits into
main
Choose a base branch
from
sandbox
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
faa3805
WIP simple prototype app to display one of the analysis gadgets
jonocarroll 7ba1db7
dynamic add and remove buttons which clean up after themselves
jonocarroll e503cdf
add a running listing of results
jonocarroll 9ac8a59
loop outputs as inputs (not quite ready)
jonocarroll fd94bfa
add filtering as an output
jonocarroll 7a188cb
store faro results where possible
jonocarroll a6510c4
adding myself as a contributor
jonocarroll c1d3029
no need for special treatment of ReactiveFacileDataStore
jonocarroll 991d1c7
don't run analysisModule() for filter; already run RFDS() - prevents …
jonocarroll 7c4dbce
Uses `FacileShine::active_samples()` input came from a filtering func…
lianos 1221c02
use existing filteredReactiveFacileDataStore invocation coming from R…
jonocarroll 4ddb317
Merge branch 'sandbox' of github.com:facilebio/FacileIncubator into s…
jonocarroll a504cf7
cleanup
jonocarroll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
library(shiny) | ||
|
||
# https://github.com/Appsilon/dynamic-shiny-modules/blob/3b05aad99f633103788b62a94d8ed198ce4b977b/after.R | ||
remove_shiny_inputs <- function(id, .input) { | ||
invisible( | ||
lapply(grep(id, names(.input), value = TRUE), function(i) { | ||
.subset2(.input, "impl")$.values$remove(i) | ||
}) | ||
) | ||
} | ||
|
||
remove_observers <- function(id, .session) { | ||
invisible( | ||
lapply(grep(paste0(id, "_observer"), names(.session$userData), value = TRUE), | ||
function(i) { | ||
.subset2(.session$userData, i)$destroy() | ||
}) | ||
) | ||
} | ||
|
||
module_UI <- function(id, ui) { | ||
ns <- NS(id) | ||
div(id = id, ui) | ||
} | ||
|
||
shinyServer(function(input, output, session) { | ||
|
||
module_stack <- reactiveVal(NULL) | ||
results_stack <- reactiveVal(tibble::tibble(id = character(), analysis = character(), result = list())) | ||
|
||
observe({ | ||
shinyjs::disable("remove_module") | ||
}) | ||
|
||
## UI only works with debug = TRUE for some reason (!) | ||
debug <- TRUE | ||
bs4dash <- getOption("facile.bs4dash") | ||
options(facile.bs4dash = FALSE) | ||
on.exit(options(facile.bs4dash = bs4dash)) | ||
|
||
x <- eventReactive(input$dataset, { | ||
d <- switch(req(input$dataset), | ||
"TCGA" = FacileData:::exampleFacileDataSet(), | ||
results_stack()[results_stack()$id == input$dataset, "result", drop = TRUE][[1]] | ||
) | ||
if (is(d, "ReactiveFacileAnalysisResultContainer")) { | ||
.x <- FacileAnalysis::faro(d) | ||
} else { | ||
.x <- d | ||
} | ||
.x | ||
}) | ||
|
||
analysisModule <- reactive({ | ||
switch(req(input$analysis), | ||
"filter" = FacileShine::filteredReactiveFacileDataStore, | ||
"fdge" = FacileAnalysis::fdgeAnalysis, | ||
"fpca" = FacileAnalysis::fpcaAnalysis, | ||
"ffsea" = FacileAnalysis::ffseaAnalysis, | ||
NULL | ||
) | ||
}) | ||
|
||
analysisUI <- reactive({ | ||
switch(req(input$analysis), | ||
"filter" = FacileShine::filteredReactiveFacileDataStoreUI, | ||
"fdge" = FacileAnalysis::fdgeAnalysisUI, | ||
"fpca" = FacileAnalysis::fpcaAnalysisUI, | ||
"ffsea" = FacileAnalysis::ffseaAnalysisUI, | ||
NULL | ||
) | ||
}) | ||
|
||
observeEvent(input$add_module, { | ||
req(input$analysis != "none") | ||
|
||
# store the id of the newly added module using the | ||
# value of the actionButton to make it unique | ||
module_id <- paste0("id_", input$add_module) | ||
if (debug) print(paste0("this module is ", module_id)) | ||
module_stack(c(module_id, module_stack())) | ||
|
||
ui.content <- analysisUI()(ifelse(isolate(req(input$analysis)) == "filter","ds","analysis"), debug = debug) | ||
|
||
ui <- tagList( | ||
ui.content | ||
) | ||
|
||
ui_with_id <- module_UI(module_id, ui) | ||
|
||
## NOTE: immediate = TRUE is necessary! | ||
insertUI("#gadget_container", "afterEnd", ui_with_id, immediate = TRUE) | ||
|
||
shinyjs::disable("add_module") | ||
shinyjs::enable("remove_module") | ||
|
||
isolate(module_res()) | ||
}) | ||
|
||
observeEvent(input$remove_module, { | ||
if (NROW(req(module_stack())) > 0) { | ||
if (debug) { | ||
print(paste0("removing module ", module_stack()[1])) | ||
} | ||
removeUI(paste0("#", module_stack()[1])) | ||
} | ||
remove_shiny_inputs(module_stack()[1], input) | ||
remove_observers(module_stack()[1], session) | ||
module_stack(module_stack()[-1]) | ||
|
||
shinyjs::enable("add_module") | ||
shinyjs::disable("remove_module") | ||
}) | ||
|
||
## this logic should be isolated into a function | ||
rfds <- reactive({ | ||
req(input$dataset) | ||
req(input$analysis != "none") | ||
|
||
.x <- x() | ||
if (is(.x, "facile_frame")) { | ||
if (debug) print("facile_frame") | ||
fds. <- FacileData::fds(.x) | ||
samples. <- .x | ||
sample.filter <- FALSE | ||
restrict_samples <- samples. | ||
} else if (is(.x, "ReactiveFacileDataStore")) { | ||
if (debug) print("reactivefaciledatastore") | ||
fds. <- .x$.state$fds | ||
samples. <- .x$.state$active_samples | ||
} else if (is(.x, "FacileDataStore")) { | ||
if (debug) print("facileDataStore") | ||
sample.filter <- TRUE | ||
fds. <- .x | ||
samples. <- dplyr::collect(FacileData::samples(.x), n = Inf) | ||
} else if (is(.x, "FacileAnalysisResult")) { | ||
if (debug) print("facileAnalysisResult") | ||
# ugh, this isn't going to work -- I'm writing this in to fire up a | ||
# ffseaGadget, whose needs to be a FacileAnalysisResult. | ||
sample.filter <- FALSE | ||
fds. <- FacileData::fds(.x[["fds"]]) | ||
samples. <- dplyr::collect(FacileData::samples(.x), n = Inf) | ||
restrict_samples <- samples. | ||
} else if (is(.x, "ReactiveFacileAnalysisResultContainer")) { | ||
if (debug) print("result container") | ||
fds. <- .x | ||
samples. <- dplyr::collect(FacileData::samples(fds.), n = Inf) | ||
} else { | ||
stop("What in the world?") | ||
} | ||
checkmate::assert_class(fds., "FacileDataStore") | ||
checkmate::assert_class(samples., "facile_frame") | ||
|
||
FacileShine::ReactiveFacileDataStore(fds., "ds", samples = samples.) | ||
}) | ||
|
||
module_res <- reactive({ | ||
res <- callModule(analysisModule(), | ||
id = ifelse(req(input$analysis) == "filter", "ds", "analysis"), | ||
rfds = rfds(), | ||
aresult = x(), | ||
gdb = reactive({sparrow::exampleGeneSetDb()}), | ||
path= reactive(rfds()[["parent.dir"]]), | ||
debug = debug | ||
) | ||
if (req(input$analysis) == "filter") { | ||
return(rfds()) | ||
} else { | ||
return(res) | ||
} | ||
}) | ||
|
||
observeEvent(input$add_module, { | ||
input$dataset | ||
req(input$analysis != "none") | ||
module_res() | ||
}) | ||
|
||
observeEvent(input$remove_module, { | ||
results_stack( | ||
rbind( | ||
results_stack(), | ||
tibble::tibble(id = paste0("result_", input$add_module), analysis = input$analysis, result = list(req(module_res()))) | ||
) | ||
) | ||
if (debug) { | ||
print("results stack:") | ||
print(results_stack()) | ||
} | ||
|
||
shinyWidgets::updatePickerInput(session, "dataset", choices = c("TCGA", results_stack()$id), selected = input$dataset) | ||
}) | ||
|
||
output$results_list <- renderTable({ | ||
results_stack()[, c("id", "analysis")] | ||
}) | ||
|
||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
library(shiny) | ||
|
||
shinyUI(fluidPage( | ||
|
||
# Application title | ||
titlePanel("FacileSandbox"), | ||
|
||
sidebarLayout( | ||
sidebarPanel(width = 3, | ||
shinyjs::useShinyjs(), | ||
shinyWidgets::pickerInput("dataset", | ||
"Select an input:", | ||
choices = c("TCGA"), | ||
selected = "TCGA" | ||
), | ||
shinyWidgets::pickerInput("analysis", | ||
"Select an output:", | ||
choices = c("none", "filter", "fdge", "fpca", "ffsea"), | ||
selected = "none" | ||
), | ||
actionButton("add_module", "Add", icon = icon("plus-circle")), | ||
actionButton("remove_module", "Done", icon = icon("check-circle")), | ||
br(), | ||
h4("Results:"), | ||
tableOutput("results_list") | ||
), | ||
|
||
mainPanel( | ||
tags$div(id = "gadget_container") | ||
) | ||
) | ||
)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is growing a bit too big, but I think this is the correct way to extract out the relevant parts - feel free to correct me, I'm still learning how these parts work.