在 table 中使用格式 table() 在 R 中格式化文本

formatting text in table with formattable() in R

我在使用 formattable() 格式化 table 的文本时遇到问题。

我希望第二栏 (Species (Scientific)) 以斜体显示。

我试过下面的代码,但没有任何反应(table 出现在图表 window 中,但没有斜体和警告)

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

下面是我的 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), ]     

使用 font.style 表示斜体。这应该有效:

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

为了将来参考,您可以对 font.family(例如 "times")和 font.size(例如“200%”)执行相同的操作。

另请注意,Species (Scientific) 在 Scientific 中需要大写字母 'S' 才能匹配列名。