[R] xtable does not print out units of a variable

Marc Schwartz m@rc_@chw@rtz @end|ng |rom me@com
Thu Jul 19 15:38:57 CEST 2018


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 <SWay using meco.com> 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 <jdnewmil using dcn.davis.ca.us> 
> Sent: Wednesday, July 18, 2018 9:26 PM
> To: Shawn Way <SWay using meco.com>
> Cc: r-help using r-project.org
> 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.




More information about the R-help mailing list