Joanne Demmler - 5 months ago 26

R Question

I have successfully created a table, that I can run as follows:

`library(xtable)`

print(x, floating.environment='sidewaystable', inlude.rownames=F)

Please note, that the table only fits onto the page if it is rotated sideways.

I would now like to manipulate the entries to add a

`databar`

For example:

`x <- print(xtable(x, caption="My table"), include.rownames=F`

x <- gsub("(\\d{1,3}\\.\\d{2})","\\\\databar{\\1}",x)

The problem is that

`x`

`print(x, floating.environment='sidewaystable', inlude.rownames=F)`

I can plot the table using

`cat(x, '\n')`

Any ideas?

Answer

a solution could be to put the `databar`

before using the `xtable`

function, like this :

```
d1 <- data.frame(x1 = c("A", "B", "C", "D", "E", "F"), x2 = runif(6)*100)
d1
# x1 x2
# 1 A 0.2890781
# 2 B 99.9268717
# 3 C 70.0964751
# 4 D 90.7419767
# 5 E 58.6721176
# 6 F 38.5582817
d1$x3 <- paste0("\\databar{", round(d1$x2, 2), "}")
d1
# x1 x2 x3
# 1 A 0.2890781 \\databar{0.29}
# 2 B 99.9268717 \\databar{99.93}
# 3 C 70.0964751 \\databar{70.1}
# 4 D 90.7419767 \\databar{90.74}
# 5 E 58.6721176 \\databar{58.67}
# 6 F 38.5582817 \\databar{38.56}
library(xtable)
print(xtable(d1), sanitize.text.function=function(x) x)
# \begin{table}[ht]
# \centering
# \begin{tabular}{rlrl}
# \hline
# & x1 & x2 & x3 \\
# \hline
# 1 & A & 0.29 & \databar{0.29} \\
# 2 & B & 99.93 & \databar{99.93} \\
# 3 & C & 70.10 & \databar{70.1} \\
# 4 & D & 90.74 & \databar{90.74} \\
# 5 & E & 58.67 & \databar{58.67} \\
# 6 & F & 38.56 & \databar{38.56} \\
# \hline
# \end{tabular}
# \end{table}
```

Note that you have to redefine the `sanitize.text.function`

parameter, so be careful to special characters.