Skip to content

RS-18683: Keep span attributes and some fixes for multiple stats#81

Merged
chschan merged 7 commits intomasterfrom
RS-18683
May 20, 2025
Merged

RS-18683: Keep span attributes and some fixes for multiple stats#81
chschan merged 7 commits intomasterfrom
RS-18683

Conversation

@chschan
Copy link
Contributor

@chschan chschan commented May 18, 2025

This PR is a repeat of #61 but the includes some additional fixes for tables with multiple statistics. They are included here instead of verbs because verbs explicitly does not handle 3d-arrays. Also, for the tidy tables work we are currently working on, we are treating the multiple statistics in the 1-d table with multiple stats like any other column, and flattening multiple statistics



if (!inherits(data, "QTable") && !is.null(attr(data, "span")))
attr(data, "span") <- NULL
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add additional check here because it is fairly easy to generate a hardship error with a span attribute if it is not subscripted properly (which usually does occur because NETs are removed by default)

@chschan chschan requested a review from jrwishart May 18, 2025 23:12
Comment on lines +1399 to +1424
is.qtable <- inherits(data, "QTable")
old.span <- attr(data, "span", exact = TRUE)
new.data <- array(data, dim = c(1, nrow(data), ncol(data)), dimnames = list("", rownames(data), colnames(data)))
data <- CopyAttributes(new.data, data)
if (is.qtable)
class(data) <- c(class(data), "QTable")
if (!is.null(old.span))
attr(data, "span") <- list(rows = old.span$columns, columns = old.span$rows)

} else if (length(dim(data)) > 2)
{
# 2-dimensional table with multiple statistics
is.qtable <- inherits(data, "QTable")
old.span <- attr(data, "span", exact = TRUE)
new.data <- aperm(data, c(2, 1, 3))
else
new.data <- t(data)
data <- CopyAttributes(new.data, data)
attr(data, "questions") <- rev(attr(data, "questions"))
data <- CopyAttributes(new.data, data)
if (is.qtable)
class(data) <- c(class(data), "QTable")
attr(data, "questions") <- rev(attr(data, "questions"))
if (!is.null(old.span))
attr(data, "span") <- list(rows = old.span$columns, columns = old.span$rows)
} else {
# Attributes handled by verbs (for QTables)
data <- t(data)
if (!inherits(data, "QTable"))
attr(data, "questions") <- rev(attr(data, "questions"))
Copy link
Contributor

Choose a reason for hiding this comment

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

This operation makes me a bit nervious. In most of this code block if input is a QTable, the data is restructured however the QTable class is retained. So subscripting will still call the QTable subscript? Or is this safe to do here? I fear this will break subscripting?

Copy link
Contributor

Choose a reason for hiding this comment

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

I do notice on a second viewing that data <- CopyAttributes(new.data, data) was already there. So the QTable attributes would've been copied. This might be safe to do here? I can't recall where in the workflow this operation occurs. Does this occur after the statistics testing information has been extracted? And also after the subscripting (removal of rows/columns?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I tried this in Displayr with my ad hoc R server. In all 3 cases the output is still an array. The operation is a transpose, so its actually safer than subscripting because it never drops any dimensions.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was more concerned with reshaping the data but keeping the attributes. The subscripting relies on attributes heavily and might do the wrong operation on the reshaped data. e.g.

new.data <- array(data, dim = c(1, nrow(data), ncol(data)), dimnames = list("", rownames(data), colnames(data)))
            data <- CopyAttributes(new.data, data)

Copy link
Contributor Author

@chschan chschan May 20, 2025

Choose a reason for hiding this comment

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

I'm not entirely sure what your example is trying to show. But CopyAttributes has by default attr.to.not.copy = c("dimnames", "names", "row.names", "dim", "class", "levels")

@chschan chschan merged commit 37e2794 into master May 20, 2025
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants