Skip to content

Commit

Permalink
Merge pull request #182 from poissonconsulting/main
Browse files Browse the repository at this point in the history
  • Loading branch information
joethorley authored Dec 13, 2024
2 parents af860ad + 911b9f9 commit ae06126
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 108 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shinyssdtools
Title: ssdtools Shiny App
Version: 0.3.2
Version: 0.3.4
Authors@R:
c(person(given = "Seb",
family = "Dalgarno",
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<!-- NEWS.md is maintained by https://fledge.cynkra.com, contributors should not edit this file -->

# shinyssdtools 0.3.4 (2024-12-12)

- Update plotting functions to match new arguments in ssdtools.
- Code rendering is now simplified as most plot 'add-ons' have been incorporated into `ssd_plot()`.
- French big.mark fixed.

# shinyssdtools 0.3.3 (2024-12-05)

- Added label size to rendered code for prediction plot.

# shinyssdtools 0.3.2 (2024-11-28)

- Adding TESTING.md file for steps on how to manually test app.
Expand Down
89 changes: 50 additions & 39 deletions R/app-server.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ app_server <- function(input, output, session) {
# --- predict and model average
predict_hc <- reactive({
dist <- fit_dist()
req(thresh_rv$percent)
stats::predict(dist, proportion = c(1:99, thresh_rv$percent) / 100)
stats::predict(dist, proportion = unique(c(1:99, thresh_rv$percent)) / 100)
})

transformation <- reactive({
Expand All @@ -319,6 +318,28 @@ app_server <- function(input, output, session) {
trans
})

plot_model_average_xbreaks <- reactive({
req(predict_hc())
req(names_data())
req(input$selectConc)
req(input$selectLabel)
req(thresh_rv$conc)
pred <- predict_hc()
data <- names_data()
conc <- thresh_rv$conc
percent <- thresh_rv$percent
conc_col <- make.names(input$selectConc)
label_col <- ifelse(input$selectLabel == "-none-", NULL, make.names(input$selectLabel))

gp <- ssdtools::ssd_plot(data,
pred = pred,
left = conc_col, label = label_col,
hc = percent / 100
)
xbreaks <- gp_xbreaks(gp)
xbreaks[xbreaks != conc]
})

plot_model_average <- reactive({
req(input$thresh)
req(input$selectColour)
Expand All @@ -329,7 +350,6 @@ app_server <- function(input, output, session) {
req(input$adjustLabel)
req(thresh_rv$percent)
req(thresh_rv$conc)
req(input$xbreaks)

data <- names_data()
pred <- predict_hc()
Expand Down Expand Up @@ -386,7 +406,7 @@ app_server <- function(input, output, session) {

silent_plot(plot_predictions(data, pred,
conc = conc, label = label, colour = colour,
shape = shape, percent = percent, xbreaks = sort(as.numeric(input$xbreaks)),
shape = shape, percent = percent, xbreaks = as.numeric(input$xbreaks),
label_adjust = shift_label, xaxis = append_unit(input$xaxis, input$selectUnit),
yaxis = input$yaxis, title = input$title, xmax = xmax, xmin = xmin,
palette = input$selectPalette, legend_colour = input$legendColour,
Expand Down Expand Up @@ -584,21 +604,11 @@ app_server <- function(input, output, session) {
})

output$uiXbreaks <- renderUI({
req(names_data())
req(thresh_rv$conc)
data <- names_data()
conc <- input$selectConc %>% make.names()

scale <- scales::trans_breaks("identity", function(x) x)
trans <- transformation()
if(trans == "log10")
scale <- scales::trans_breaks("log10", function(x) 10^x)
y <- sort(signif(c(scale(data[[conc]]), thresh_rv$conc), 3))

xbreaks <- plot_model_average_xbreaks()
selectizeInput("xbreaks", tr("ui_xbreaks", trans()),
options = list(create = TRUE, plugins = list("remove_button")),
choices = y,
selected = y,
choices = xbreaks,
selected = xbreaks,
multiple = TRUE
)
})
Expand Down Expand Up @@ -715,12 +725,9 @@ app_server <- function(input, output, session) {
", rescale = ", input$rescale, ")"
)
plot <- paste0(
"ssd_plot_cdf(dist, ylab = '", ylab, "', xlab = '", xlab, "', delta = Inf, average = NA) +
<br/> theme_classic() + <br/> ",
"theme(axis.text = ggplot2::element_text(color = 'black', size = ", text_size, "), <br/>
axis.title = ggplot2::element_text(size = ", text_size, "), <br/>
legend.text = ggplot2::element_text(size = ", text_size, "), <br/>
legend.title = ggplot2::element_text(size = ", text_size, ")) <br/>"
"ssd_plot_cdf(dist, ylab = '", ylab, "', xlab = '", xlab,
"', delta = Inf, <br/>average = NA, theme_classic = TRUE, text_size = ",
text_size, ") <br/>"
)

table <- "ssd_gof(dist) %>% dplyr::mutate_if(is.numeric, ~ signif(., 3))"
Expand All @@ -731,34 +738,38 @@ app_server <- function(input, output, session) {
req(check_fit() == "")
req(check_pred() == "")
req(input$selectLabel)
xmax <- ifelse(is.null(input$xMax), "NA", input$xMax)
xmin <- ifelse(is.null(input$xMin), "NA", input$xMin)
legend.colour <- ifelse(is.null(input$legendColour), "NULL", paste0("'", input$legendColour, "'"))
xmax <- input$xMax
xmin <- input$xMin
xlimits <- ifelse(is.na(xmin) & is.na(xmax), "NULL", paste0("c(", xmin, ", ", xmax, ")"))
legend.colour <- ifelse(is.null(input$legendColour) || input$legendColour == "-none-", "NULL", paste0("'", input$legendColour, "'"))
legend.shape <- ifelse(is.null(input$legendShape) || input$legendShape == "-none-", "NULL", paste0("'", input$legendShape, "'"))
text_size <- input$size3
xlab <- input$xaxis
ylab <- input$yaxis
title <- input$title
big.mark <- ifelse(translation.value$lang == "French", " ", ",")
trans <- transformation()
xbreaks <- input$xbreaks
pred <- paste0("pred <- predict(dist, proportion = c(1:99, ", thresh_rv$percent, ")/100)")
xbreaks <- paste0("c(", paste(xbreaks, collapse = ", "), ")")
pred <- paste0("pred <- predict(dist, proportion = unique(c(1:99, ", thresh_rv$percent, ")/100))")
plot <- paste0(
"ssd_plot(data, pred, left = '", input$selectConc %>% make.names(),
"ssd_plot(data, pred, left = '", make.names(input$selectConc),
"', label = ", code_label(),
", color = ", code_colour(),
", shape = ", code_shape(),
", hc = ", code_hc(),
", ci = FALSE, <br/>shift_x = ", input$adjustLabel,
", color = ", code_colour(),
", <br/>label_size = ", input$sizeLabel3,
", ylab = '", ylab,
"', xlab = '", xlab,
"', ci = FALSE, shift_x = ", input$adjustLabel,
", hc = ", code_hc(),
", <br/>big.mark = '", big.mark,
"', trans = '", trans,
"') + <br/> ggtitle('", title,
"') + <br/> theme_classic() + <br/>",
"theme(axis.text = ggplot2::element_text(color = 'black', size = ", text_size, "), <br/>
axis.title = ggplot2::element_text(size = ", text_size, "), <br/>
legend.text = ggplot2::element_text(size = ", text_size, "), <br/>
legend.title = ggplot2::element_text(size = ", text_size, ")) + <br/>",
"scale_x_continuous(name = '", xlab, "', breaks = c(", paste(xbreaks, collapse = ", "), "), limits = c(", xmin, ", ", xmax, "), labels = comma_signif) + <br/>",
"scale_color_brewer(palette = '", input$selectPalette, "', name = ", legend.colour, ") +<br/>
"', xlimits = ", xlimits,
", xbreaks = ", xbreaks,
", text_size = ", text_size,
", theme_classic = TRUE",
") + <br/> ggtitle('", title,
"') + <br/>scale_color_brewer(palette = '", input$selectPalette, "', name = ", legend.colour, ") +<br/>
scale_shape(name = ", legend.shape, ")"
)
HTML(paste(pred, plot, sep = "<br/>"))
Expand Down
80 changes: 16 additions & 64 deletions R/functions-plots.R
Original file line number Diff line number Diff line change
@@ -1,88 +1,40 @@
label_comma <- function(x, digits = 3, big.mark = ",") {
x <- signif(x, digits = digits)
y <- as.character(x)
bol <- !is.na(x) & as.numeric(x) >= 1000
y[bol] <- stringr::str_replace_all(y[bol], "(\\d{1,1})(\\d{3,3}(?<=\\.|$))", paste0("\\1", big.mark, "\\2"))
y
}

plot_distributions <- function(x, ylab, xlab, text_size) {
gp <- ssdtools::ssd_plot_cdf(x,
ylab = ylab, xlab = xlab,
delta = Inf, average = NA
delta = Inf, average = NA, theme_classic = TRUE, text_size = text_size
)
gp <-
gp +
ggplot2::theme_classic() +
ggplot2::theme(
axis.text = ggplot2::element_text(size = text_size),
axis.title = ggplot2::element_text(size = text_size),
legend.title = ggplot2::element_text(size = text_size),
legend.text = ggplot2::element_text(size = text_size)
)
gp
}

bold_conc <- function(conc, breaks) {
ifelse(breaks == conc, "bold", "plain")
}

gp_xbreaks <- function(gp){
gp_xbreaks <- function(gp) {
breaks <- ggplot2::ggplot_build(gp)$layout$panel_params[[1]]$x$breaks
as.numeric(stats::na.omit(breaks))
sort(signif(as.numeric(stats::na.omit(breaks)), 3))
}

plot_predictions <- function(x, pred, conc, label, colour, shape, percent,
label_adjust, xaxis, yaxis, title, xmin, xmax, palette,
legend_colour, legend_shape, xbreaks, trans, text_size,
legend_colour, legend_shape, xbreaks = NULL, trans, text_size,
label_size, conc_value, big.mark) {
proportion <- percent / 100
if (!length(proportion)) {
proportion <- NULL
}

gp <- ssdtools::ssd_plot(x, pred,
left = conc, label = label, xbreaks = xbreaks, size = label_size,
color = colour, shape = shape, hc = proportion, ci = FALSE,
shift_x = label_adjust %>% as.numeric(),
xlab = xaxis, ylab = yaxis, trans = trans
xlimits <- c(xmin, xmax)
if (is.na(xmin) & is.na(xmax)) {
xlimits <- NULL
}

gp <- ssdtools::ssd_plot(x,
pred = pred,
left = conc, label = label, shape = shape, color = colour,
label_size = label_size, xlab = xaxis, ylab = yaxis,
ci = FALSE, hc = proportion, shift_x = as.numeric(label_adjust),
big.mark = big.mark, trans = trans, xlimits = xlimits,
xbreaks = xbreaks, text_size = text_size, theme_classic = TRUE
) +
ggplot2::scale_x_continuous(
name = xaxis, breaks = xbreaks,
limits = c(xmin, xmax),
labels = function(lab) {
do.call(
expression,
lapply(lab, function(x) {
mark <- label_comma(x, big.mark = big.mark)
if (!is.na(x) & x == conc_value) {
y <- paste0("\n", mark)
} else {
y <- mark
}
y
})
)
}
) +
ggplot2::scale_color_brewer(palette = palette, name = legend_colour) +
ggplot2::scale_shape(name = legend_shape)

# get breaks again for bold face as limits convert some to NA
actual_breaks <- gp_xbreaks(gp)

gp <-
gp +
ggplot2::ggtitle(title) +
ggplot2::theme_classic() +
ggplot2::theme(
axis.text = ggplot2::element_text(color = "black", size = text_size),
axis.text.x = ggplot2::element_text(face = bold_conc(conc_value, actual_breaks)),
axis.title = ggplot2::element_text(size = text_size),
legend.text = ggplot2::element_text(size = text_size),
legend.title = ggplot2::element_text(size = text_size),
)


gp
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ account: poissonconsulting
server: shinyapps.io
hostUrl: https://api.shinyapps.io/v1
appId: 3888468
bundleId: 9170900
bundleId: 9336556
url: https://poissonconsulting.shinyapps.io/shinyssdtools-dev/
2 changes: 1 addition & 1 deletion rsconnect/shinyapps.io/poissonconsulting/shinyssdtools.dcf
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ account: poissonconsulting
server: shinyapps.io
hostUrl: https://api.shinyapps.io/v1
appId: 3888556
bundleId: 9170997
bundleId: 9336595
url: https://poissonconsulting.shinyapps.io/shinyssdtools/
3 changes: 1 addition & 2 deletions scripts/deploy-app.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# install ssdtools
remotes::install_github("poissonconsulting/ssdtools")
install.packages("ssdtools")
# get cran versions of poisson pkgs
pak::pak("err")
pak::pak("universals")
pak::pak("chk")

Expand Down
File renamed without changes.

0 comments on commit ae06126

Please sign in to comment.