HTML和Word输出之间的简单方法?

时间:2018-03-16 04:44:30

标签: r ms-word knitr r-markdown kable

我创建了一些表格(使用knitr& kableExtra),当我使用output: html_document但我尝试使用output: word_document的内容表格中的每个单元格都打印到Word文档中的连续新行。

是否有:

  1. 一种简单的方法来重新格式化表格,以便在设置output: word_document时我可以复制它们?
  2. 一个包可以创建Word友好表,使我能够复制kableExtra::add_header_abovekableExtra::indent_row函数吗?
  3. 使用output: word_document

    生成的表格的表示法
    ---
    title: "A tale of two tables"
    output: word_document
    ---
    ```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
    ```
    
    ```{r two-tables, results='asis', echo=FALSE}
    library(tidyverse)
    library(knitr)
    library(kableExtra)
    library(formattable)
    
    a <- tribble(~location, ~'Month (persons)', ~'TTY (persons)', ~'TTY % change',
           'new_south_wales',1604,40328,3.3,
           'victoria',-2118,13415,3.5,
           'queensland',214,10023,3.2,
           'south_australia',1969,11787,5.0,
           'western_australia',531,1316,1.6,
           'tasmania',-887,2428,1.9,
           'northern_territory',-44,570,2.4,
           'australian_capital_territory',32,-434,-3.1,
           'australia',78,1060,4.8)
    
    b <- tribble(~series,~Dec,~Jan,~TTY,
           'FT employed', 12700, 49800, 293200,
           'PT employed', 20700, 65900, 110100,
           'Total', 33500, 16000, 403300,
           'per cent', 0.3, 0.1, 3.3,
           'Agg hours worked', -0.8, -1.5, 0.5,
           'Part rate', 65.7, 65.6, 64.6,
           'Ue', 5.56, 5.49, 5.7)
    
    ### Creating the tables
    ## Federal employment table
    test_federal <- kable(b, format = "html", output = F) %>%
      kable_styling(c("striped", 'hover', 'condensed', 'responsive'), full_width = F, position = 'left', font_size = 11) %>%
      add_indent(4) %>% 
      row_spec(row = 3, bold = TRUE) %>% 
      add_header_above(c(" ", 'Month (sa)' = 2, 'Year (sa)' = 1))
    
    ## State employment table
    test_state <- kable(a, format = "html", escape = FALSE, output = F) %>%
      kable_styling(c("striped", 'hover', 'condensed', 'responsive'), full_width = F, position = 'right', font_size = 11) %>% 
      row_spec(row = 9, bold = TRUE) %>% 
      add_header_above(c("", "Dec" = 1, "Dec" = 1, "Dec" = 1))
    
    cat(
      c('<table style="margin: auto"><tr valign="top"><td style="padding-right: 2cm">', 
        test_federal, 
        '</td>', 
        '<td>', 
        test_state, 
        '</td></tr></table>'
      ), 
      sep = ''
    )
    ```
    

0 个答案:

没有答案