Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print HTML or Word table in knitr so that whitespaces in strings are respected

Using knitr and Rstudio, I'm trying to print a dataframe to HTML or Word, so that the leading whitespaces in versicolor, will push versicolor to the right.

#data
library(knitr ) 
library(xtable)

df <- iris[c(1,51),c(5,1)]
df$Species  <- as.character(df$Species)
df$Species[ df$Species=="versicolor"]  <- "         versicolor"

Trying different combinations of kable()...

#table
kable(  df)
kable(  df, right = FALSE,align = c("l", "l" ) )
kable(  df, right = FALSE,align = c("r", "l" ) )

I get this: enter image description here

...or this: enter image description here

But I'm trying to get this: enter image description here

like image 445
Rasmus Larsen Avatar asked Mar 16 '23 09:03

Rasmus Larsen


1 Answers

If you're willing to muck with some HTML:

df$Species[ df$Species=="versicolor"]  <- 
  "<code style='background:white'>         </code>versicolor"` will get you something like you want

or

df$Species[ df$Species=="versicolor"]  <- 
  "<span style='padding-left:30px'>         versicolor</span>"

will get you left-space-padding.

The latter might even be cleaner programmatically (inserting multiples of # in padding-left.

like image 94
hrbrmstr Avatar answered Apr 13 '23 00:04

hrbrmstr