-2

I want to create a table of point estimates from a sample for print in the following format

variable     group1     group2     group3     etc
age           18.2       18.5       23.2      
weight        125.4     130.1      117.1
etc

I also have confidence intervals for each point estimate, but displaying them will cause too much clutter. Instead, I'd like to use text attributes (italics, bold, underline, font) to signal which point estimates in a row differ significantly. So, in the first row, if 23.2 differed significantly from the other two, it would be displayed in bold (for example). I'm not sure if such a display would appear bewildering, but I'd like to try.

Could anyone suggest a table formatting library in R that would allow me to accomplish this? Perhaps one that allows me to supply text attributes in the data table for each point estimate?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
davideps
  • 541
  • 3
  • 13
  • What output format do you want? General questions about package recommendations are not really allowed on this site either, only specific programming questions. So if you already know what R package you want to use, then that's more reasonable. Decide if you want an image, tex/pdf, rtf, docx, or other format first. – MrFlick May 22 '14 at 18:59
  • I'm interested in pdf or rtf. I was hoping to use ggplot2 or pander. I am less familiar with the latter, but it appears to handle cell-level formatting in a table well. – davideps May 22 '14 at 19:27
  • `ggplot2` just makes images. often i use `xtable` to format LaTeX tables and then create a PDF from that, or the `tables` package can do that as well. Maybe pick a package and re-ask. – MrFlick May 22 '14 at 20:27

3 Answers3

1

Another solution could be to use ReporteRs package using FlexTable API and send the object to a docx document :

library( ReporteRs )

data = iris[45:55, ]

MyFTable = FlexTable( data = data )
MyFTable[data$Petal.Length < 3, "Species"] = textProperties( color="red"
    , font.style="italic")
MyFTable[data$Sepal.Length < 5, 1:4] = cellProperties( background.color="#999999")
MyFTable[ , 1:4] = parProperties( text.align="right" )


doc.filename = "test.docx"
doc = docx( )
doc = addFlexTable( doc, MyFTable )
writeDoc( doc, file = doc.filename )
David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Thank you. I am not familiar with ReporteRs and will look into it. I am torn between finally stepping away from Microsoft products and learning to deal with them more effectively since many people use them. – davideps Jun 10 '14 at 20:33
  • If you don't want to use MS Word or PowerPoint, you can use html output : as.html( MyFTable ) and send that into knitr or shiny. – David Gohel Jun 11 '14 at 12:32
  • The interface to ReporteRs is intuitive and easy to read upon returning to the code after a few days. Thank you. – davideps Jun 22 '14 at 20:33
0

I believe you can do something like this with the xtable() package - if you have xtable output your table, you can use knitr/pandoc to convert it to word, HTML, etc. or you can just paste the LaTeX output into a document and compile it.

Here's a demo:

library(xtable)

# original data frame
df <- data.frame(var=c("age", "weight", "etc"), group1=c("18.2", "125.4", "3"), group2=c("18.5", "130.1", "3"), group3=c("23.2", "117.1", "3"), etc=c("1", "2", "3"))

# data frame in similar format indicating significance
significant <- data.frame(var=c("age", "weight", "etc"), group1=c(F, T, F), group2=c(T, F, T), group3=c(F, T, F))

library(reshape2)
# transform everything into long form to apply text formatting
df.melt <- melt(df, id.vars = 1, variable.name="group", value.name="value")
sig.melt <- melt(significant, id.vars=1, variable.name = "group", value.name="sig")

# merge datasets together
tmp <- merge(df.melt, sig.melt)


tmp$ans <- tmp$value
# apply text formatting using LaTeX functions
tmp$ans[tmp$sig] <- paste0("\\textit{", tmp$ans, "}")[tmp$sig]

# transform dataset back to "wide form" for table output
df2 <- dcast(tmp, var~group, value.var="ans")

# output table in LaTeX format
print(xtable(df2), include.rownames=FALSE, sanitize.text.function=identity)
srvanderplas
  • 424
  • 6
  • 9
0

A qucik demo based on the OP-mentioned pander package:

  1. Load it:

    library(pander)
    
  2. Create some dummy data, which I will import from the rapport package this time:

    df <- rapport::ius2008
    
  3. Compute a basic cross table:

    t <- table(df$dwell, df$net.pay)
    
  4. Identify those cells with high standardized residuals and emphasize those:

    emphasize.cells(which(abs(chisq.test(t)$stdres) > 2, arr.ind = TRUE))
    
  5. Do not split the markdown table:

    panderOptions('table.split.table', Inf)
    
  6. Print the markdown table:

    pander(t)
    

Resulting in:

----------------------------------------------------------------------------
     &nbsp;       parents   school/faculty   employer   self-funded   other 
---------------- --------- ---------------- ---------- ------------- -------
    **city**        276           14            26          229       *20*  

 **small town**     14            1             1           11         *4*  

  **village**       13            1             0           13          2   
----------------------------------------------------------------------------
daroczig
  • 28,004
  • 7
  • 90
  • 124