################################################################################
# CHANGE LOG (last 20 changes)
# 07.07.2023: Fixed Error in !is.na(.gData) && !is.null(.gData) in coercion to 'logical(1)
# 10.09.2022: Compacted the gui. Fixed narrow dropdowns. Removed destroy workaround.
# 11.05.2021: Removed unused imports. Fixes issue#19.
# 04.07.2020: Fixed no visible binding for variables.
# 02.05.2020: Added language support.
# 06.09.2019: Fixed narrow dropdown with hidden argument ellipsize = "none".
# 24.02.2019: Compacted and tweaked gui for tcltk.
# 17.02.2019: Fixed Error in if (svalue(savegui_chk)) { : argument is of length zero (tcltk)
# 13.07.2017: Fixed issue with button handlers.
# 13.07.2017: Fixed expanded 'gexpandgroup'.
# 07.07.2017: Replaced 'droplist' with 'gcombobox'.
# 07.07.2017: Removed argument 'border' for 'gbutton'.
# 29.04.2016: 'Save as' textbox expandable.
# 25.04.2016: First version.
#' @title Plot Profile Slope
#'
#' @description
#' GUI simplifying the creation of plots from slope data.
#'
#' @details Select a dataset to plot. Plot slope by sample.
#' Automatic plot titles can be replaced by custom titles.
#' A name for the result is automatically suggested.
#' The resulting plot can be saved as either a plot object or as an image.
#' @param env environment in which to search for data frames and save result.
#' @param savegui logical indicating if GUI settings should be saved in the environment.
#' @param debug logical indicating printing debug information.
#' @param parent widget to get focus when finished.
#'
#' @return TRUE
#'
#' @export
#'
#' @importFrom utils help str
#' @importFrom ggplot2 ggplot aes_string geom_point theme element_text labs
#' xlab ylab theme_gray theme_bw theme_linedraw theme_light theme_dark
#' theme_minimal theme_classic theme_void geom_errorbar position_dodge
#'
#' @seealso \url{https://ggplot2.tidyverse.org/} for details on plot settings.
plotSlope_gui <- function(env = parent.frame(), savegui = NULL, debug = FALSE, parent = NULL) {
# Global variables.
.gData <- NULL
.gDataName <- NULL
.gPlot <- NULL
.theme <- c(
"theme_grey()", "theme_bw()", "theme_linedraw()",
"theme_light()", "theme_dark()", "theme_minimal()",
"theme_classic()", "theme_void()"
)
val_obj <- NULL
# Language ------------------------------------------------------------------
# Get this functions name from call.
fnc <- as.character(match.call()[[1]])
if (debug) {
print(paste("IN:", fnc))
}
# Default strings.
strWinTitle <- "Plot slope"
strChkGui <- "Save GUI settings"
strBtnHelp <- "Help"
strFrmDataset <- "Dataset"
strLblDataset <- "Slope dataset:"
strDrpDataset <- "<Select dataset>"
strLblSamples <- "samples"
strFrmOptions <- "Options"
strChkOverride <- "Override automatic titles"
strLblTitlePlot <- "Plot title:"
strLblTitleX <- "X title:"
strLblTitleY <- "Y title:"
strLblTheme <- "Plot theme:"
strExpLabels <- "X labels"
strLblSize <- "Text size (pts):"
strLblAngle <- "Angle:"
strLblJustification <- "Justification (v/h):"
strFrmPlot <- "Plot slope data"
strBtnPlot <- "Slope vs. Sample"
strBtnProcessing <- "Processing..."
strFrmSave <- "Save as"
strLblSave <- "Name for result:"
strBtnSaveObject <- "Save as object"
strBtnSaveImage <- "Save as image"
strBtnObjectSaved <- "Object saved"
strLblMainTitle <- "Profile slope"
strLblXTitle <- "Sample"
strLblYTitle <- "Slope"
strMsgNotDf <- "Data set must be a data.frame!"
strMsgTitleError <- "Error"
# Get strings from language file.
dtStrings <- getStrings(gui = fnc)
# If language file is found.
if (!is.null(dtStrings)) {
# Get language strings, use default if not found.
strtmp <- dtStrings["strWinTitle"]$value
strWinTitle <- ifelse(is.na(strtmp), strWinTitle, strtmp)
strtmp <- dtStrings["strChkGui"]$value
strChkGui <- ifelse(is.na(strtmp), strChkGui, strtmp)
strtmp <- dtStrings["strBtnHelp"]$value
strBtnHelp <- ifelse(is.na(strtmp), strBtnHelp, strtmp)
strtmp <- dtStrings["strFrmDataset"]$value
strFrmDataset <- ifelse(is.na(strtmp), strFrmDataset, strtmp)
strtmp <- dtStrings["strLblDataset"]$value
strLblDataset <- ifelse(is.na(strtmp), strLblDataset, strtmp)
strtmp <- dtStrings["strDrpDataset"]$value
strDrpDataset <- ifelse(is.na(strtmp), strDrpDataset, strtmp)
strtmp <- dtStrings["strLblSamples"]$value
strLblSamples <- ifelse(is.na(strtmp), strLblSamples, strtmp)
strtmp <- dtStrings["strFrmOptions"]$value
strFrmOptions <- ifelse(is.na(strtmp), strFrmOptions, strtmp)
strtmp <- dtStrings["strChkOverride"]$value
strChkOverride <- ifelse(is.na(strtmp), strChkOverride, strtmp)
strtmp <- dtStrings["strLblTitlePlot"]$value
strLblTitlePlot <- ifelse(is.na(strtmp), strLblTitlePlot, strtmp)
strtmp <- dtStrings["strLblTitleX"]$value
strLblTitleX <- ifelse(is.na(strtmp), strLblTitleX, strtmp)
strtmp <- dtStrings["strLblTitleY"]$value
strLblTitleY <- ifelse(is.na(strtmp), strLblTitleY, strtmp)
strtmp <- dtStrings["strLblTheme"]$value
strLblTheme <- ifelse(is.na(strtmp), strLblTheme, strtmp)
strtmp <- dtStrings["strExpLabels"]$value
strExpLabels <- ifelse(is.na(strtmp), strExpLabels, strtmp)
strtmp <- dtStrings["strLblSize"]$value
strLblSize <- ifelse(is.na(strtmp), strLblSize, strtmp)
strtmp <- dtStrings["strLblAngle"]$value
strLblAngle <- ifelse(is.na(strtmp), strLblAngle, strtmp)
strtmp <- dtStrings["strLblJustification"]$value
strLblJustification <- ifelse(is.na(strtmp), strLblJustification, strtmp)
strtmp <- dtStrings["strFrmPlot"]$value
strFrmPlot <- ifelse(is.na(strtmp), strFrmPlot, strtmp)
strtmp <- dtStrings["strBtnPlot"]$value
strBtnPlot <- ifelse(is.na(strtmp), strBtnPlot, strtmp)
strtmp <- dtStrings["strBtnProcessing"]$value
strBtnProcessing <- ifelse(is.na(strtmp), strBtnProcessing, strtmp)
strtmp <- dtStrings["strFrmSave"]$value
strFrmSave <- ifelse(is.na(strtmp), strFrmSave, strtmp)
strtmp <- dtStrings["strLblSave"]$value
strLblSave <- ifelse(is.na(strtmp), strLblSave, strtmp)
strtmp <- dtStrings["strBtnSaveObject"]$value
strBtnSaveObject <- ifelse(is.na(strtmp), strBtnSaveObject, strtmp)
strtmp <- dtStrings["strBtnSaveImage"]$value
strBtnSaveImage <- ifelse(is.na(strtmp), strBtnSaveImage, strtmp)
strtmp <- dtStrings["strBtnObjectSaved"]$value
strBtnObjectSaved <- ifelse(is.na(strtmp), strBtnObjectSaved, strtmp)
strtmp <- dtStrings["strLblMainTitle"]$value
strLblMainTitle <- ifelse(is.na(strtmp), strLblMainTitle, strtmp)
strtmp <- dtStrings["strLblXTitle"]$value
strLblXTitle <- ifelse(is.na(strtmp), strLblXTitle, strtmp)
strtmp <- dtStrings["strLblYTitle"]$value
strLblYTitle <- ifelse(is.na(strtmp), strLblYTitle, strtmp)
strtmp <- dtStrings["strMsgNotDf"]$value
strMsgNotDf <- ifelse(is.na(strtmp), strMsgNotDf, strtmp)
strtmp <- dtStrings["strMsgTitleError"]$value
strMsgTitleError <- ifelse(is.na(strtmp), strMsgTitleError, strtmp)
}
# WINDOW ####################################################################
# Main window.
w <- gwindow(title = strWinTitle, visible = FALSE)
# Runs when window is closed.
addHandlerUnrealize(w, handler = function(h, ...) {
# Save GUI state.
.saveSettings()
# Focus on parent window.
if (!is.null(parent)) {
focus(parent)
}
# Destroy window.
return(FALSE)
})
# Vertical main group.
gv <- ggroup(
horizontal = FALSE,
spacing = 1,
use.scrollwindow = FALSE,
container = w,
expand = TRUE
)
# Help button group.
gh <- ggroup(container = gv, expand = FALSE, fill = "both")
savegui_chk <- gcheckbox(text = strChkGui, checked = FALSE, container = gh)
addSpring(gh)
help_btn <- gbutton(text = strBtnHelp, container = gh)
addHandlerChanged(help_btn, handler = function(h, ...) {
# Open help page for function.
print(help(fnc, help_type = "html"))
})
# FRAME 0 ###################################################################
f0 <- gframe(
text = strFrmDataset,
horizontal = TRUE,
spacing = 1,
container = gv
)
glabel(text = strLblDataset, container = f0)
samples_lbl <- glabel(
text = paste(" 0 ", strLblSamples, sep = ""),
container = f0
)
dataset_drp <- gcombobox(
items = c(
strDrpDataset,
listObjects(
env = env,
obj.class = "data.frame"
)
),
selected = 1,
editable = FALSE,
container = f0,
ellipsize = "none",
expand = TRUE,
fill = "x"
)
addHandlerChanged(dataset_drp, handler = function(h, ...) {
val_obj <- svalue(dataset_drp)
# Check if suitable.
requiredCol <- c("Sample.Name", "Group", "Slope", "Lower", "Upper")
ok <- checkDataset(
name = val_obj, reqcol = requiredCol,
env = env, parent = w, debug = debug
)
if (ok) {
# Load or change components.
# Get data.
.gData <<- get(val_obj, envir = env)
.gDataName <<- val_obj
# Suggest name.
svalue(f5_save_edt) <- paste(val_obj, "_ggplot", sep = "")
svalue(samples_lbl) <- paste(" ",
length(unique(.gData$Sample.Name)),
" ", strLblSamples,
sep = ""
)
# Enable buttons.
enabled(plot_sample_btn) <- TRUE
} else {
# Reset components.
.gData <<- NULL
svalue(f5_save_edt) <- ""
svalue(dataset_drp, index = TRUE) <- 1
svalue(samples_lbl) <- paste(" 0 ", strLblSamples, sep = "")
}
})
# FRAME 1 ###################################################################
f1 <- gframe(
text = strFrmOptions,
horizontal = FALSE,
spacing = 1,
container = gv
)
titles_chk <- gcheckbox(
text = strChkOverride,
checked = FALSE, container = f1
)
addHandlerChanged(titles_chk, handler = function(h, ...) {
.updateGui()
})
titles_group <- ggroup(
container = f1, spacing = 1, horizontal = FALSE,
expand = TRUE, fill = TRUE
)
# Legends
glabel(text = strLblTitlePlot, container = titles_group, anchor = c(-1, 0))
title_edt <- gedit(expand = TRUE, fill = TRUE, container = titles_group)
glabel(text = strLblTitleX, container = titles_group, anchor = c(-1, 0))
x_title_edt <- gedit(expand = TRUE, fill = TRUE, container = titles_group)
glabel(text = strLblTitleY, container = titles_group, anchor = c(-1, 0))
y_title_edt <- gedit(expand = TRUE, fill = TRUE, container = titles_group)
f1g2 <- glayout(container = f1)
f1g2[1, 1] <- glabel(text = strLblTheme, anchor = c(-1, 0), container = f1g2)
f1g2[1, 2] <- f1_theme_drp <- gcombobox(
items = .theme,
selected = 1,
container = f1g2,
ellipsize = "none"
)
# BUTTON ####################################################################
plot_sample_btn <- gbutton(
text = strBtnPlot, container = gv,
expand = TRUE, fill = TRUE
)
tooltip(plot_sample_btn) <- "Plot slope by sample and group"
addHandlerChanged(plot_sample_btn, handler = function(h, ...) {
enabled(plot_sample_btn) <- FALSE
.plot()
enabled(plot_sample_btn) <- TRUE
})
# FRAME 5 ###################################################################
f5 <- gframe(
text = strFrmSave,
horizontal = TRUE,
spacing = 1,
container = gv
)
glabel(text = strLblSave, container = f5)
f5_save_edt <- gedit(container = f5, expand = TRUE, fill = TRUE)
f5_save_btn <- gbutton(text = strBtnSaveObject, container = f5)
f5_ggsave_btn <- gbutton(text = strBtnSaveImage, container = f5)
addHandlerClicked(f5_save_btn, handler = function(h, ...) {
val_name <- svalue(f5_save_edt)
# Change button.
blockHandlers(f5_save_btn)
svalue(f5_save_btn) <- strBtnProcessing
unblockHandlers(f5_save_btn)
enabled(f5_save_btn) <- FALSE
# Save data.
saveObject(
name = val_name, object = .gPlot,
parent = w, env = env, debug = debug
)
# Change button.
blockHandlers(f5_save_btn)
svalue(f5_save_btn) <- strBtnObjectSaved
unblockHandlers(f5_save_btn)
})
addHandlerChanged(f5_ggsave_btn, handler = function(h, ...) {
val_name <- svalue(f5_save_edt)
# Save data.
ggsave_gui(
ggplot = .gPlot, name = val_name,
parent = w, env = env, savegui = savegui, debug = debug
)
})
# ADVANCED OPTIONS ##########################################################
# FRAME 4 ###################################################################
e4 <- gexpandgroup(
text = strExpLabels,
horizontal = FALSE,
container = f1
)
# Start collapsed.
visible(e4) <- FALSE
grid4 <- glayout(container = e4)
grid4[1, 1] <- glabel(text = strLblSize, container = grid4)
grid4[1, 2] <- e4_size_edt <- gedit(text = "8", width = 4, container = grid4)
grid4[1, 3] <- glabel(text = strLblAngle, container = grid4)
grid4[1, 4] <- e4_angle_spb <- gspinbutton(
from = 0, to = 360, by = 1,
value = 0,
container = grid4
)
grid4[2, 1] <- glabel(text = strLblJustification, container = grid4)
grid4[2, 2] <- e4_vjust_spb <- gspinbutton(
from = 0, to = 1, by = 0.1,
value = 0.5,
container = grid4
)
grid4[2, 3] <- e4_hjust_spb <- gspinbutton(
from = 0, to = 1, by = 0.1,
value = 0.5,
container = grid4
)
# FUNCTIONS #################################################################
.plot <- function() {
# Get values.
val_titles <- svalue(titles_chk)
val_title <- svalue(title_edt)
val_xtitle <- svalue(x_title_edt)
val_ytitle <- svalue(y_title_edt)
val_angle <- as.numeric(svalue(e4_angle_spb))
val_vjust <- as.numeric(svalue(e4_vjust_spb))
val_hjust <- as.numeric(svalue(e4_hjust_spb))
val_size <- as.numeric(svalue(e4_size_edt))
val_theme <- svalue(f1_theme_drp)
if (debug) {
print("val_title")
print(val_title)
print("val_xtitle")
print(val_xtitle)
print("val_ytitle")
print(val_ytitle)
print("val_angle")
print(val_angle)
print("val_vjust")
print(val_vjust)
print("val_hjust")
print(val_hjust)
print("val_size")
print(val_size)
print("str(.gData)")
print(str(.gData))
print("val_theme")
print(val_theme)
}
if (is.data.frame(.gData)) {
# Create default tit
if (val_titles) {
maintitle <- val_title
xtitle <- val_xtitle
ytitle <- val_ytitle
} else {
maintitle <- strLblMainTitle
xtitle <- strLblXTitle
ytitle <- strLblYTitle
}
# Create plot with groups.
gp <- ggplot(
data = .gData,
aes_string(colour = "Group", y = "Slope", x = "Sample.Name")
)
# Apply theme.
gp <- gp + eval(parse(text = val_theme))
# Add point layer.
gp <- gp + geom_point(position = position_dodge(width = 0.3))
# Define the top and bottom of the errorbars
limits <- aes_string(ymax = "Upper", ymin = "Lower")
# Add error bars.
gp <- gp + geom_errorbar(limits,
width = 0.2,
position = position_dodge(width = 0.3)
)
# Add titles etc.
gp <- gp + theme(axis.text.x = element_text(
angle = val_angle,
hjust = val_hjust,
vjust = val_vjust,
size = val_size
))
gp <- gp + labs(title = maintitle)
gp <- gp + xlab(xtitle)
gp <- gp + ylab(ytitle)
# plot.
print(gp)
# Change save button.
svalue(f5_save_btn) <- strBtnSaveObject
enabled(f5_save_btn) <- TRUE
# Store in global variable.
.gPlot <<- gp
} else {
gmessage(
msg = strMsgNotDf,
title = strMsgTitleError,
icon = "error"
)
}
}
# INTERNAL FUNCTIONS ########################################################
.updateGui <- function() {
# Override titles.
val <- svalue(titles_chk)
if (val) {
enabled(titles_group) <- TRUE
} else {
enabled(titles_group) <- FALSE
}
}
.loadSavedSettings <- function() {
# First check status of save flag.
if (!is.null(savegui)) {
svalue(savegui_chk) <- savegui
enabled(savegui_chk) <- FALSE
if (debug) {
print("Save GUI status set!")
}
} else {
# Load save flag.
if (exists(".strvalidator_plotSlope_gui_savegui", envir = env, inherits = FALSE)) {
svalue(savegui_chk) <- get(".strvalidator_plotSlope_gui_savegui", envir = env)
}
if (debug) {
print("Save GUI status loaded!")
}
}
if (debug) {
print(svalue(savegui_chk))
}
# Then load settings if true.
if (svalue(savegui_chk)) {
if (exists(".strvalidator_plotSlope_gui_title", envir = env, inherits = FALSE)) {
svalue(title_edt) <- get(".strvalidator_plotSlope_gui_title", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_title_chk", envir = env, inherits = FALSE)) {
svalue(titles_chk) <- get(".strvalidator_plotSlope_gui_title_chk", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_x_title", envir = env, inherits = FALSE)) {
svalue(x_title_edt) <- get(".strvalidator_plotSlope_gui_x_title", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_y_title", envir = env, inherits = FALSE)) {
svalue(y_title_edt) <- get(".strvalidator_plotSlope_gui_y_title", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_xlabel_size", envir = env, inherits = FALSE)) {
svalue(e4_size_edt) <- get(".strvalidator_plotSlope_gui_xlabel_size", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_xlabel_angle", envir = env, inherits = FALSE)) {
svalue(e4_angle_spb) <- get(".strvalidator_plotSlope_gui_xlabel_angle", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_xlabel_justh", envir = env, inherits = FALSE)) {
svalue(e4_hjust_spb) <- get(".strvalidator_plotSlope_gui_xlabel_justh", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_xlabel_justv", envir = env, inherits = FALSE)) {
svalue(e4_vjust_spb) <- get(".strvalidator_plotSlope_gui_xlabel_justv", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_theme", envir = env, inherits = FALSE)) {
svalue(f1_theme_drp) <- get(".strvalidator_plotSlope_gui_theme", envir = env)
}
if (debug) {
print("Saved settings loaded!")
}
}
}
.saveSettings <- function() {
# Then save settings if true.
if (svalue(savegui_chk)) {
assign(x = ".strvalidator_plotSlope_gui_savegui", value = svalue(savegui_chk), envir = env)
assign(x = ".strvalidator_plotSlope_gui_title", value = svalue(title_edt), envir = env)
assign(x = ".strvalidator_plotSlope_gui_title_chk", value = svalue(titles_chk), envir = env)
assign(x = ".strvalidator_plotSlope_gui_x_title", value = svalue(x_title_edt), envir = env)
assign(x = ".strvalidator_plotSlope_gui_y_title", value = svalue(y_title_edt), envir = env)
assign(x = ".strvalidator_plotSlope_gui_xlabel_size", value = svalue(e4_size_edt), envir = env)
assign(x = ".strvalidator_plotSlope_gui_xlabel_angle", value = svalue(e4_angle_spb), envir = env)
assign(x = ".strvalidator_plotSlope_gui_xlabel_justh", value = svalue(e4_hjust_spb), envir = env)
assign(x = ".strvalidator_plotSlope_gui_xlabel_justv", value = svalue(e4_vjust_spb), envir = env)
assign(x = ".strvalidator_plotSlope_gui_theme", value = svalue(f1_theme_drp), envir = env)
} else { # or remove all saved values if false.
if (exists(".strvalidator_plotSlope_gui_savegui", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_savegui", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_title", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_title", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_title_chk", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_title_chk", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_x_title", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_x_title", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_y_title", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_y_title", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_xlabel_size", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_xlabel_size", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_xlabel_angle", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_xlabel_angle", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_xlabel_justh", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_xlabel_justh", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_xlabel_justv", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_xlabel_justv", envir = env)
}
if (exists(".strvalidator_plotSlope_gui_theme", envir = env, inherits = FALSE)) {
remove(".strvalidator_plotSlope_gui_theme", envir = env)
}
if (debug) {
print("Settings cleared!")
}
}
if (debug) {
print("Settings saved!")
}
}
# END GUI ###################################################################
# Load GUI settings.
.loadSavedSettings()
.updateGui()
# Show GUI.
visible(w) <- TRUE
focus(w)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.