[R] Tables Package Ampersand Quoting

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Nov 10 19:16:49 CET 2013


There seems to be some inappropriate quoting in the tabular function from 
the tables package. Consider this example:

library(tables)
sampledf <- data.frame( Manufacturer=c(rep("Joe & Co.",6)
                                       ,rep("\\tabular, Inc.",4))
                       , Tool=rep(c("Shovel","Screwdriver","Harpoon"
                                 ,"Items","Stuff"),each=2)
                       , When=rep(c("List","After"),times=5)
                       , Price=c(180,190,190,180,200,200,140,145,150,140)
                       )
sampledf$MfrTool <- with( sampledf
                         , factor( paste( Manufacturer, "-", Tool ) ) )
tabular( MfrTool ~ When * Price * identity, data=sampledf )

which produces for me:

                                           When
                                           After    List
                                           Price    Price
  MfrTool                                  identity identity
  \\textbackslash{}tabular, Inc. - Items   145      140
  \\textbackslash{}tabular, Inc. - Stuff   140      150
  Joe \\textbackslash{}& Co. - Harpoon     200      200
  Joe \\textbackslash{}& Co. - Screwdriver 180      190
  Joe \\textbackslash{}& Co. - Shovel      190      180

Although I do intend to pass this through the latex() function, I think
that in this representation the substitution of '\textbackslash{}' for
'\' is premature, and I am not sure why the backslash is getting doubled 
here.  In any event, the ampersand in 'Joe & Co.' is getting a '\' added 
to it (which it shouldn't here), and then when I pass this through the
latex function I get:

> latex(tabular( MfrTool ~ When * Price * identity, data=sampledf ))

\begin{tabular}{lcc}
\hline
  & \multicolumn{2}{c}{When} \\
  & After & \multicolumn{1}{c}{List} \\
  & Price & \multicolumn{1}{c}{Price} \\
MfrTool  & identity & \multicolumn{1}{c}{identity} \\
\hline
\textbackslash{}tabular, Inc. - Items  & $145$ & $140$ \\
\textbackslash{}tabular, Inc. - Stuff  & $140$ & $150$ \\
Joe \textbackslash{}& Co. - Harpoon  & $200$ & $200$ \\
Joe \textbackslash{}& Co. - Screwdriver  & $180$ & $190$ \\
Joe \textbackslash{}& Co. - Shovel  & $190$ & $180$ \\
\hline
\end{tabular}

where 'Joe & Co.' has become illegal TeX.

For reference,, my session:

> sessionInfo()
R version 2.15.3 (2013-03-01)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets  methods
[8] base

other attached packages:
[1] tables_0.7      Hmisc_3.12-2    Formula_1.1-1   survival_2.37-4

loaded via a namespace (and not attached):
[1] cluster_1.14.4  grid_2.15.3     lattice_0.20-24 rpart_4.1-3


---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list