簡體   English   中英

用R中的formattable()格式化表中的文本

[英]formatting text in table with formattable() in R

我在使用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), ]     

使用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”與列名匹配。

帶有灰色和斜體字的表格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM