Home > front end >  Having trouble aligning data using formattable in R
Having trouble aligning data using formattable in R

Time:09-24

I put my data into a data frame and now want to align the data.

df.obs <- data.frame(Observer_Unanimous, row.names = TRUE)

Basic formattable plot

I now want to left align the first column (header), and center align the next three.

Code run:

formattable(df.obs, align = c("1", "c", "c", "c")) 

**Error in kable(mat, format = format, align = align, escape = FALSE, ...,  : 
  'align' must be a character vector of possible values 'l', 'r', and 'c'**

Not making progess! Any thoughts?

CodePudding user response:

If you're open to using the gt package it's pretty straightforward. Here's a quick example using a subset of the iris data set:

df <- iris %>% 
  dplyr::select(Species, Sepal.Length, Sepal.Width, Petal.Width) %>% 
  head()

df %>% 
  gt::gt() %>% 
  gt::cols_align(
    align = "center",  
    columns = 2:4
  )

output

CodePudding user response:

If we directly apply formattable on the data.frame, it would work

library(formattable)
df1 <- head(iris)
formattable(df1, align = c("l", "c", "c", "c", "r"))

-output

enter image description here

  • Related