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

Fix dataSetToDataFrame #606

Merged
merged 2 commits into from
Sep 8, 2021
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
1 change: 1 addition & 0 deletions R/data-set.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ DataSet <- R6::R6Class(
#' @param ... Rest arguments.
print = function(...) {
private$printClass()
private$printLine("Name", self$name)
private$printLine("X dimension", self$xDimension)
private$printLine("X unit", self$xUnit)
private$printLine("Y dimension", self$yDimension)
Expand Down
57 changes: 32 additions & 25 deletions R/utilities-data-set.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.makeDataFrameColumn <- function(dataSet, property, metaDataName = NULL) {
.makeDataFrameColumn <- function(dataSets, property, metaDataName = NULL) {
columForDataSet <- function(dataSet){
# check length of entry for a certain property of this data set, i.e. if it exists
if (is.null(metaDataName)) {
len <- length(dataSet[[property]])
Expand All @@ -17,6 +18,11 @@
} else {
dataSet[[property]]
}
}

unlist(lapply(dataSets, function(x) {
columForDataSet(x)
}), use.names = FALSE)
}

#' Loads data (typically observed data) from a PKML file and creates a \code{DataSet} from it.
Expand Down Expand Up @@ -55,41 +61,42 @@ saveDataSetToPKML <- function(dataSet, filePath) {
rClr::clrCall(dataRepositoryTask, "SaveDataRepository", dataSet$dataRepository$ref, filePath)
}

#' Converts a list of DataSet objects to a data.frame
#' Converts a list of `DataSet` objects to a data.frame
#'
#' @param dataSets A list of DataSet objects or a single DataSet
#' @param dataSets A list of `DataSet` objects or a single `DataSet`
#'
#' @return DataSet objects as data.frame with columns name, xValue, yValue, yErrorValues,
#' xDimension, xUnit, yDimension, yUnit, yErrorType, yErrorUnit, yMolWeight
#' @return DataSet objects as data.frame with columns name, xValues, yValues, yErrorValues,
#' xDimension, xUnit, yDimension, yUnit, yErrorType, yErrorUnit, molWeight, lloq,
#' and a column for each meta data that is present in any `DataSet`
#' @export
dataSetToDataFrame <- function(dataSets) {
dataSets <- c(dataSets)
validateIsOfType(dataSets, DataSet)

name <- unlist(mapply(.makeDataFrameColumn, dataSets, "name"))
xUnit <- unlist(mapply(.makeDataFrameColumn, dataSets, "xUnit"))
yUnit <- unlist(mapply(.makeDataFrameColumn, dataSets, "yUnit"))
yErrorUnit <- as.character(unlist(mapply(.makeDataFrameColumn, dataSets, "yErrorUnit")))
xDimension <- unlist(mapply(.makeDataFrameColumn, dataSets, "xDimension"))
yDimension <- unlist(mapply(.makeDataFrameColumn, dataSets, "yDimension"))
yErrorType <- as.character(unlist(mapply(.makeDataFrameColumn, dataSets, "yErrorType")))
yMolWeight <- unlist(mapply(.makeDataFrameColumn, dataSets, "molWeight"))
xValue <- unlist(mapply(.makeDataFrameColumn, dataSets, "xValues"))
yValue <- unlist(mapply(.makeDataFrameColumn, dataSets, "yValues"))
yErrorValues <- unlist(mapply(.makeDataFrameColumn, dataSets, "yErrorValues"))

name <- .makeDataFrameColumn(dataSets, "name")
xUnit <- .makeDataFrameColumn(dataSets, "xUnit")
yUnit <- .makeDataFrameColumn(dataSets, "yUnit")
yErrorUnit <- .makeDataFrameColumn(dataSets, "yErrorUnit")
xDimension <- .makeDataFrameColumn(dataSets, "xDimension")
yDimension <- .makeDataFrameColumn(dataSets, "yDimension")
yErrorType <- .makeDataFrameColumn(dataSets, "yErrorType")
molWeight <- .makeDataFrameColumn(dataSets, "molWeight")
xValues <- .makeDataFrameColumn(dataSets, "xValues")
yValues <- .makeDataFrameColumn(dataSets, "yValues")
yErrorValues <- .makeDataFrameColumn(dataSets, "yErrorValues")
lloq <- .makeDataFrameColumn(dataSets, "LLOQ")
df <- data.frame(
name, xValue, yValue, yErrorValues, xDimension, xUnit, yDimension,
yUnit, yErrorType, yErrorUnit, yMolWeight
name, xValues, yValues, yErrorValues, xDimension, xUnit, yDimension,
Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed column names (using plural) and added a LLOQ column.

Copy link
Member

Choose a reason for hiding this comment

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

if feels like you could write a method

makeColums <- function(dataSets, columnName) {
  unlist(lapply(dataSet, function(x){
.makeDatqaFrameColumn(x, columnsName)
}
}

or sthg like this

yUnit, yErrorType, yErrorUnit, molWeight, lloq
)

# get all names of meta data entries from all data sets
metaDataNames <- unique(unlist(sapply(dataSets, function(dataSets) {
return(names(dataSets[["metaData"]]))
})))
metaDataNames <- unique(unlist(lapply(dataSets, function(dataSets) {
names(dataSets[["metaData"]])
}), use.names = FALSE))
# add one column for each one
for (name in metaDataNames) {
col <- unlist(lapply(dataSets, .makeDataFrameColumn, "metaData", metaDataName = name))
col <- .makeDataFrameColumn(dataSets, "metaData", metaDataName = name)
df[[name]] <- col
}

Expand Down Expand Up @@ -127,11 +134,11 @@ loadDataSetsFromExcel <- function(xlsFilePath, importerConfiguration, importAllS
dataImporterTask <- getNetTask("DataImporterTask")
rClr::clrSet(dataImporterTask, "IgnoreSheetNamesAtImport", importAllSheets)
dataRepositories <- rClr::clrCall(dataImporterTask, "ImportExcelFromConfiguration", importerConfiguration$ref, xlsFilePath)
dataSets <- lapply(dataRepositories, function(x){
dataSets <- lapply(dataRepositories, function(x) {
repository <- DataRepository$new(x)
DataSet$new(repository)
})
names(dataSets) <- lapply(dataSets, function(x){
names(dataSets) <- lapply(dataSets, function(x) {
x$name
})

Expand Down
11 changes: 6 additions & 5 deletions man/dataSetToDataFrame.Rd

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

108 changes: 54 additions & 54 deletions tests/testthat/test-utilities-data-set.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ test_that("It can convert an empty data set", {
expect_equal(
dataSetToDataFrame(dataSet),
data.frame(
name = character(0), xValue = logical(0), yValue = logical(0), yErrorValues = logical(0),
name = character(0), xValues = logical(0), yValues = logical(0), yErrorValues = logical(0),
xDimension = character(0), xUnit = character(0), yDimension = character(0),
yUnit = character(0), yErrorType = character(0), yErrorUnit = character(0), yMolWeight = logical(0)
yUnit = character(0), yErrorType = logical(0), yErrorUnit = logical(0), molWeight = logical(0),
lloq = logical(0)
)
)
})
Expand All @@ -27,10 +28,10 @@ test_that("It can convert a data set with xValues and yValues set by setValues,
expect_equal(
dataSetToDataFrame(dataSet),
data.frame(
name = rep("", 5), xValue = dataSet$xValues, yValue = dataSet$yValues, yErrorValues = rep(NA, 5),
name = rep("", 5), xValues = dataSet$xValues, yValues = dataSet$yValues, yErrorValues = rep(NA, 5),
xDimension = rep(dataSet$xDimension, 5), xUnit = rep(dataSet$xUnit, 5),
yDimension = rep(dataSet$yDimension, 5), yUnit = rep(dataSet$yUnit, 5),
yErrorType = rep(NA_character_, 5), yErrorUnit = rep(NA_character_, 5), yMolWeight = rep(NA, 5)
yErrorType = rep(NA, 5), yErrorUnit = rep(NA, 5), molWeight = rep(NA, 5), lloq = rep(NA, 5)
)
)
})
Expand All @@ -39,88 +40,56 @@ test_that("It can convert a data set with only non-empty fields, except for meta
dataSet$setValues(xValues = c(1, 2, 3, 4, 5), yValues = c(10, 20, 30, 40, 50), yErrorValues = c(0, 1, 2, 3, 0))
dataSet$name <- "Data1"
dataSet$molWeight <- 123
dataSet$LLOQ <- 0.2
expect_equal(
dataSetToDataFrame(dataSet),
data.frame(
name = rep(dataSet$name, 5), xValue = dataSet$xValues,
yValue = dataSet$yValues, yErrorValues = dataSet$yErrorValues,
name = rep(dataSet$name, 5), xValues = dataSet$xValues,
yValues = dataSet$yValues, yErrorValues = dataSet$yErrorValues,
xDimension = rep(dataSet$xDimension, 5), xUnit = rep(dataSet$xUnit, 5),
yDimension = rep(dataSet$yDimension, 5), yUnit = rep(dataSet$yUnit, 5),
yErrorType = rep(dataSet$yErrorType, 5), yErrorUnit = rep(dataSet$yErrorUnit, 5),
yMolWeight = rep(dataSet$molWeight, 5)
molWeight = rep(dataSet$molWeight, 5), lloq = rep(dataSet$LLOQ, 5)
)
)
})

test_that("It can convert a list of data sets", {
dataSet$setValues(xValues = c(1, 2, 3, 4, 5), yValues = c(10, 20, 30, 40, 50), yErrorValues = c(0, 1, 2, 3, 0))
dataSet$name <- "Data1"
dataSet$molWeight <- 123
dataSet2 <- DataSet$new()
dataSet2$setValues(xValues = c(6, 7, 8), yValues = c(11, 21, 31))
dataSet2$molWeight <- 456

expect_equal(
dataSetToDataFrame(list(dataSet, dataSet2)),
data.frame(
name = c(rep(dataSet$name, 5), rep("", 3)), xValue = c(dataSet$xValues, dataSet2$xValues),
yValue = c(dataSet$yValues, dataSet2$yValues), yErrorValues = c(dataSet$yErrorValues, rep(NA, 3)),
xDimension = c(rep(dataSet$xDimension, 5), rep(dataSet2$xDimension, 3)),
xUnit = c(rep(dataSet$xUnit, 5), rep(dataSet2$xUnit, 3)),
yDimension = c(rep(dataSet$yDimension, 5), rep(dataSet2$yDimension, 3)),
yUnit = c(rep(dataSet$yUnit, 5), rep(dataSet2$yUnit, 3)),
yErrorType = c(rep(dataSet$yErrorType, 5), rep(NA_character_, 3)),
yErrorUnit = c(rep(dataSet$yErrorUnit, 5), rep(NA_character_, 3)),
yMolWeight = c(rep(dataSet$molWeight, 5), rep(dataSet2$molWeight, 3))
)
)
})

test_that("It can convert a single data set with meta data", {
dataSet$setValues(xValues = c(1, 2, 3, 4, 5), yValues = c(10, 20, 30, 40, 50))
dataSet$name <- "Data1"
dataSet$molWeight <- 123
dataSet$addMetaData("City", "Rome")

test_that("It can convert a data set with metaData", {
dataSet$addMetaData("Organ", "Blood")
expect_equal(
dataSetToDataFrame(dataSet),
data.frame(
name = rep(dataSet$name, 5), xValue = dataSet$xValues, yValue = dataSet$yValues, yErrorValues = rep(NA, 5),
name = rep(dataSet$name, 5), xValues = dataSet$xValues,
yValues = dataSet$yValues, yErrorValues = dataSet$yErrorValues,
xDimension = rep(dataSet$xDimension, 5), xUnit = rep(dataSet$xUnit, 5),
yDimension = rep(dataSet$yDimension, 5), yUnit = rep(dataSet$yUnit, 5),
yErrorType = rep(NA_character_, 5), yErrorUnit = rep(NA_character_, 5), yMolWeight = rep(123, 5),
City = rep("Rome", 5)
yErrorType = rep(dataSet$yErrorType, 5), yErrorUnit = rep(dataSet$yErrorUnit, 5),
molWeight = rep(dataSet$molWeight, 5), lloq = rep(dataSet$LLOQ, 5),
Organ = rep("Blood", 5)
)
)
})

test_that("It can convert a list of data sets with meta data", {
dataSet$setValues(xValues = c(1, 2, 3, 4, 5), yValues = c(10, 20, 30, 40, 50), yErrorValues = c(0, 1, 2, 3, 0))
dataSet$name <- "Data1"
dataSet$molWeight <- 123
dataSet$addMetaData("City", "Rome")
test_that("It can convert a list of data sets", {
dataSet2 <- DataSet$new()
dataSet2$setValues(xValues = c(6, 7, 8), yValues = c(11, 21, 31))
dataSet2$molWeight <- 456
dataSet2$addMetaData("City", "Berlin")
dataSet2$addMetaData("ZIP", "10245")
dataSet2$addMetaData("Country", "Germany")
dataSet2$addMetaData("Compartment", "Plasma")

expect_equal(
dataSetToDataFrame(list(dataSet, dataSet2)),
data.frame(
name = c(rep(dataSet$name, 5), rep("", 3)), xValue = c(dataSet$xValues, dataSet2$xValues),
yValue = c(dataSet$yValues, dataSet2$yValues), yErrorValues = c(dataSet$yErrorValues, rep(NA, 3)),
name = c(rep(dataSet$name, 5), rep("", 3)), xValues = c(dataSet$xValues, dataSet2$xValues),
yValues = c(dataSet$yValues, dataSet2$yValues), yErrorValues = c(dataSet$yErrorValues, rep(NA, 3)),
xDimension = c(rep(dataSet$xDimension, 5), rep(dataSet2$xDimension, 3)),
xUnit = c(rep(dataSet$xUnit, 5), rep(dataSet2$xUnit, 3)),
yDimension = c(rep(dataSet$yDimension, 5), rep(dataSet2$yDimension, 3)),
yUnit = c(rep(dataSet$yUnit, 5), rep(dataSet2$yUnit, 3)),
yErrorType = c(rep(dataSet$yErrorType, 5), rep(NA_character_, 3)),
yErrorUnit = c(rep(dataSet$yErrorUnit, 5), rep(NA_character_, 3)),
yMolWeight = c(rep(dataSet$molWeight, 5), rep(dataSet2$molWeight, 3)),
City = c(rep("Rome", 5), rep("Berlin", 3)),
ZIP = c(rep(NA_character_, 5), rep("10245", 3)),
Country = c(rep(NA_character_, 5), rep("Germany", 3))
molWeight = c(rep(dataSet$molWeight, 5), rep(dataSet2$molWeight, 3)),
lloq = c(rep(dataSet$LLOQ, 5), rep(NA, 3)),
Organ = c(rep("Blood", 5), rep(NA, 3)), Compartment = c(rep(NA, 5), rep("Plasma", 3))
)
)
})
Expand Down Expand Up @@ -191,3 +160,34 @@ test_that("it can load when loading from file with one sheet without
expect_true(isOfType(dataSets, DataSet))
expect_equal(length(dataSets), 4)
})

test_that("it can convert DataSets loaded from excel to data.frame", {
dataSets <- loadDataSetsFromExcel(xlsFilePath = xlsFilePath, importerConfiguration = importerConfiguration, importAllSheets = TRUE)
dataSetsFrame <- dataSetToDataFrame(dataSets)
expect_equal(names(dataSetsFrame), c(
"name",
"xValues",
"yValues",
"yErrorValues",
"xDimension",
"xUnit",
"yDimension",
"yUnit",
"yErrorType",
"yErrorUnit",
"molWeight",
"lloq",
"Source",
"Sheet",
"Study Id",
"Organ",
"Compartment",
"Species",
"Gender",
"Molecule",
"Route",
"Subject Id",
"Dose",
"Group Id"
))
})