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

[Bug]: row_labels don't match docs in qtable() #698

Closed
3 tasks done
anajens opened this issue Jul 26, 2023 · 1 comment · Fixed by #700
Closed
3 tasks done

[Bug]: row_labels don't match docs in qtable() #698

anajens opened this issue Jul 26, 2023 · 1 comment · Fixed by #700

Comments

@anajens
Copy link
Contributor

anajens commented Jul 26, 2023

What happened?

According to the docs, row_labels from qtable() should be used to modify the analysis rows. In practice it seems to only change the top left header label.

Relevant log output

fivenum3 <- function(x) {
    as.list(fivenum(x))
}
# annotate top left
qtable(ex_adsl, row_vars = "STRATA2", col_vars = "ARM", avar = "AGE", afun = fivenum3,
       row_labels = "ABC")

# error
qtable(ex_adsl, row_vars = "STRATA2", col_vars = "ARM", avar = "AGE", afun = fivenum3,
       row_labels = letters[1:5])
ABC   A: Drug X   B: Placebo   C: Combination
       (N=134)     (N=134)        (N=132)    
—————————————————————————————————————————————
S1                                           
        23.00       24.00          20.00     
        28.00       30.00          30.50     
        34.00       36.00          35.00     
        39.00       40.50          40.00     
        48.00       62.00          69.00     
S2                                           
        21.00       21.00          23.00     
        29.00       29.50          30.00     
        32.00       32.00          34.50     
        38.00       39.50          38.00     
        50.00       58.00          64.00     
> 
> # error
> qtable(ex_adsl, row_vars = "STRATA2", col_vars = "ARM", avar = "AGE", afun = fivenum3,
+        row_labels = letters[1:5])
Error: Error applying analysis function (var - AGE): 'length = 5' in coercion to 'logical(1)'
occured at (row) path: STRATA2[S1]

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.
@gmbecker
Copy link
Collaborator

@anajens Thanks for the report. You are correct. the behavior controlled by row_labels is more complicated than its documentation suggests.

Unlike rtables more generally, qtable tries pretty hard to be "smart" about table appearance, and with respect to this issue, supports the following implicit configurations:

1. no row splitting, single-row-returning afun

Here, the label is used for the row label

> qtable(ex_adsl , col_vars = "ARM", avar = "AGE", afun = mean, row_labels = "ABC")
      A: Drug X   B: Placebo   C: Combination
       (N=134)     (N=134)        (N=132)    
—————————————————————————————————————————————
ABC     33.77       35.43          35.43     

2. row splitting, single-row-returning afun, row_label specified

Here, we use the value label for the most recent split as the row label, automatically, for a more compact table. In this case, the row label (whether default or specified in a single string) is pulled up to the top_left

> qtable(ex_adsl , row_vars = "STRATA2", col_vars = "ARM", avar = "AGE", afun = mean, row_labels = "ABC")
ABC   A: Drug X   B: Placebo   C: Combination
       (N=134)     (N=134)        (N=132)    
—————————————————————————————————————————————
S1      34.10       36.46          35.70     
S2      33.38       34.40          35.24     

3. no row splitting, multi-row-returning afun, row_labels vector specified

Here, the labels should be used as the row labels, and its a bug that they are not

> qtable(ex_adsl, col_vars = "ARM", avar = "AGE", afun = fivenum3, row_labels = letters[1:5])
   A: Drug X   B: Placebo   C: Combination
    (N=134)     (N=134)        (N=132)    
——————————————————————————————————————————
     21.00       21.00          20.00     
     28.00       30.00          30.00     
     33.00       35.00          35.00     
     39.00       40.00          40.00     
     50.00       62.00          69.00     
Warning messages:
1: In !is.null(lbls) && is.na(lbls) :
  'length(x) = 5 > 1' in coercion to 'logical(1)'
2: In !is.null(lbls) && is.na(lbls) :
  'length(x) = 5 > 1' in coercion to 'logical(1)'
3: In !is.null(lbls) && is.na(lbls) :
  'length(x) = 5 > 1' in coercion to 'logical(1)'

4. row splitting present, multi-row returning afun

Here we have a different bug, which puts the vector of row_labels in the topleft:

 qtable(ex_adsl, row_vars = "STRATA2", col_vars = "ARM", avar = "AGE", afun = fivenum3, row_labels = letters[1:5])
a                                           
b                                           
c                                           
d    A: Drug X   B: Placebo   C: Combination
e     (N=134)     (N=134)        (N=132)    
————————————————————————————————————————————
S1                                          
       23.00       24.00          20.00     
       28.00       30.00          30.50     
       34.00       36.00          35.00     
       39.00       40.50          40.00     
       48.00       62.00          69.00     
S2                                          
       21.00       21.00          23.00     
       29.00       29.50          30.00     
       32.00       32.00          34.50     
       38.00       39.50          38.00     
       50.00       58.00          64.00     
Warning messages:
1: In !is.null(lbls) && is.na(lbls) :
  'length(x) = 5 > 1' in coercion to 'logical(1)'
<snip>

I've fixed this locally and will create a PR soon. I went ahead and put the default single rowname back into the top-left in these multi-row cases, which I think is ok, because qtable is supposed to be opinionated:

## with change
> qtable(ex_adsl, row_vars = "STRATA2", col_vars = "ARM", avar = "AGE", afun = fivenum3, row_labels = letters[1:5])
AGE - fivenum3   A: Drug X   B: Placebo   C: Combination
                  (N=134)     (N=134)        (N=132)    
————————————————————————————————————————————————————————
S1                                                      
  a                23.00       24.00          20.00     
  b                28.00       30.00          30.50     
  c                34.00       36.00          35.00     
  d                39.00       40.50          40.00     
  e                48.00       62.00          69.00     
S2                                                      
  a                21.00       21.00          23.00     
  b                29.00       29.50          30.00     
  c                32.00       32.00          34.50     
  d                38.00       39.50          38.00     
  e                50.00       58.00          64.00     

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants