Skip to content

Commit

Permalink
Merge pull request #14 from jumpingrivers/ggiraph-demo
Browse files Browse the repository at this point in the history
Ggiraph demo
  • Loading branch information
jr-nicola authored Nov 25, 2022
2 parents 19e9f20 + 57ee9af commit cb43ab0
Show file tree
Hide file tree
Showing 51 changed files with 130 additions and 13,409 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tfpbrowser
Title: Builds Shiny application for 'tfpscanner' outputs
Version: 0.0.5
Version: 0.0.6
Authors@R:
person("Jumping", "Rivers", , "[email protected]", role = c("aut", "cre"))
Description: An R package to contain the code for the Shiny app to explore the
Expand All @@ -12,6 +12,8 @@ Imports:
bslib,
dplyr,
fs,
ggplot2,
ggiraph,
glue,
htmltools,
janitor,
Expand All @@ -30,5 +32,5 @@ Suggests:
usethis
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
RoxygenNote: 7.2.2
LazyData: true
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# tfpbrowser 0.0.6 _2022-11-08_

- Switch to using rds inputs instead of html inputs
- Use {ggiraph} widget as input to folder selection
- Add functions to parse tooltip and extract cluster id
- Remove unnecessary html files

# tfpbrowser 0.0.5 _2022-11-02_

- Move panels from RHS to collapsible above
Expand Down
50 changes: 37 additions & 13 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,61 @@ app_server = function(input, output, session) {
mustWork = TRUE)),
server = TRUE)

# load static html for treeview
output$treeview = shiny::renderUI({


# Load treeview -----------------------------------------------------------

# create plotly output from saved ggplot2 outputs
output$treeview = ggiraph::renderGirafe({
filename = get_filename(input$widgetChoice)
shiny::div(
style = "width:100%; align:center",
id = "treeview",
htmltools::tags$iframe(src = filename,
width = "100%",
height = 600)
g = readRDS(filename)
tooltip_css = paste0(
"background-color:black;",
"color:grey;",
"padding:14px;",
"border-radius:8px;",
"font-family:\"Courier New\",monospace;"
)
suppressWarnings(ggiraph::girafe(ggobj = g,
options = list(
ggiraph::opts_selection(
type = "single"),
ggiraph::opts_sizing(
width = 0.8),
ggiraph::opts_tooltip(
css = tooltip_css,
use_fill = FALSE)
)
))
})

# Choose Cluster ID -------------------------------------------------------
number_from_cluster_mod = cluster_idServer("choice1")
# get selected cluster id based on widget choice
selected_cluster_id = shiny::reactive({
get_selected_cluster_id(widgetChoice = input$widgetChoice,
treeviewSelected = input$treeview_selected)
})

# output result of click
output$select_text = shiny::renderText({
paste("You have selected cluster ID:", selected_cluster_id())
})

# Tables Tab --------------------------------------------------------------
tablesServer(
"table1",
cluster_choice = number_from_cluster_mod
cluster_choice = selected_cluster_id
)

# Plots Tab ----------------------------------------------------------
plotsServer(
"plot1",
cluster_choice = number_from_cluster_mod
cluster_choice = selected_cluster_id
)

# RDS Tab ----------------------------------------------------------
rdsServer(
"rds1",
cluster_choice = number_from_cluster_mod
cluster_choice = selected_cluster_id
)

} # end server function
10 changes: 6 additions & 4 deletions R/app_ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ app_ui = function(request) {
# preview of expandable section
htmltools::tags$summary("Download Files (click to expand)"),

# choose cluster id
cluster_idUI("choice1"),
shiny::br(),

# text to print choice
shiny::textOutput("select_text"),
shiny::br(),

# output options
shiny::tabsetPanel(id = "plot_tabs",
Expand Down Expand Up @@ -67,10 +70,9 @@ app_ui = function(request) {

# show treeview widget
shiny::wellPanel(
shiny::htmlOutput("treeview"),
ggiraph::girafeOutput("treeview"),
style = "background: white",
),

shiny::br()
)
) # end fluid row
Expand Down
31 changes: 0 additions & 31 deletions R/module_clusterid.R

This file was deleted.

39 changes: 35 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
#' @param type Character string detailing type of widget to show
get_filename = function(type) {
filename = switch(type,
"Logistic growth rate" = "tree-logistic_growth_rate-2021-11-27.html", # nolint
"Simple logistic growth rate" = "tree-simple_logistic_growth_rate-2021-11-27.html", # nolint
"Simple trait log odds" = "tree-sim_trait_logodds-2021-11-27.html" # nolint
"Logistic growth rate" = "tree-logistic_growth_rate.rds", # nolint
"Simple logistic growth rate" = "tree-simple_logistic_growth_rate.rds", # nolint
"Simple trait log odds" = "tree-sim_trait_logodds.rds" # nolint
)
filename = file.path("www", "data", "treeview", filename)
filename = system.file("app", "www", "data", "treeview",
filename,
package = "tfpbrowser",
mustWork = TRUE)
return(filename)
}

Expand Down Expand Up @@ -134,3 +137,31 @@ downloader_tab_panel = function(title,
)
)
}

#' function to get node id from data_id column of ggplot
#' @param tooltip_input Character vector of tooltip content
get_cluster_ID = function(tooltip_input) {
# start searching the string after the "Cluster.ID" text
# until the next new line
match_matrix = stringr::str_match(tooltip_input, r"(Cluster.ID\s+#(\d+))")
cluster_ids = as.numeric(match_matrix[, 2])
return(cluster_ids)
}

#' function to get node id from data_id column of ggplot
#' @param widgetChoice From click of radio button to select widget to display
#' @param treeviewSelected Output from clicking on treeview plot
get_selected_cluster_id = function(widgetChoice,
treeviewSelected) {
filename = get_filename(widgetChoice)
g = readRDS(filename)
built = suppressWarnings(ggplot2::ggplot_build(g))
n_layers = length(built$data)
ids = built$data[n_layers][[1]]["data_id"]
tooltips = built$data[n_layers][[1]]$tooltip

tooltip_ids = get_cluster_ID(tooltips)
ids$cluster_ids = tooltip_ids
selected_cluster = as.numeric(ids[which(ids$data_id == treeviewSelected), 2])
return(selected_cluster)
}
Loading

0 comments on commit cb43ab0

Please sign in to comment.