在R中以所需的Html表格式转换Dataframe

时间:2018-05-06 12:44:46

标签: css r html-table kable kableextra

我有下面提到的数据框:

structure(list(Month = c("May-17", "A", "B", "C", "D", "E", "Apr-17", 
"A", "B", "C", "D", "E", "Mar-17", "A", "B", "C", "D", "E"), 
    No = c(75L, 10L, 15L, 12L, 18L, 20L, 75L, 9L, 12L, 10L, 19L, 
    25L, 72L, 8L, 13L, 9L, 18L, 24L), `Inc_%` = c("0.00%", "11.11%", 
    "25.00%", "20.00%", "-5.26%", "-20.00%", "4.17%", "12.50%", 
    "-7.69%", "11.11%", "5.56%", "4.17%", "0.00%", "0.00%", "0.00%", 
    "0.00%", "0.00%", "0.00%"), Total = c("34,500", "1,000", 
    "1,500", "2,500", "4,500", "25,000", "36,500", "1,100", "1,200", 
    "2,200", "4,000", "28,000", "35,000", "1,000", "1,100", "2,000", 
    "3,900", "27,000"), `Inc_%2` = c("-5.48%", "-9.09%", "25.00%", 
    "13.64%", "12.50%", "-10.71%", "4.29%", "10.00%", "9.09%", 
    "10.00%", "2.56%", "3.70%", "0.00%", "0.00%", "0.00%", "0.00%", 
    "0.00%", "0.00%"), Mean = c("28,200", "800", "1,200", "2,200", 
    "4,000", "20,000", "6,47,289", "7,57,991", "5,18,091", "6,95,407", 
    "1,68,428", "9,10,521", "7,38,687", "5,87,810", "8,05,300", 
    "7,67,826", "1,84,875", "11,73,866"), A = c(56L, 11L, 15L, 
    19L, 10L, 1L, 50L, 10L, 10L, 10L, 10L, 10L, 56L, 11L, 15L, 
    19L, 10L, 1L), Sum_A = c("27,800", "800", "1,000", "2,000", 
    "4,000", "20,000", "32,500", "1,000", "1,000", "2,000", "3,500", 
    "25,000", "31,800", "800", "1,000", "1,500", "3,500", "25,000"
    ), B = c(19L, 5L, 9L, 2L, 3L, 0L, 25L, 5L, 5L, 5L, 5L, 5L, 
    16L, 5L, 9L, 2L, 0L, 0L), Sum_B = c("6,700", "200", "500", 
    "500", "500", "5000", "4,000", "100", "200", "200", "500", 
    "3,000", "3,200", "200", "100", "500", "400", "2,000"), `%` = c("20.00%", 
    "12.00%", "22.50%", "10.50%", "11.50%", "16.20%", "20.20%", 
    "10.20%", "15.50%", "14.20%", "18.20%", "18.00%", "15.00%", 
    "10.20%", "13.20%", "14.50%", "15.00%", "11.20%"), Day = c(12L, 
    10L, 8L, 7L, 5L, 12L, 10L, 8L, 9L, 7L, 8L, 8L, 10L, 8L, 9L, 
    7L, 8L, 8L)), class = "data.frame", row.names = c(NA, -18L
))

我想将其转换为html表格格式,如下所示,或者如果你们建议任何好的格式: enter image description here

我无法将A,B,C,D和E的字体大小转换为正常大小(正常字体大小= 11)以及斜体和边框,如格式所示。

1 个答案:

答案 0 :(得分:2)

您可以使用kable knitrkableExtra - 包中的kable(my_df, "html", table.attr = 'style = "border-collapse: collapse;"') %>% # for left alignment kable_styling(position = "left") %>% # header row in orange row_spec(0, background = "orange") %>% # header row in orange # All rows equal A, B, C, D, or E are italic and one font size smaller row_spec(which(my_df$Month %in% LETTERS[1:5]), font_size = 10, italic = T) %>% # All other rows are yellow and bold row_spec(which(!my_df$Month %in% LETTERS[1:5]), background = "yellow", bold = T) %>% # All cells get a border row_spec(0:nrow(my_df), extra_css = "border: 1px solid black;")

handleFacebookLogin () {
LoginManager.logInWithReadPermissions(['public_profile']).then(
  function(result) {
    if (result.isCancelled) {
      alert('Login isCancelled');
    } else {
      alert('Login ok')
      this.props.navigation.navigate('ListaUf')
    }
  },
  function(error) {
    alert('Error: ' + error);
  }
);
}

enter image description here