Skip to content

Commit

Permalink
- clean up server.R
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-enzlein committed May 23, 2024
1 parent 3d55480 commit 7b783c8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 67 deletions.
87 changes: 30 additions & 57 deletions components/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ server <- function(input, output, session) {
if (!is.null(defaults$dir)) {
appData$selected_dir <- defaults$dir
message("Dir set from loaded default value.\n")
#info_state("dir_set")
appData$info_state <- "dir_set"
}

Expand Down Expand Up @@ -264,7 +263,7 @@ server <- function(input, output, session) {
### curve and peak plots ####
output$curve <- renderPlotly({
if (appData$show_plot) {
p_curve <<- plotCurves(appData$res,
p_curve <- plotCurves(appData$res,
mzIdx = input$mzTable_rows_selected[1],
errorbars = input$errorbars) +
labs(title = paste0("m/z = ",
Expand All @@ -275,38 +274,30 @@ server <- function(input, output, session) {

ggplotly(p_curve)
} else {
# dummy plot
p_curve <- dummyPlot()

ggplotly(p_curve)
dummyPlot()
}

})

output$peak <- renderPlotly({
if (appData$show_plot) {
p_peak <<- plotPeak(appData$res,
p_peak <- plotPeak(appData$res,
mzIdx = input$mzTable_rows_selected[1],
tol = input$zoom) +
labs(title = NULL)
ggplotly(p_peak)
} else {
# dummy plot
p_peak <- dummyPlot()
ggplotly(p_peak)
dummyPlot()
}

})

### score plot ####
output$scorePlot <- renderPlotly({
if (appData$show_plot) {
p_score <- scorePlot(appData$stats, metric = input$metric)
ggplotly(p_score)
scorePlot(appData$stats, metric = input$metric)
} else {
# dummy plot
p_score <- dummyPlot()
ggplotly(p_score)
dummyPlot()
}
})

Expand All @@ -318,8 +309,8 @@ server <- function(input, output, session) {
idx = seq_along(getAvgSpectra(appData$res)))
ggplotly(p)
} else {
p <- dummyPlot()
ggplotly(p)
dummyPlot()

}

})
Expand Down Expand Up @@ -352,8 +343,7 @@ server <- function(input, output, session) {
#### PCA tab ####
# default plot for PCA
output$pca <- renderPlotly({
p <- dummyPlot()
ggplotly(p)
dummyPlot()
})

observeEvent(input$doPca, {
Expand All @@ -369,54 +359,43 @@ server <- function(input, output, session) {
observeEvent(input$doPca, {
output$pca <- renderPlotly({
if (!is.null(appData$pca)) {
p <- pcaPlot(pca = appData$pca,
conc = factor(getConc(appData$res)),
x = input$pcaX,
y = input$pcaY,
ellipseLevel = as.numeric(input$pcaEllipse),
spots = getSpots(appData$res))

pcaPlot(pca = appData$pca,
conc = factor(getConc(appData$res)),
x = input$pcaX,
y = input$pcaY,
ellipseLevel = as.numeric(input$pcaEllipse),
spots = getSpots(appData$res))
} else {
p <- dummyPlot()
dummyPlot()
}
ggplotly(p)
})
})

output$pcaLoading1 <- renderPlotly({
if (appData$show_plot & !is.null(appData$pca)) {
browser()
p <- loadingsPlot(appData$pca,
pc = input$pcaX,
simple = input$simpleLoadings) +
labs(title = paste("Feature importance for", input$pcaX))
if (input$simpleLoadings) {
p <- p +
labs(title = paste("Top-Features for", input$pcaX))
}
simple = input$simpleLoadings)

ggplotly(p)
return(p)

} else {
p <- dummyPlot()
ggplotly(p)
dummyPlot()

}
})

output$pcaLoading2 <- renderPlotly({
if (appData$show_plot & !is.null(appData$pca)) {
p <- loadingsPlot(appData$pca,
pc = input$pcaY,
simple = input$simpleLoadings) +
labs(title = paste("Feature importance for", input$pcaY))
if (input$simpleLoadings) {
p <- p + labs(title = paste("Top-Features for", input$pcaY))
}
simple = input$simpleLoadings)

ggplotly(p)
return(p)

} else {
p <- dummyPlot()
ggplotly(p)
dummyPlot()
}
})

Expand All @@ -443,35 +422,29 @@ server <- function(input, output, session) {
})
output$hclustPlot <- renderPlotly({
if (appData$show_plot & !is.null(appData$hc)) {
p <- plotClusters(appData$hc, k = input$num_cluster) +
labs(y = "rel. Intensity [arb. u.]",
x = "Log10 Concentration",
title = NULL)
plotClusters(appData$hc, k = input$num_cluster)

return(ggplotly(p))
}
})

output$clustCurvesPlot <- renderPlotly({
if (appData$show_plot & !is.null(appData$hc)) {
show_spinner()

p <- plotTraj(appData$hc, k = input$num_cluster) +
labs(y = "rel. Intensity [arb. u.]",
x = "Log10 Concentration",
title = "Average Trajectories")
p <- plotTraj(appData$hc, k = input$num_cluster)

hide_spinner()
return(ggplotly(p))
return(p)
}
})

output$optNumClust <- renderPlotly({
if (appData$show_plot & !is.null(appData$hc)) {
show_spinner()
p <- plotClusterMetrics(appData$hc) +
p <- plotClusterMetrics(appData$hc)

hide_spinner()
return(ggplotly(p))
return(p)
}
})

Expand Down
2 changes: 2 additions & 0 deletions functions/dummyPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ dummyPlot <- function(label = "Load data\nto display plot") {
y = 1),
aes(x = x, y = y, label = label)) +
geom_text(size = 5)

p <- ggplotly(p)
return(p)
}
14 changes: 11 additions & 3 deletions functions/hclust.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,22 @@ extractLaClusters <- function(models, k = 2) {
plotClusters <- function(models, k) {
model <- models[[k-1]]

p <- latrend::plot(model)

p <- latrend::plot(model) +
labs(y = "rel. Intensity [arb. u.]",
x = "Log10 Concentration",
title = NULL)
p <- ggplotly(p)
return(p)
}

plotTraj <- function(models, k) {
model <- models[[k-1]]

p <- latrend::plotClusterTrajectories(model)
p <- latrend::plotClusterTrajectories(model) +
labs(y = "rel. Intensity [arb. u.]",
x = "Log10 Concentration",
title = "Average Trajectories")
p <- ggplotly(p)

return(p)
}
Expand All @@ -66,5 +73,6 @@ plotClusterMetrics <- function(models) {
scales = "free_y",
nrow = 1) +
scale_x_continuous(breaks = c(2, 5, 10, 15))
p <- ggplotly(p)
return(p)
}
18 changes: 13 additions & 5 deletions functions/plotFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pcaPlot <- function(pca, conc, x, y, ellipseLevel, spots) {
spot = spots
)

df %>%
p <- df %>%
ggplot(aes(x = xax,
y = yax,
col = c,
Expand All @@ -54,6 +54,10 @@ pcaPlot <- function(pca, conc, x, y, ellipseLevel, spots) {
labs(col = "Conc. [M]",
x = paste0(x, " (", exp[xnum], "% expl. var.)"),
y = paste0(y, " (", exp[ynum], "% expl. var.)"))

p <- ggplotly(p)

return(p)
}

loadingsPlot <- function(pca, pc, simple = TRUE, n = 10) {
Expand All @@ -78,21 +82,23 @@ loadingsPlot <- function(pca, pc, simple = TRUE, n = 10) {
geom_hline(yintercept = 0, alpha = 0.75, linetype = "dashed") +
coord_flip() +
labs(x = "m/z [Da]",
y = paste0(pc, " Loading")) +
y = paste0(pc, " Loading"),
title = paste("Top-Features for", pc)) +
theme(legend.position = "none")

return(p)
} else {
p <- df %>%
ggplot(aes(x = mz,
y = val,
col = sign)) +
geom_linerange(aes(ymin = 0, ymax = val), show.legend = FALSE) +
labs(x = "m/z [Da]",
y = paste0(pc, " Loading")) +
y = paste0(pc, " Loading"),
title = paste("Feature importance for", pc)) +
theme(legend.position = "none")
return(p)
}
p <- ggplotly(p)
return(p)
}

glmRegPlot <- function(model, penalty) {
Expand Down Expand Up @@ -379,6 +385,8 @@ scorePlot <- function(stats, metric = c("CRS", "V'", "Z'", "log2FC", "pEC50", "S
labels = c(100, 50, 0, 50 , 100))
}

p <- ggplotly(p)

return(p)
}

2 changes: 0 additions & 2 deletions settings.csv

This file was deleted.

0 comments on commit 7b783c8

Please sign in to comment.