4

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

Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
  • 1
    Within kable function you will see `x = gsub("^\\s*|\\s*$", "", x)` line, I think this is why it removes all leading and trailing spaces. Maybe modify the code, and make custom kable function. – zx8754 Jul 15 '15 at 12:39
  • You can align columns, but not unique cells with [Pandoc's markdown](http://pandoc.org/README.html#tables). – daroczig Jul 15 '15 at 12:54
  • 3
    @zx8754 Even if I don't trim the white spaces, they will not be displayed in the table anyway, since leading/trailing spaces have no special meaning in Markdown tables and will be ignored. Even if they are not ignored in Markdown, they will still be ignored in HTML by default (consecutive white spaces will be treated as one space). So I think Sharon's solution below should be the correct one. – Yihui Xie Jul 15 '15 at 15:31

4 Answers4

3

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.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
3

You can try adding

df$Species <- gsub(" ", "&nbsp;", df$Species, fixed=TRUE)

before creating the table, that will change all your spaces before versicolor to HTML non-breaking spaces.

Sharon
  • 3,676
  • 3
  • 23
  • 20
  • that will change _all_ spaces to ` `, not just leading (needs a lookahead regex). non-breaking spaces impact line breaks at end-of-line. it won't pad. – hrbrmstr Jul 15 '15 at 20:17
  • Not sure there's anything wrong with all spaces in a species name for display in a table only having   if it's not an interactive table where you're doing search/sort? – Sharon Jul 15 '15 at 21:44
1

Separate the leading spaces from the trailing text with one gsub. Then, globally replace those spaces with a second gsub. Lastly, combine the two parts with paste.

As a one-liner:

paste0(gsub('\\s', '&nbsp;', gsub('^(\\s*)\\S.*', '\\1', df$Species)), gsub('^\\s*(\\S.*)', '\\1', df$Species))

If df$Species <- " versicolor", the result is : "&nbsp;&nbsp;&nbsp;versicolor"

If df$Species <- " one two three", the result is : "&nbsp;&nbsp;&nbsp;one two three"

Or, reformatted for clarity:

x <- df$Species

paste0(                                 # Combine edited text
    gsub('\\s', '&nbsp;',               # Replace leading spaces
        gsub('^(\\s*)\\S.*', '\\1', x)  # Extract leading spaces
    ), 
    gsub('^\\s*(\\S.*)', '\\1', x)      # Extract trailing text
)
Damian
  • 1,385
  • 10
  • 10
0

What about using kableExtra package? See "Row indentation" here:

https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html

Basically,

library(kableExtra)
dt <- mtcars[1:5, 1:6]
kable(dt) %>%
  kable_styling("striped", full_width = F) %>%
  add_indent(c(1, 3, 5))
jpl2116
  • 55
  • 4