R - Conditional row highlighting in HTML table created using xtable or kable

后端 未结 4 1112
天命终不由人
天命终不由人 2020-12-30 05:55

I\'m pretty much a beginner at programmatically formatting R output, but I\'ve got a basic understanding of knitr, xtable, Markdown, and Pandoc\'s

4条回答
  •  醉梦人生
    2020-12-30 06:33

    Here is a solution using ReporteRs:

    set.seed(123)
    df <- data.frame(id = sample(1:100, 20, replace = TRUE),
      inputval = sample(seq(0, 1, by=0.01), 20, replace = TRUE),
      outcome  = sample(1:4, 20, replace = TRUE))
    
    library( ReporteRs )
    library( magrittr )
    
    # create and format table
    myft = df %>% FlexTable() %>% 
      setRowsColors( df$outcome == 1, 'magenta') %>% 
      setFlexTableWidths( c( 1, 1, 1) )
    
    # create an html doc and send myft into
    doc = bsdoc() %>% addFlexTable( myft )
    
    # write the html file in a new dir
    writeDoc( doc, "example_out/df.html")
    

提交回复
热议问题