Skip to content

Commit 15305a2

Browse files
committed
Simplyfication in the copy plot functionality.
1 parent 029fcf4 commit 15305a2

6 files changed

Lines changed: 250 additions & 350 deletions

File tree

program/shinyApp/R/SourceAll.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
source("R/version.R",local = T)
22
source("R/util.R", local = T)
33
source("R/C.R",local = T)
4+
# Source constants
5+
source("R/constants/ui_styles.R", local = T)
6+
# Source utility helpers
7+
source("R/utils/plotly_helpers.R", local = T)
8+
source("R/utils/ui_components.R", local = T)
49
source("R/fun_filterRNA.R",local = T)
510
source("R/fun_LFC.R",local = T)
611
source("R/heatmap/fun_entitieSelection.R",local = T)

program/shinyApp/R/pca/server.R

Lines changed: 23 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -114,137 +114,41 @@ pca_Server <- function(id, session_data, session_params, data_input_shiny){
114114
output$PCA_plot <- renderPlotly({
115115
req(pca_reactives$allow_plot)
116116
req(pca_reactives$PCA_plot)
117-
118-
p <- ggplotly(
119-
pca_reactives$PCA_plot,
120-
tooltip = ifelse(is.null(input$PCA_anno_tooltip),"all","chosenAnno"),
117+
118+
# Use helper function for clipboard functionality
119+
create_clipboard_plotly(
120+
gg_plot = pca_reactives$PCA_plot,
121+
plot_id = "PCA_plot",
122+
tooltip = ifelse(is.null(input$PCA_anno_tooltip), "all", "chosenAnno"),
121123
legendgroup = "color"
122124
)
123-
124-
onRender(p, "
125-
function(el, x) {
126-
Plotly.newPlot(el, x.data, x.layout, {
127-
modeBarButtonsToAdd: [{
128-
name: 'Copy to clipboard',
129-
icon: Plotly.Icons.camera, // placeholder icon
130-
click: function(gd) {
131-
Plotly.toImage(gd, {format: 'png'}).then(function(url) {
132-
fetch(url)
133-
.then(res => res.blob())
134-
.then(blob => {
135-
navigator.clipboard.write([
136-
new ClipboardItem({ [blob.type]: blob })
137-
]).then(function() {
138-
Shiny.setInputValue('plot_copied_status', 'PCA_plot_success_' + Date.now(), {priority: 'event'});
139-
}).catch(function(err) {
140-
Shiny.setInputValue('plot_copied_status', 'PCA_plot_clipboard_error_' + Date.now(), {priority: 'event'});
141-
});
142-
});
143-
});
144-
}
145-
}],
146-
modeBarButtonsToRemove: ['toImage']
147-
});
148-
}
149-
")
150125
})
151126
output$PCA_Loadings_plot <- renderPlotly({
152127
req(pca_reactives$allow_plot)
153-
p = ggplotly(
154-
pca_reactives$Loadings_plot
155-
) %>%
156-
layout(
157-
yaxis = list(tickfont = list(size = 15)),
158-
xaxis = list(tickfont = list(size = 15))
159-
)
160-
onRender(p, "
161-
function(el, x) {
162-
Plotly.newPlot(el, x.data, x.layout, {
163-
modeBarButtonsToAdd: [{
164-
name: 'Copy to clipboard',
165-
icon: Plotly.Icons.camera, // placeholder icon
166-
click: function(gd) {
167-
Plotly.toImage(gd, {format: 'png'}).then(function(url) {
168-
fetch(url)
169-
.then(res => res.blob())
170-
.then(blob => {
171-
navigator.clipboard.write([
172-
new ClipboardItem({ [blob.type]: blob })
173-
]).then(function() {
174-
Shiny.setInputValue('plot_copied_status', 'PCA_plot_success_' + Date.now(), {priority: 'event'});
175-
}).catch(function(err) {
176-
Shiny.setInputValue('plot_copied_status', 'PCA_plot_clipboard_error_' + Date.now(), {priority: 'event'});
177-
});
178-
});
179-
});
180-
}
181-
}],
182-
modeBarButtonsToRemove: ['toImage']
183-
});
184-
}
185-
")
128+
129+
# Use helper function for clipboard functionality (includes standard layout)
130+
create_clipboard_plotly(
131+
gg_plot = pca_reactives$Loadings_plot,
132+
plot_id = "PCA_Loadings_plot"
133+
)
186134
})
187135
output$Scree_Plot <- renderPlotly({
188136
req(pca_reactives$allow_plot)
189-
p = ggplotly(
190-
pca_reactives$Scree_plot
137+
138+
# Use helper function for clipboard functionality
139+
create_clipboard_plotly(
140+
gg_plot = pca_reactives$Scree_plot,
141+
plot_id = "Scree_plot"
191142
)
192-
onRender(p, "
193-
function(el, x) {
194-
Plotly.newPlot(el, x.data, x.layout, {
195-
modeBarButtonsToAdd: [{
196-
name: 'Copy to clipboard',
197-
icon: Plotly.Icons.camera, // placeholder icon
198-
click: function(gd) {
199-
Plotly.toImage(gd, {format: 'png'}).then(function(url) {
200-
fetch(url)
201-
.then(res => res.blob())
202-
.then(blob => {
203-
navigator.clipboard.write([
204-
new ClipboardItem({ [blob.type]: blob })
205-
]).then(function() {
206-
Shiny.setInputValue('plot_copied_status', 'PCA_plot_success_' + Date.now(), {priority: 'event'});
207-
}).catch(function(err) {
208-
Shiny.setInputValue('plot_copied_status', 'PCA_plot_clipboard_error_' + Date.now(), {priority: 'event'});
209-
});
210-
});
211-
});
212-
}
213-
}],
214-
modeBarButtonsToRemove: ['toImage']
215-
});
216-
}
217-
")
218143
})
219144
output$PCA_Loadings_matrix_plot <- renderPlotly({
220145
req(pca_reactives$allow_plot)
221-
p = ggplotly(pca_reactives$LoadingsMatrix_plot)
222-
onRender(p, "
223-
function(el, x) {
224-
Plotly.newPlot(el, x.data, x.layout, {
225-
modeBarButtonsToAdd: [{
226-
name: 'Copy to clipboard',
227-
icon: Plotly.Icons.camera, // placeholder icon
228-
click: function(gd) {
229-
Plotly.toImage(gd, {format: 'png'}).then(function(url) {
230-
fetch(url)
231-
.then(res => res.blob())
232-
.then(blob => {
233-
navigator.clipboard.write([
234-
new ClipboardItem({ [blob.type]: blob })
235-
]).then(function() {
236-
Shiny.setInputValue('plot_copied_status', 'PCA_loadings_matrix_plot_success_' + Date.now(), {priority: 'event'});
237-
}).catch(function(err) {
238-
Shiny.setInputValue('plot_copied_status', 'PCA_loadings_matrix_plot_clipboard_error_' + Date.now(), {priority: 'event'});
239-
});
240-
});
241-
});
242-
}
243-
}],
244-
modeBarButtonsToRemove: ['toImage']
245-
});
246-
}
247-
")
146+
147+
# Use helper function for clipboard functionality
148+
create_clipboard_plotly(
149+
gg_plot = pca_reactives$LoadingsMatrix_plot,
150+
plot_id = "PCA_loadings_matrix_plot"
151+
)
248152
})
249153

250154
observeEvent(input$Do_PCA,{ # Calculate values needed for PCA

program/shinyApp/R/significance_analysis/results_tab/components/volcano_server.R

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -171,57 +171,24 @@ generate_volcano_plots <- function(result, input, output, ids, tab_state,
171171
session_params$SigAna$th_lfc <- th_lfc
172172
session_params$SigAna$anno_vector <- anno_vector
173173

174-
# Render plotly outputs
174+
# Render plotly outputs using centralized helper function
175175
output[[ids$volcano_plot_adj]] <- renderPlotly({
176-
create_plotly_with_clipboard(volcano_obj$volcano_plt, anno_col_name)
176+
create_clipboard_plotly(
177+
gg_plot = volcano_obj$volcano_plt,
178+
plot_id = ids$volcano_plot_adj,
179+
tooltip = ifelse(is.null(anno_col_name), "all", "chosenAnno")
180+
) %>% plotly::layout(showlegend = TRUE)
177181
})
178182

179183
output[[ids$volcano_plot_raw]] <- renderPlotly({
180-
create_plotly_with_clipboard(volcano_obj_raw$volcano_plt, anno_col_name)
184+
create_clipboard_plotly(
185+
gg_plot = volcano_obj_raw$volcano_plt,
186+
plot_id = ids$volcano_plot_raw,
187+
tooltip = ifelse(is.null(anno_col_name), "all", "chosenAnno")
188+
) %>% plotly::layout(showlegend = TRUE)
181189
})
182190
}
183191

184-
#' Create Plotly object with clipboard functionality
185-
#'
186-
#' Adds a custom clipboard button to Plotly plots.
187-
#'
188-
#' @param ggplot_obj ggplot object to convert to plotly
189-
#' @param anno_col_name Name of annotation column for tooltips
190-
#' @return Plotly object with clipboard button
191-
#' @export
192-
create_plotly_with_clipboard <- function(ggplot_obj, anno_col_name) {
193-
p <- ggplotly(ggplot_obj,
194-
tooltip = ifelse(is.null(anno_col_name), "all", "chosenAnno")) %>%
195-
layout(showlegend = TRUE)
196-
197-
onRender(p, "
198-
function(el, x) {
199-
Plotly.newPlot(el, x.data, x.layout, {
200-
modeBarButtonsToAdd: [{
201-
name: 'Copy to clipboard',
202-
icon: Plotly.Icons.camera,
203-
click: function(gd) {
204-
Plotly.toImage(gd, {format: 'png'}).then(function(url) {
205-
fetch(url)
206-
.then(res => res.blob())
207-
.then(blob => {
208-
navigator.clipboard.write([
209-
new ClipboardItem({ [blob.type]: blob })
210-
]).then(function() {
211-
Shiny.setInputValue('plot_copied_status', el.id + '_success_' + Date.now(), {priority: 'event'});
212-
}).catch(function(err) {
213-
Shiny.setInputValue('plot_copied_status', el.id + '_clipboard_error_' + Date.now(), {priority: 'event'});
214-
});
215-
});
216-
});
217-
}
218-
}],
219-
modeBarButtonsToRemove: ['toImage']
220-
});
221-
}
222-
")
223-
}
224-
225192
#' Setup legend toggle observers
226193
#'
227194
#' Creates observers for the legend visibility checkboxes.

program/shinyApp/R/single_gene_visualisation/server.R

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,35 +124,12 @@ single_gene_visualisation_server <- function(id, session_data, session_params, d
124124
# Render Plot and Info
125125
output$SingleGenePlot <- renderPlotly({
126126
req(single_gene_reactives$allow_plot)
127-
p = plotly::ggplotly(single_gene_reactives$plot)
128-
onRender(p, "
129-
function(el, x) {
130-
Plotly.newPlot(el, x.data, x.layout, {
131-
modeBarButtonsToAdd: [{
132-
name: 'Copy to clipboard',
133-
icon: Plotly.Icons.camera, // You can use a more appropriate icon if desired
134-
click: function(gd) {
135-
Plotly.toImage(gd, {format: 'png'}).then(function(url) {
136-
fetch(url)
137-
.then(res => res.blob())
138-
.then(blob => {
139-
navigator.clipboard.write([
140-
new ClipboardItem({ [blob.type]: blob })
141-
]).then(function() {
142-
// Send a success message back to Shiny
143-
Shiny.setInputValue('plot_copied_status', 'SingleGenePlot_success_' + Date.now(), {priority: 'event'});
144-
}).catch(function(err) {
145-
// Send an error message back to Shiny
146-
Shiny.setInputValue('plot_copied_status', 'SingleGenePlot_clipboard_error_' + Date.now(), {priority: 'event'});
147-
});
148-
});
149-
});
150-
}
151-
}],
152-
modeBarButtonsToRemove: ['toImage']
153-
});
154-
}
155-
")
127+
128+
# Use helper function for clipboard functionality
129+
create_clipboard_plotly(
130+
gg_plot = single_gene_reactives$plot,
131+
plot_id = "SingleGenePlot"
132+
)
156133
})
157134

158135
output$SingleGene_Info <- renderText({

0 commit comments

Comments
 (0)