Skip to content
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
42 changes: 39 additions & 3 deletions R/class-plotdata-scatter.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ newScatterPD <- function(.dt = data.table::data.table(),
value = character(),
useGradientColorscale = FALSE,
overlayValues = veupathUtils::BinList(),
idColumn = character(),
returnPointIds = logical(),
correlationMethod = character(),
sampleSizes = logical(),
completeCases = logical(),
Expand All @@ -15,6 +17,8 @@ newScatterPD <- function(.dt = data.table::data.table(),
variables = variables,
useGradientColorscale = useGradientColorscale,
overlayValues = overlayValues,
idColumn = idColumn,
returnPointIds = returnPointIds,
sampleSizes = sampleSizes,
completeCases = completeCases,
evilMode = evilMode,
Expand All @@ -31,6 +35,16 @@ newScatterPD <- function(.dt = data.table::data.table(),
group <- veupathUtils::findColNamesFromPlotRef(variables, 'overlay')
panel <- findPanelColName(veupathUtils::findVariableSpecFromPlotRef(variables, 'facet1'),
veupathUtils::findVariableSpecFromPlotRef(variables, 'facet2'))
# If we ask for the point ids, ensure the column is present. Otherwise set to null.
if (returnPointIds) {
if (!is.null(idColumn) && idColumn %in% names(.dt)) {
idCol <- idColumn
} else {
stop("idColumn not found or not supplied. Supply proper idColumn if returnPointIds is TRUE.")
}
} else {
idCol <- NULL
}

dtForCorr <- data.table::as.data.table(.pd)

Expand All @@ -40,7 +54,7 @@ newScatterPD <- function(.dt = data.table::data.table(),
series <- collapseByGroup(.pd, group = 'overlayMissingData', panel)
.pd$overlayMissingData <- NULL
series$overlayMissingData <- NULL
data.table::setnames(series, c(panel, 'seriesX', 'seriesY', 'seriesGradientColorscale'))
data.table::setnames(series, c(panel, 'seriesX', 'seriesY', 'seriesGradientColorscale', idCol))

# corr results w gradient, same as w/o groups so set group to NULL
dtForCorr[[group]] <- NULL
Expand All @@ -50,7 +64,7 @@ newScatterPD <- function(.dt = data.table::data.table(),
} else {
#series data w/o gradient
series <- collapseByGroup(.pd, group, panel)
data.table::setnames(series, c(group, panel, 'seriesX', 'seriesY'))
data.table::setnames(series, c(group, panel, 'seriesX', 'seriesY', idCol))

# corr results w/o gradient
if (correlationMethod != 'none') {
Expand Down Expand Up @@ -113,6 +127,7 @@ newScatterPD <- function(.dt = data.table::data.table(),

} else if (value == 'density') {

# Note, density is not implemented in production code.
density <- groupDensity(.pd, NULL, x, group, panel)
.pd <- density
veupathUtils::logWithTime('Kernel density estimate calculated from raw data.', verbose)
Expand Down Expand Up @@ -193,6 +208,9 @@ validateScatterPD <- function(.scatter, verbose) {
#' @param sampleSizes boolean indicating if sample sizes should be computed
#' @param completeCases boolean indicating if complete cases should be computed
#' @param evilMode String indicating how evil this plot is ('strataVariables', 'allVariables', 'noVariables')
#' @param idColumn character indicating the column name of the id variable in data
#' @param returnPointIds boolean indicating if any point ids should be returned with the scatterplot data.
#' This value will only be used when idColumn is present.
#' @param verbose boolean indicating if timed logging is desired
#' @return data.table plot-ready data
#' @examples
Expand Down Expand Up @@ -243,6 +261,8 @@ scattergl.dt <- function(data,
evilMode = c('noVariables', 'allVariables', 'strataVariables'),
collectionVariablePlotRef = NULL,
computedVariableMetadata = NULL,
idColumn = NULL,
returnPointIds = c(FALSE, TRUE),
verbose = c(TRUE, FALSE)) {

if (!inherits(variables, 'VariableMetadataList')) stop("The `variables` argument must be a VariableMetadataList object.")
Expand All @@ -252,6 +272,7 @@ scattergl.dt <- function(data,
completeCases <- veupathUtils::matchArg(completeCases)
evilMode <- veupathUtils::matchArg(evilMode)
verbose <- veupathUtils::matchArg(verbose)
returnPointIds <- veupathUtils::matchArg(returnPointIds)

if (!'data.table' %in% class(data)) {
data.table::setDT(data)
Expand All @@ -276,7 +297,14 @@ scattergl.dt <- function(data,
if (!yVM@dataType@value %in% c('NUMBER', 'INTEGER') & value != 'raw') {
stop('Trend lines can only be provided for numeric dependent axes.')
}
}
}

# If returnPointIds is TRUE, require that the idColumn is present in the data.
if (returnPointIds) {
if (is.null(idColumn) || !(idColumn %in% names(data))) {
stop("idColumn not found or not supplied. Supply proper idColumn if returnPointIds is TRUE.")
}
}

groupVM <- veupathUtils::findVariableMetadataFromPlotRef(variables, 'overlay')
# Decide if we should use a gradient colorscale
Expand All @@ -302,6 +330,8 @@ scattergl.dt <- function(data,
useGradientColorscale = useGradientColorscale,
overlayValues = overlayValues,
correlationMethod = correlationMethod,
idColumn = idColumn,
returnPointIds = returnPointIds,
sampleSizes = sampleSizes,
completeCases = completeCases,
inferredVarAxis = 'y',
Expand Down Expand Up @@ -357,6 +387,8 @@ scattergl.dt <- function(data,
#' @param sampleSizes boolean indicating if sample sizes should be computed
#' @param completeCases boolean indicating if complete cases should be computed
#' @param evilMode String indicating how evil this plot is ('strataVariables', 'allVariables', 'noVariables')
#' @param idColumn character indicating the column name of the id variable in data
#' @param returnPointIds boolean indicating if any point ids should be returned with the scatterplot data.
#' @param verbose boolean indicating if timed logging is desired
#' @return character name of json file containing plot-ready data
#' @examples
Expand Down Expand Up @@ -405,6 +437,8 @@ scattergl <- function(data,
sampleSizes = c(TRUE, FALSE),
completeCases = c(TRUE, FALSE),
evilMode = c('noVariables', 'allVariables', 'strataVariables'),
idColumn = NULL,
returnPointIds = c(FALSE, TRUE),
verbose = c(TRUE, FALSE)) {

verbose <- veupathUtils::matchArg(verbose)
Expand All @@ -417,6 +451,8 @@ scattergl <- function(data,
sampleSizes = sampleSizes,
completeCases = completeCases,
evilMode = evilMode,
idColumn = idColumn,
returnPointIds = returnPointIds,
verbose = verbose)

outFileName <- writeJSON(.scatter, evilMode, 'scattergl', verbose)
Expand Down
18 changes: 16 additions & 2 deletions R/class-plotdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ newPlotdata <- function(.dt = data.table(),
#make sure lat, lon, geoAgg vars are valid plot References
variables = NULL,
useGradientColorscale = FALSE,
overlayValues = veupathUtils::BinList(),
overlayValues = veupathUtils::BinList(),
idColumn = character(),
returnPointIds = logical(),
sampleSizes = logical(),
completeCases = logical(),
inferredVarAxis = c('y', 'x'),
Expand Down Expand Up @@ -45,6 +47,17 @@ newPlotdata <- function(.dt = data.table(),
lat <- veupathUtils::findColNamesFromPlotRef(variables, 'latitude')
lon <- veupathUtils::findColNamesFromPlotRef(variables, 'longitude')

# If we ask for the point ids, ensure the column is present. Otherwise set to null.
if (!is.null(returnPointIds) && returnPointIds && length(idColumn) > 0) {
if (idColumn %in% names(.dt) && nrow(.dt) == uniqueN(.dt[[idColumn]])) {
idCol <- idColumn
} else {
idCol <- NULL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Should this maybe throw an error? Seems like there will be downstream trouble if we asked for point IDs and there's no column to get them from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea! I'll add it.

}
} else {
idCol <- NULL
}

isEvil <- ifelse(evilMode %in% c('allVariables', 'strataVariables'), TRUE, FALSE)
collectionVarMetadata <- veupathUtils::findCollectionVariableMetadata(variables)
isOverlayCollection <- ifelse(is.null(collectionVarMetadata), FALSE, ifelse(collectionVarMetadata@plotReference@value == 'overlay', TRUE, FALSE))
Expand Down Expand Up @@ -88,7 +101,7 @@ newPlotdata <- function(.dt = data.table(),
panel <- c(facet1, facet2)
}

myCols <- c(x, y, z, lat, lon, group, panel, geo)
myCols <- c(x, y, z, lat, lon, group, panel, geo, idCol)
.dt <- .dt[, myCols, with=FALSE]
veupathUtils::logWithTime('Identified facet intersections.', verbose)

Expand Down Expand Up @@ -204,6 +217,7 @@ newPlotdata <- function(.dt = data.table(),
if (!is.null(lon)) { .dt[[lon]] <- updateType(.dt[[lon]], 'NUMBER')}
if (!is.null(group)) { .dt[[group]] <- updateType(.dt[[group]], groupType) }
if (!is.null(panel)) { .dt[[panel]] <- updateType(.dt[[panel]], 'STRING') }
if (!is.null(idCol)) { .dt[[idCol]] <- updateType(.dt[[idCol]], 'STRING') }
veupathUtils::logWithTime('Base data types updated for all columns as necessary.', verbose)

if (!is.null(group)) {
Expand Down
6 changes: 6 additions & 0 deletions man/scattergl.Rd

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

7 changes: 7 additions & 0 deletions man/scattergl.dt.Rd

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

Loading
Loading