Andrea
Andrea

Reputation: 41

formatting text in table with formattable() in R

I have trouble formatting the text of my table using formattable().

My wish is to have the second column (Species (Scientific)) in Italics.

I have tried the code below, but nothing happened (the table appeared in the graph window but no italics nor warnings)

formattable(table.species, 
        align =c("l","l","c"), list(`Species (scientific)` = formatter(
          "span", style = ~ style(color = "grey",font.weight = "italic"))))   

Below is the code for my table.

Species_eng <- c("Lowland paca", "Agouti", "Nine-banded armadillo",
              "Common opossum", "Ocelot","Red brocket deer",
              "White-nosed coati", "Collared peccary" ,"Central American Spiny rat",
              "Northern tamandua")
Species_sc <- c("Cuniculus paca","Dasyprocta punctata",
                            "Dasypus novemcinxtus", "Didelphis marsupialis",
                            "Leopardus pardalis", "Mazama americana",
                            "Nasua narica","Peccari tajacu","Proechimys semispinosus",
                            "Tamandua mexicana")
weight <- c(8.0, 3.6, 4.2, 2.5, 11.9, 22.8, 3.9, 25.2, 0.4, 4.3)

table.species <- data.frame(cbind(Species_eng, Species_sc, weight))
table.species <- table.species %>%
  rename(`Species (Eng)` = Species_eng,
         `Species (Scientific)` = Species_sc,
         `Weight (kg)` = weight)
#rearrange rows in order of increasing weight
table.species$`Weight (kg)` <- as.numeric(paste(table.species$`Weight (kg)`))
rownames(table.species) = NULL
table.species <- table.species[order(as.integer
                                     (table.species$`Weight (kg)`),
                                     decreasing = FALSE), ]     

Upvotes: 1

Views: 2534

Answers (1)

Ben
Ben

Reputation: 30474

Use font.style for italic. This should work:

formattable(table.species, 
            align =c("l","l","c"), list(`Species (Scientific)` = formatter(
              "span", style = ~ style(color = "grey",font.style = "italic"))))

For future reference, you can do the same for font.family (e.g., "times") and font.size (e.g., "200%").

Also note that Species (Scientific) required capital 'S' in Scientific to match column name.

table with species grey and in italics

Upvotes: 3

Related Questions