[R] Tables Package Ampersand Quoting

Duncan Murdoch murdoch.duncan at gmail.com
Mon Nov 11 01:15:06 CET 2013


On 13-11-10 1:16 PM, Jeff Newmiller wrote:> 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,

Yes, that's a bug.  I have just fixed it.


  and I am not sure why the backslash is getting doubled

That's just the way R prints matrix entries that contain single 
backslashes.  print.tabular just converts the table + labels to a matrix 
and then prints it without quotes.

 > 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:

The & backslash is the same bug as the first one.

 >
 >> 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.

After the fix, this appears as

 > 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
\tabular, Inc. - Items  & $145$ & $140$ \\
\tabular, Inc. - Stuff  & $140$ & $150$ \\
Joe & Co. - Harpoon  & $200$ & $200$ \\
Joe & Co. - Screwdriver  & $180$ & $190$ \\
Joe & Co. - Shovel  & $190$ & $180$ \\
\hline
\end{tabular}

which is probably still not what you want:  here you'd want the & in the 
company name to be escaped.  I'll have to think about this a bit to find 
the best solution, but in the meantime, if you use the R-forge version, 
you can get the right behaviour by including the escape in the original 
factor level.  (What I may do is include some information about whether 
something has been texified or not, so that it only happens once.  Or 
perhaps I will just never do it unless you explicitly ask for it.  We'll 
see.)

Duncan Murdoch

 >
 > 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
 >
 > ______________________________________________
 > R-help at r-project.org mailing list
 > 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.
 >



More information about the R-help mailing list