-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotPCAVariancePlot.R
105 lines (76 loc) · 3.21 KB
/
plotPCAVariancePlot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#'
#' Plot module for variance plot
#'
library(shiny)
library(ggplot2)
library(scatterplot3d)
source("widgetVisualsEditor.R")
source("widgetDownloadablePlot.R")
source("defaultParameters.R")
plotPCAVariancePlotUI <- function(id) {
ns <- NS(id)
return(downloadablePlotOutput(ns("plot")))
}
plotPCAVariancePlotSettingsUI <- function(id) {
ns <- NS(id)
return(bsCollapse(
bsCollapsePanel(optionalDataText("General settings"),
value = "generalsettings",
generalPlotSettingsInput(ns("plot.settings"),
legend.color = F,
legend.shape = F))
))
}
plotPCAVariancePlot.save <- function(pca, plot.settings, format, filename){
plot.settings <- plotSettingsSetNA(plot.settings,
PlotSettings(width = default.plot.width,
height = default.plot.height,
dpi = default.plot.dpi,
scale = 1,
title = "Principal component variances",
subtitle = ""))
width <- plot.settings@width
height <- plot.settings@height
dpi <- plot.settings@dpi
scale <- plot.settings@scale
title <- plot.settings@title
subtitle <- plot.settings@subtitle
plot.y.label <- sprintf("Relative variance (to %s)", paste(sum(pca$var)))
data <- pca$var
data$index <- seq_len(nrow(data))
#p <- ggplot(pca$var, aes(x=factor(rownames(pca$var), levels = rownames(pca$var)), y=var.relative)) + geom_point()
p <- ggplot(data, aes(x=index, y=var.relative)) + geom_point()
p <- p + labs(x = "Principal component", y = plot.y.label, title = title, subtitle = subtitle)
ggsave(filename, p, width = width / 72, height = height / 72, dpi = dpi, scale = 0.75 / scale, device = format)
return(plot.settings)
}
plotPCAVariancePlot_ <- function(input,
output,
session,
pca,
xauto = NULL) {
plot.settings <- generalPlotSettings("plot.settings")
downloadablePlot("plot",
plot.settings = plot.settings,
exprplot = function(plot.settings, format, filename)
{
return(plotPCAVariancePlot.save(pca(), plot.settings, format, filename))
})
# xauto exporter that allows triggering of exporting data from code
xautovars <- reactiveValues(xautocounter = 1)
if(!is.null(xauto)) {
observeEvent(xauto(), {
filename <- xauto()$filename
format <- xauto()$format
plotPCAVariancePlot.save(pca(), plot.settings(), format, filename)
xautovars$xautocounter <- xautovars$xautocounter + 1
})
}
return(reactive({ xautovars$xautocounter }))
}
plotPCAVariancePlot <- function(id, pca, xauto = NULL) {
return(callModule(plotPCAVariancePlot_,
id,
pca = pca,
xauto = xauto))
}