spgarbet / tangram

Table Grammar package for R
66 stars 3 forks source link

Label does not show in table for variable if not associated with class "labelled" attribute #62

Closed kylerove closed 4 years ago

kylerove commented 4 years ago

If I include hmisc and set a label with

label(var) <- "the label" it shows in a table.

If however I do not include the hmisc package and set the label with

attr(var, "label") <- "the label" only the variable name shows in the table output.

With hmisc example, I see output from attributes(var) that shows:

> attributes(recordsDataSingle$sex)
$levels
[1] "Female"    "Male"

$class
[1] "labelled" "factor"  

$label
[1] "Sex"

One (hmisc) approach has a class of "labelled" while the other does not. Any reason we need to rely on hmisc for this?

spgarbet commented 4 years ago

Excellent question. I am trying really hard to make sure their are very few dependencies. I overlooked this one somewhere. It should be easy to fix. I'll see if I can fit it in the next update.

Side thought, what would be really nice is if we had a package "labelled" that did exactly what Hmisc does for labeling carving that out of Hmisc. I'm not picking up another package to manage at this point however.

spgarbet commented 4 years ago

The problem is at line 349 of compile.R. That's really a one line change to fix this.

spgarbet commented 4 years ago

I can't reproduce. What version of R are you using?

R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(tangram)
Loading required package: R6
Loading required package: magrittr
Loading required package: knitr
> 
> pbc$other <- rnorm(length(pbc$bili))
> tangram(drug ~ other, pbc)
==============================================================================
        N     D-penicillamine            placebo            not randomized    
                  (N=154)                (N=158)                (N=106)       
------------------------------------------------------------------------------
other  418  -0.750 *0.018* 0.672  -0.611 *-0.070* 0.806  -0.816 *-0.173* 0.546
==============================================================================
N is the number of non-missing value. ^1 Kruskal-Wallis. ^2 Pearson. ^3 Wilcoxon.Warning message:
In tangram.formula(drug ~ other, pbc) :
  tangram() will require unique id to be specified in the future
> attr(pbc$other, "label") <- "LABEL"
> tangram(drug ~ other, pbc)
==============================================================================
        N     D-penicillamine            placebo            not randomized    
                  (N=154)                (N=158)                (N=106)       
------------------------------------------------------------------------------
LABEL  418  -0.750 *0.018* 0.672  -0.611 *-0.070* 0.806  -0.816 *-0.173* 0.546
==============================================================================
N is the number of non-missing value. ^1 Kruskal-Wallis. ^2 Pearson. ^3 Wilcoxon.Warning message:
In tangram.formula(drug ~ other, pbc) :
  tangram() will require unique id to be specified in the future
> attributes(pbc$other)
$label
[1] "LABEL"

> 
> pbc$other <- factor(sample(c("A", "B"), length(pbc$bili), replace=TRUE))
> tangram(drug ~ other, pbc)
===============================================================
            N   D-penicillamine     placebo      not randomized
                    (N=154)         (N=158)         (N=106)    
---------------------------------------------------------------
other : B  418  0.487   75/154   0.494   78/158  0.434   46/106
===============================================================
N is the number of non-missing value. ^1 Kruskal-Wallis. ^2 Pearson. ^3 Wilcoxon.Warning message:
In tangram.formula(drug ~ other, pbc) :
  tangram() will require unique id to be specified in the future
> attr(pbc$other, "label") <- "LABEL"
> tangram(drug ~ other, pbc)
===============================================================
            N   D-penicillamine     placebo      not randomized
                    (N=154)         (N=158)         (N=106)    
---------------------------------------------------------------
LABEL : B  418  0.487   75/154   0.494   78/158  0.434   46/106
===============================================================
N is the number of non-missing value. ^1 Kruskal-Wallis. ^2 Pearson. ^3 Wilcoxon.Warning message:
In tangram.formula(drug ~ other, pbc) :
  tangram() will require unique id to be specified in the future
> attributes(pbc$other)
$levels
[1] "A" "B"

$class
[1] "factor"

$label
[1] "LABEL"
kylerove commented 4 years ago

3.6.3 with latest github pull of tangram. And I just retested on the project I was working on and don't see the behavior. Weird. I wonder if I just had a stale version of the df I was using. Sorry I should have tested more!

spgarbet commented 4 years ago

Okay. Closing.