Skip to content

Commit

Permalink
limits for x and y axis can be set
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Nov 5, 2024
1 parent 36290b5 commit 89bcf64
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 80 deletions.
8 changes: 8 additions & 0 deletions bs/R/OperationsModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ OperatorEditorSidebar <- function(id) {
actionButton(NS(id, "as.real"), "convert to real number",
title = "Convert a column of the dataset or an intermediate variable to a real number. For example as.real(ColName)",
class = "add-button"),
actionButton(NS(id, "as.fact"), "convert to factors",
title = "Convert a column of the dataset or an intermediate variable to a factor. For example as.fact(ColName)",
class = "add-button"),
class = "boxed-output"
),
div(
Expand Down Expand Up @@ -710,6 +713,11 @@ OperationEditorServer <- function(id, data) {
updated_text <- paste(current_text, "as.real(", sep = " ")
updateTextAreaInput(session, "editable_code", value = updated_text)
})
observeEvent(input$as.fact, {
current_text <- input$editable_code
updated_text <- paste(current_text, "as.fact(", sep = " ")
updateTextAreaInput(session, "editable_code", value = updated_text)
})
observeEvent(input$dnorm, {
current_text <- input$editable_code
updated_text <- paste(current_text, "dnorm(", sep = " ")
Expand Down
Binary file added bs/R/Rplots.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion bs/R/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ server <- function(input, output, session) {
}
tryCatch(
{
system(paste("rm -r ", file))
unlink(file)
},
warning = function(warn) {
showNotification(paste("A warning occurred: ", conditionMessage(warn)), duration = 0)
Expand Down
2 changes: 1 addition & 1 deletion bs/R/check_ast.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ allowed_fcts <- function() {
"sum", "diff", "min", "max", "scale",
"c", "vector", "length", "matrix", "~",
"get_rows", "get_cols",
"as.char", "as.int", "as.real"
"as.char", "as.int", "as.real", "as.fact"
)
}

Expand Down
1 change: 1 addition & 0 deletions bs/R/loadLibraries.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ library(MASS)
library(Matrix)
library(shinyjs)
library(equatiomatic)
# TODO: add missing libraries to the Dockerfile

if (Sys.getenv("RUN_MODE") == "SERVER") {
library(COMELN)
Expand Down
51 changes: 39 additions & 12 deletions bs/R/plottingInternally.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
addFacet <- function(p, facetVar, facetMode) {
if (facetMode == "facet_wrap") {
return(p + facet_wrap(. ~ .data[[facetVar]], scales = "free"))
} else if (facetMode == "facet_grid") {
return(p + facet_grid(. ~ .data[[facetVar]], scales = "free"))
}
}

addInterval <- function(p, df, xCol, yCol, xMin, xMax, yMin, yMax) {
x <- df[ , xCol]
y <- df[ , yCol]
if (is.numeric(x)) {
p <- p + scale_x_continuous(limits = c(xMin, xMax))
} else {
choices <- unique(x)
xStart <- which(xMin == choices)
xEnd <- which(xMax == choices)
p <- p + scale_x_discrete(limits = choices[xStart:xEnd])
}
if (is.numeric(y)) {
p <- p + scale_y_continuous(limits = c(yMin, yMax))
} else {
choices <- unique(y)
yStart <- which(yMin == choices)
yEnd <- which(yMax == choices)
p <- p + scale_y_discrete(limits = choices[yStart:yEnd])
}
return(p)
}

annotateDF <- function(p, method, level = 2) {
pB <- ggplot_build(p)
df <- pB$data[[1]]
Expand Down Expand Up @@ -107,18 +137,11 @@ calcParams <- function(df, formula, method) {
}
}

addFacet <- function(p, facetVar, facetMode) {
if (facetMode == "facet_wrap") {
return(p + facet_wrap(. ~ .data[[facetVar]], scales = "free"))
} else if (facetMode == "facet_grid") {
return(p + facet_grid(. ~ .data[[facetVar]], scales = "free"))
}
}

DotplotFct <- function(df, x, y, xLabel, yLabel,
fitMethod,
colourVar, legendTitleColour,
colourTheme, facetMode, facetVar, k = 10) {
colourTheme, facetMode, facetVar, k = 10,
xMin, xMax, yMin, yMax) {
# create plot
# ==========================================
aes <- aes(x = .data[[x]], y = .data[[y]])
Expand All @@ -144,7 +167,7 @@ DotplotFct <- function(df, x, y, xLabel, yLabel,

p <- p + xlab(xLabel)
p <- p + ylab(yLabel)

p <- addInterval(p, df, x, y, xMin, xMax, yMin, yMax)
if (colourVar != "") {
p <- p + guides(colour = guide_legend(title = legendTitleColour))
p <- p + scale_color_brewer(palette = colourTheme)
Expand Down Expand Up @@ -221,7 +244,8 @@ DotplotFct <- function(df, x, y, xLabel, yLabel,
BoxplotFct <- function(df, x, y, xLabel, yLabel,
fillVar, legendTitleFill, fillTheme,
colourVar, legendTitleColour,
colourTheme, facetMode, facetVar) {
colourTheme, facetMode, facetVar,
xMin, xMax, yMin, yMax) {
aes <- aes(x = .data[[x]], y = .data[[y]])
aesColour <- NULL
aesFill <- NULL
Expand Down Expand Up @@ -252,6 +276,7 @@ BoxplotFct <- function(df, x, y, xLabel, yLabel,
p <- p + guides(colour = guide_legend(title = legendTitleColour))
p <- p + scale_fill_brewer(palette = fillTheme)
p <- p + scale_color_brewer(palette = colourTheme)
p <- addInterval(p, df, x, y, xMin, xMax, yMin, yMax)
if (facetMode != "none") {
p <- addFacet(p, facetVar, facetMode)
}
Expand All @@ -260,7 +285,8 @@ BoxplotFct <- function(df, x, y, xLabel, yLabel,

LineplotFct <- function(df, x, y, xLabel, yLabel,
colourVar, legendTitleColour,
colourTheme, facetMode, facetVar) {
colourTheme, facetMode, facetVar,
xMin, xMax, yMin, yMax) {
aes <- aes(x = .data[[x]], y = .data[[y]])
aesColour <- NULL
p <- NULL
Expand All @@ -283,6 +309,7 @@ LineplotFct <- function(df, x, y, xLabel, yLabel,
p <- p + ylab(yLabel)
p <- p + guides(colour = guide_legend(title = legendTitleColour))
p <- p + scale_color_brewer(palette = colourTheme)
p <- addInterval(p, df, x, y, xMin, xMax, yMin, yMax)
if (facetMode != "none") {
p <- addFacet(p, facetVar, facetMode)
}
Expand Down
34 changes: 34 additions & 0 deletions bs/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ as.real <- function(v) {
return(as.numeric(v))
}

as.fact <- function(v) {
return(as.factor(v))
}

# Split groups
split <- function(df, cols, levels) {
df_res <- NULL
Expand All @@ -269,3 +273,33 @@ split <- function(df, cols, levels) {
}
return(df_res)
}

# check and print notifications
print_noti <- function(expr, message) {
if (!expr) {
showNotification(message)
}
req(expr)
}

# Check axis limits
check_axis_limits <- function(col, min, max) {
if (is.numeric(col)) {
if (!is.numeric(min) || !is.numeric(max)) {
stop("Found invalid axis limits")
}
if (max <= min) {
stop("Found invalid axis limits: max <= min")
}
return()
} else {
choices <- unique(col)
if (!(min %in% choices) || !(max %in% choices)) {
stop("Found invalid axis limits")
}
if (which(max == choices) <= which(min == choices)) {
stop("Found invalid axis limits. The max value is found before the min value")
}
return()
}
}
Loading

0 comments on commit 89bcf64

Please sign in to comment.