xtable does not print out units of a variable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

xtable does not print out units of a variable

Shawn Way-4
I have a dataframe that contains units using the units package.  Unfortunately, I really need the units for reporting.  I'm assuming that's because the data is in a class units and xtable doesn't know what to do with this.

The following is a MWE:

    library(xtable)
    library(units)
    data <- data.frame(x=c(as_units(12,"ft")))
    xtable(data)

    % latex table generated in R 3.5.1 by xtable 1.8-2 package
    % Wed Jul 18 17:31:44 2018
    \begin{table}[ht]
    \centering
    \begin{tabular}{rr}
      \hline
     & x \\
      \hline
    1 & 12.00 \\
      \hline
    \end{tabular}
    \end{table}

What I'm looking for is the line

    1 & 12.00 \\

to be

    1 & 12.00 $ft$\\

Can someone point me in the correct direction to make this happen?  Since units are used extensively in engineering calculations, being able to handle this class would be extremely beneficial to engineers that are using R with knitr to generate engineering documents.

Shawn Way

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: xtable does not print out units of a variable

Jeff Newmiller
On Wed, 18 Jul 2018, Shawn Way wrote:

> I have a dataframe that contains units using the units package.
> Unfortunately, I really need the units for reporting.  I'm assuming
> that's because the data is in a class units and xtable doesn't know what
> to do with this.

If you want a bug or feature in a CONTRIBUTED PACKAGE, then you need to
communicate with the maintainer:

maintainer( "xtable" )

Do keep in mind that they almost always volunteer their time, so be
patient, and consider figuring out what code changes they need to make so
it will work.

More below.

> The following is a MWE:
>
>    library(xtable)
>    library(units)
>    data <- data.frame(x=c(as_units(12,"ft")))
>    xtable(data)
>
>    % latex table generated in R 3.5.1 by xtable 1.8-2 package
>    % Wed Jul 18 17:31:44 2018
>    \begin{table}[ht]
>    \centering
>    \begin{tabular}{rr}
>      \hline
>     & x \\
>      \hline
>    1 & 12.00 \\
>      \hline
>    \end{tabular}
>    \end{table}
>
> What I'm looking for is the line
>
>    1 & 12.00 \\
>
> to be
>
>    1 & 12.00 $ft$\\
>
> Can someone point me in the correct direction to make this happen?
> Since units are used extensively in engineering calculations, being able
> to handle this class would be extremely beneficial to engineers that are
> using R with knitr to generate engineering documents.
>
> Shawn Way

I do want to emphasize that R focuses on consistency among elements within
columns, not rows, so putting the units into the body of the table is kind
of visually redundant in most cases. Consider:

####################
library(xtable)
library(units)
#> udunits system database from /usr/share/xml/udunits
data <- data.frame(x=c(as_units(c(12,13),"ft")))
datax <- xtable(data)
names(datax) <- paste0( names(datax)[1]
                       , " ($"
                       , deparse_unit( datax[[1]] )
                       , "$)"
                       )
datax
#> % latex table generated in R 3.4.4 by xtable 1.8-2 package
#> % Wed Jul 18 19:13:29 2018
#> \begin{table}[ht]
#> \centering
#> \begin{tabular}{rr}
#>   \hline
#>  & x (\$ft\$) \\
#>   \hline
#> 1 & 12.00 \\
#>   2 & 13.00 \\
#>    \hline
#> \end{tabular}
#> \end{table}

#' Created on 2018-07-18 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0).
####################

If you have some kind of summary table with different units on each row,
then you will probably arrive at that information a single-row, many
column data frame. I usually transpose this into a three-column data frame
with a description column, a value column, and a units column. I don't use
the units package so have never tried to adapt it to that process.

> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: xtable does not print out units of a variable

Shawn Way-4
Thank you for the example you posted.  I'll try to make a go of it from there.

I understand and actually use columns with consistent information, however, it's the reporting of the variables and their units that is the crux of the situation.  The units package is extremely useful in automatic conversion between units, something necessary for us engineering folks.

Thank you kindly!

Shawn Way, PE

-----Original Message-----
From: Jeff Newmiller <[hidden email]>
Sent: Wednesday, July 18, 2018 9:26 PM
To: Shawn Way <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] xtable does not print out units of a variable

On Wed, 18 Jul 2018, Shawn Way wrote:

> I have a dataframe that contains units using the units package.
> Unfortunately, I really need the units for reporting.  I'm assuming
> that's because the data is in a class units and xtable doesn't know
> what to do with this.

If you want a bug or feature in a CONTRIBUTED PACKAGE, then you need to communicate with the maintainer:

maintainer( "xtable" )

Do keep in mind that they almost always volunteer their time, so be patient, and consider figuring out what code changes they need to make so it will work.

More below.

> The following is a MWE:
>
>    library(xtable)
>    library(units)
>    data <- data.frame(x=c(as_units(12,"ft")))
>    xtable(data)
>
>    % latex table generated in R 3.5.1 by xtable 1.8-2 package
>    % Wed Jul 18 17:31:44 2018
>    \begin{table}[ht]
>    \centering
>    \begin{tabular}{rr}
>      \hline
>     & x \\
>      \hline
>    1 & 12.00 \\
>      \hline
>    \end{tabular}
>    \end{table}
>
> What I'm looking for is the line
>
>    1 & 12.00 \\
>
> to be
>
>    1 & 12.00 $ft$\\
>
> Can someone point me in the correct direction to make this happen?
> Since units are used extensively in engineering calculations, being
> able to handle this class would be extremely beneficial to engineers
> that are using R with knitr to generate engineering documents.
>
> Shawn Way

I do want to emphasize that R focuses on consistency among elements within columns, not rows, so putting the units into the body of the table is kind of visually redundant in most cases. Consider:

####################
library(xtable)
library(units)
#> udunits system database from /usr/share/xml/udunits data <- data.frame(x=c(as_units(c(12,13),"ft")))
datax <- xtable(data)
names(datax) <- paste0( names(datax)[1]
                       , " ($"
                       , deparse_unit( datax[[1]] )
                       , "$)"
                       )
datax
#> % latex table generated in R 3.4.4 by xtable 1.8-2 package #> % Wed Jul 18 19:13:29 2018 #> \begin{table}[ht] #> \centering #> \begin{tabular}{rr}
#>   \hline
#>  & x (\$ft\$) \\
#>   \hline
#> 1 & 12.00 \\
#>   2 & 13.00 \\
#>    \hline
#> \end{tabular}
#> \end{table}

#' Created on 2018-07-18 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0).
####################

If you have some kind of summary table with different units on each row, then you will probably arrive at that information a single-row, many column data frame. I usually transpose this into a three-column data frame with a description column, a value column, and a units column. I don't use the units package so have never tried to adapt it to that process.

> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: xtable does not print out units of a variable

R help mailing list-2
  I wonder if the problem may be that xtable is not designed to deal with an object of class "object"

> dat1 <- data.frame(  x=c(as_units(12,"ft")))
> str(dat1)
'data.frame':    1 obs. of  1 variable:
 $ x:Object of class units:
 atomic  12
  ..- attr(*, "units")=List of 2
  .. ..$ numerator  : chr "ft"
  .. ..$ denominator: chr
  .. ..- attr(*, "class")= chr "symbolic_units"



   On Thursday, July 19, 2018, 7:53:09 a.m. EDT, Shawn Way <[hidden email]> wrote:  
 
 Thank you for the example you posted.  I'll try to make a go of it from there.

I understand and actually use columns with consistent information, however, it's the reporting of the variables and their units that is the crux of the situation.  The units package is extremely useful in automatic conversion between units, something necessary for us engineering folks.

Thank you kindly!

Shawn Way, PE

-----Original Message-----
From: Jeff Newmiller <[hidden email]>
Sent: Wednesday, July 18, 2018 9:26 PM
To: Shawn Way <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] xtable does not print out units of a variable

On Wed, 18 Jul 2018, Shawn Way wrote:

> I have a dataframe that contains units using the units package.
> Unfortunately, I really need the units for reporting.  I'm assuming
> that's because the data is in a class units and xtable doesn't know
> what to do with this.

If you want a bug or feature in a CONTRIBUTED PACKAGE, then you need to communicate with the maintainer:

maintainer( "xtable" )

Do keep in mind that they almost always volunteer their time, so be patient, and consider figuring out what code changes they need to make so it will work.

More below.

> The following is a MWE:
>
>    library(xtable)
>    library(units)
>    data <- data.frame(x=c(as_units(12,"ft")))
>    xtable(data)
>
>    % latex table generated in R 3.5.1 by xtable 1.8-2 package
>    % Wed Jul 18 17:31:44 2018
>    \begin{table}[ht]
>    \centering
>    \begin{tabular}{rr}
>      \hline
>    & x \\
>      \hline
>    1 & 12.00 \\
>      \hline
>    \end{tabular}
>    \end{table}
>
> What I'm looking for is the line
>
>    1 & 12.00 \\
>
> to be
>
>    1 & 12.00 $ft$\\
>
> Can someone point me in the correct direction to make this happen?
> Since units are used extensively in engineering calculations, being
> able to handle this class would be extremely beneficial to engineers
> that are using R with knitr to generate engineering documents.
>
> Shawn Way

I do want to emphasize that R focuses on consistency among elements within columns, not rows, so putting the units into the body of the table is kind of visually redundant in most cases. Consider:

####################
library(xtable)
library(units)
#> udunits system database from /usr/share/xml/udunits data <- data.frame(x=c(as_units(c(12,13),"ft")))
datax <- xtable(data)
names(datax) <- paste0( names(datax)[1]
                      , " ($"
                      , deparse_unit( datax[[1]] )
                      , "$)"
                      )
datax
#> % latex table generated in R 3.4.4 by xtable 1.8-2 package #> % Wed Jul 18 19:13:29 2018 #> \begin{table}[ht] #> \centering #> \begin{tabular}{rr}
#>  \hline
#>  & x (\$ft\$) \\
#>  \hline
#> 1 & 12.00 \\
#>  2 & 13.00 \\
#>    \hline
#> \end{tabular}
#> \end{table}

#' Created on 2018-07-18 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0).
####################

If you have some kind of summary table with different units on each row, then you will probably arrive at that information a single-row, many column data frame. I usually transpose this into a three-column data frame with a description column, a value column, and a units column. I don't use the units package so have never tried to adapt it to that process.

> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The    .....      .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.      ##.#.  Live Go...
                                      Live:  OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.      #.O#.  with
/Software/Embedded Controllers)              .OO#.      .OO#.  rocks...1k

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.  
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: xtable does not print out units of a variable

R help mailing list-2
In reply to this post by Shawn Way-4
Hi,

Just to throw in an alternative possibility, you may want to look at Frank's Hmisc package on CRAN:

  https://cran.r-project.org/web/packages/Hmisc/index.html

and note the label(), latex() and units() functions therein.

They may or may not work for what you are looking to do here.

As Jeff has noted, you may need to do some pre-processing of the output to the LaTeX table to get what you want, before calling any of the LaTeX output functions, in the absence of direct support for specific classes of object, such as 'units'. Keep in mind, that all of the output to LaTeX is character based, so you can create any formatted character string you need, then generate the LaTeX output.

Regards,

Marc Schwartz


> On Jul 19, 2018, at 7:52 AM, Shawn Way <[hidden email]> wrote:
>
> Thank you for the example you posted.  I'll try to make a go of it from there.
>
> I understand and actually use columns with consistent information, however, it's the reporting of the variables and their units that is the crux of the situation.  The units package is extremely useful in automatic conversion between units, something necessary for us engineering folks.
>
> Thank you kindly!
>
> Shawn Way, PE
>
> -----Original Message-----
> From: Jeff Newmiller <[hidden email]>
> Sent: Wednesday, July 18, 2018 9:26 PM
> To: Shawn Way <[hidden email]>
> Cc: [hidden email]
> Subject: Re: [R] xtable does not print out units of a variable
>
>> On Wed, 18 Jul 2018, Shawn Way wrote:
>>
>> I have a dataframe that contains units using the units package.
>> Unfortunately, I really need the units for reporting.  I'm assuming
>> that's because the data is in a class units and xtable doesn't know
>> what to do with this.
>
> If you want a bug or feature in a CONTRIBUTED PACKAGE, then you need to communicate with the maintainer:
>
> maintainer( "xtable" )
>
> Do keep in mind that they almost always volunteer their time, so be patient, and consider figuring out what code changes they need to make so it will work.
>
> More below.
>
>> The following is a MWE:
>>
>>   library(xtable)
>>   library(units)
>>   data <- data.frame(x=c(as_units(12,"ft")))
>>   xtable(data)
>>
>>   % latex table generated in R 3.5.1 by xtable 1.8-2 package
>>   % Wed Jul 18 17:31:44 2018
>>   \begin{table}[ht]
>>   \centering
>>   \begin{tabular}{rr}
>>     \hline
>>    & x \\
>>     \hline
>>   1 & 12.00 \\
>>     \hline
>>   \end{tabular}
>>   \end{table}
>>
>> What I'm looking for is the line
>>
>>   1 & 12.00 \\
>>
>> to be
>>
>>   1 & 12.00 $ft$\\
>>
>> Can someone point me in the correct direction to make this happen?
>> Since units are used extensively in engineering calculations, being
>> able to handle this class would be extremely beneficial to engineers
>> that are using R with knitr to generate engineering documents.
>>
>> Shawn Way
>
> I do want to emphasize that R focuses on consistency among elements within columns, not rows, so putting the units into the body of the table is kind of visually redundant in most cases. Consider:
>
> ####################
> library(xtable)
> library(units)
> #> udunits system database from /usr/share/xml/udunits data <- data.frame(x=c(as_units(c(12,13),"ft")))
> datax <- xtable(data)
> names(datax) <- paste0( names(datax)[1]
>                       , " ($"
>                       , deparse_unit( datax[[1]] )
>                       , "$)"
>                       )
> datax
> #> % latex table generated in R 3.4.4 by xtable 1.8-2 package #> % Wed Jul 18 19:13:29 2018 #> \begin{table}[ht] #> \centering #> \begin{tabular}{rr}
> #>   \hline
> #>  & x (\$ft\$) \\
> #>   \hline
> #> 1 & 12.00 \\
> #>   2 & 13.00 \\
> #>    \hline
> #> \end{tabular}
> #> \end{table}
>
> #' Created on 2018-07-18 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0).
> ####################
>
> If you have some kind of summary table with different units on each row, then you will probably arrive at that information a single-row, many column data frame. I usually transpose this into a three-column data frame with a description column, a value column, and a units column. I don't use the units package so have never tried to adapt it to that process.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.