@@ -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
0 commit comments