Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

273 na rm #278

Merged
merged 5 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions R/aaa-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ parseVariableFromObject <- function(objectName, variableName, keepIfNull = FALSE
#' @return An expression to `eval()`
#' @keywords internal
parseValueToObject <- function(objectName, value) {
if (isOfLength(value, 0)) {
if (isEmpty(value)) {
return(parse(text = paste0(objectName, " <- NULL")))
}
if (isOfType(value, "character")) {
Expand Down Expand Up @@ -113,18 +113,21 @@ parseAddScatterLayer <- function() {
shape = mapLabels$shape
),
size = getAestheticValues(n = 1, selectionKey = plotConfiguration$points$size, position = 0, aesthetic = "size"),
alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$points$alpha, aesthetic = "alpha"),
na.rm = TRUE,
show.legend = TRUE
)
})
}

#' @title parseAddLineLayer
#' @description Create an expression that adds scatter plot layer
#' TODO: create a vignette explaining how argument `lines` in dataMapping is related to this
#' @param type one of "horizontal", "vertical" or "diagonal"
#' Note that for "diagonal", geom_abline is used.
#' `value` of intercept is taken as is for linear scale but corresponds to the log of `value` for log scale.
#' For instance, intercept = c(-1, 0, 1) actually means that the line will go through c(0.1, 1, 10) for x=1
#' because log10(c(-1, 0, 1)) = c(0.1, 1, 10).
#' For instance, intercept = c(-1, 0, 1) with log scale actually means that the line will go through c(0.1, 1, 10)
#' because c(-1, 0, 1) = log10(c(0.1, 1, 10)).
#' @param value value of xintercept or yintercept
#' @param position line position for aesthetic properties
#' @return An expression to `eval()`
Expand All @@ -140,6 +143,7 @@ parseAddLineLayer <- function(type, value, position) {
),
"color=getAestheticValues(n=1,selectionKey=plotConfiguration$lines$color,position=", position, ',aesthetic="color"),',
"linetype=getAestheticValues(n=1,selectionKey=plotConfiguration$lines$linetype,position=", position, ',aesthetic="linetype"),',
"alpha=getAestheticValues(n=1,selectionKey=plotConfiguration$lines$alpha,position=", position, ',aesthetic="alpha"),',
"size=getAestheticValues(n=1,selectionKey=plotConfiguration$lines$size,position=", position, ', aesthetic="size"))'
))
}
Expand All @@ -163,6 +167,8 @@ parseAddUncertaintyLayer <- function() {
# Error bar size uses a ratio of 1/4 to match with point size
size = getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$size, position = 0, aesthetic = "size"),
linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$linetype, aesthetic = "linetype"),
alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, aesthetic = "alpha"),
na.rm = TRUE,
show.legend = TRUE
)
}
Expand Down
6 changes: 4 additions & 2 deletions R/plot-ddiratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ plotDDIRatio <- function(data,
if(residualsVsObserved){
lineOrientation <- "ddiHorizontal"
}
# Include horizontal lines
# Include diagonal or horizontal lines depending on the plot type
for (lineIndex in seq_along(dataMapping$lines)) {
# position correspond to the number of layer lines already added
eval(parseAddLineLayer(lineOrientation, dataMapping$lines[[lineIndex]], lineIndex - 1))
}
if (isOfLength(lineIndex, 0)) {
if (isEmpty(lineIndex)) {
lineIndex <- 0
}
# Add Guest et al. lines to plot
Expand All @@ -60,6 +60,7 @@ plotDDIRatio <- function(data,
na.rm = TRUE,
color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"),
linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"),
alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"),
size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size")
) +
ggplot2::geom_path(
Expand All @@ -68,6 +69,7 @@ plotDDIRatio <- function(data,
na.rm = TRUE,
color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"),
linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"),
alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"),
size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size")
)

Expand Down
7 changes: 6 additions & 1 deletion R/plot-obs-vs-pred.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ plotObsVsPred <- function(data,

plotObject <- plotObject %||% initializePlot(plotConfiguration)

# Add diagonal lines with offset defined in lines of dataMapping
for (lineIndex in seq_along(dataMapping$lines)) {
eval(parseAddLineLayer("diagonal", dataMapping$lines[[lineIndex]], lineIndex - 1))
}
if (isOfLength(lineIndex, 0)) {
if (isEmpty(lineIndex)) {
lineIndex <- 0
}
# Add Smoother if defined
Expand All @@ -57,9 +58,11 @@ plotObsVsPred <- function(data,
method = "loess",
se = FALSE,
formula = "y ~ x",
na.rm = TRUE,
mapping = ggplot2::aes_string(x = mapLabels$x, y = mapLabels$y),
color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"),
linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"),
alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"),
size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size")
)
}
Expand All @@ -71,8 +74,10 @@ plotObsVsPred <- function(data,
method = "lm",
se = FALSE,
formula = "y ~ x",
na.rm = TRUE,
color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"),
linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"),
alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"),
size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size")
)
}
Expand Down
7 changes: 6 additions & 1 deletion R/plot-res-vs-pred.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ plotResVsPred <- function(data,

plotObject <- plotObject %||% initializePlot(plotConfiguration)

# Add horizontal lines with offset defined in lines of dataMapping
for (lineIndex in seq_along(dataMapping$lines)) {
eval(parseAddLineLayer("horizontal", dataMapping$lines[[lineIndex]], lineIndex - 1))
}
if (isOfLength(lineIndex, 0)) {
if (isEmpty(lineIndex)) {
lineIndex <- 0
}
# Add Smoother if defined
Expand All @@ -47,9 +48,11 @@ plotResVsPred <- function(data,
method = "loess",
se = FALSE,
formula = "y ~ x",
na.rm = TRUE,
mapping = ggplot2::aes_string(x = mapLabels$x, y = mapLabels$y),
color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"),
linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"),
alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"),
size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size")
)
}
Expand All @@ -61,8 +64,10 @@ plotResVsPred <- function(data,
method = "lm",
se = FALSE,
formula = "y ~ x",
na.rm = TRUE,
color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"),
linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"),
alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"),
size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size")
)
}
Expand Down
5 changes: 3 additions & 2 deletions man/parseAddLineLayer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions tests/testthat/test-no-na-warnings.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
context("NA data are removed without warnings in scatter plots")

# Test data that include a NA value
testData <- data.frame(
x = c(1,2,3),
y = c(1,2,NA)
)

# To prevent copy/paste, expressions are used in the sequel
plotNames <- c("TimeProfile", "ObsVsPred", "ResVsPred", "PKRatio", "DDIRatio")

# Expression is "<plotname>DataMapping <- <PlotName>DataMapping$new()"
createDataMapping <- parse(text = paste0(tolower(plotNames), "DataMapping <- ", plotNames, "DataMapping$new(x='x',y='y')"))
eval(createDataMapping)

# createPlot expression is "plot<PlotName>(data=testData, dataMapping=<plotname>DataMapping)"
createPlot <- paste0("plot", plotNames, "(data=testData, dataMapping = ", tolower(plotNames), "DataMapping)")
ggplotExpression <- parse(text = paste0('expect_is(', createPlot, ', "ggplot")'))
warningExpression <- parse(text = paste0('expect_silent(', createPlot, ')'))


test_that("Plot is produced as a ggplot object", {
expect_is(addScatter(data=testData), "ggplot")
expect_is(addLine(data=testData), "ggplot")
eval(ggplotExpression)
})

test_that("No warnings were produced when creating/printing the plot", {
expect_silent(addScatter(data=testData))
expect_silent(addLine(data=testData))
eval(warningExpression)
})