使用R

时间:2019-09-14 13:52:40

标签: r formattable italic

我无法使用formattable()格式化表格的文本。

我希望在斜体字里有第二列( Species(Scientific))。

我尝试了下面的代码,但没有任何反应(表格出现在图形窗口中,但没有斜体或警告)

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

下面是我的表的代码。

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), ]     

1 个答案:

答案 0 :(得分:1)

使用font.style表示斜体。这应该起作用:

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

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

还要注意,Species (Scientific)在科学版中要求大写字母“ S”与列名匹配。

table with species grey and in italics