Bookdown/Huskydown中的表格具有多种功能(引文,标题,URL,PNG图等) [英] Table in Bookdown/Huskydown with several features (Citation, Caption, URL, PNG Figure, ...)

查看:151
本文介绍了Bookdown/Huskydown中的表格具有多种功能(引文,标题,URL,PNG图等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R降价文档(Bookdown/Huskydown)中包含一个表格,该表格应满足以下要求.理想情况下,表格可以使用多种输出格式,例如LaTex/PDF和HTML.

I would like to include a table in an R markdown document (Bookdown/Huskydown) which should meet the following requirements. Ideally, the table works with several output formats, e.g. LaTex/PDF and HTML.

  • 表格宽度:固定
  • 单元格宽度:固定
  • 垂直对齐:单元格内容与顶部对齐
  • 文本格式:,例如粗体斜体(最好是支持md格式,以便输出与代码无关的代码),并允许较长文本中的换行符
  • 引文:应该呈现
  • URL:作为HTML和LaTex/PDF中的可点击链接
  • 数字:包括
  • Table width: fixed
  • Cell width: fixed
  • Vertical alignment: cell content aligned to the top
  • Text formatting: like bold or italics (best would be if md formatting supported, such that code is output agnostic) and allow for line breaks in longer texts
  • Citations: should be rendered
  • URLs: as clickable links both in HTML and LaTex/PDF
  • Figures: include
  1. 本地存储的数字,位于
    • 降价方式![](Rlogo.png)
    • 编织方式knitr::include_graphics("Rlogo.png")
  1. figures stored locally, either in
    • a markdown way ![](Rlogo.png) or
    • a knitr way knitr::include_graphics("Rlogo.png")

表格的

  • 标题
  • 字幕文本格式:字幕也应允许文本格式
  • 脚注:在表中包括脚注
  • 表编号:表应编号
  • 引用表:需要在文档中
  • Caption for the table
  • Captions text formatting: caption should also allow for text formatting
  • Footnote: include footnotes in the table
  • Table numeration: tables are should be numerated
  • Referencing the table: in the document is needed
    • 固定的单元格宽度:在降价中,表格标题中的-"号确定单元格宽度
    • 换行符:
      • LaTex:\\linebreak
      • 所有其他人:<br/>
      • Fixed cell width: in markdown the number of "-"s in table header determine cell width
      • Linebreaks:
        • LaTex:\\linebreak
        • All others: <br/>
        • LaTex:添加\label{foo} => \ref{foo}(\@ref(foo))
        • 降价:添加Table: (\#tab:md-table) Caption ==> \@ref(tab:md-table))
        • LaTex: add \label{foo} => \ref{foo} ( \@ref(foo))
        • Markdown: add Table: (\#tab:md-table) Caption==> \@ref(tab:md-table))
        • 降价:在降价中轻松编码表格
        • Kable& kableExtra:表格的通用R降价编码,但垂直文本对齐方式模糊且图形未包含在PDF中
        • 平移:实现最多,但没有垂直对齐和脚注
        • 可扩展:最有前途,但数字未包含在PDF中
        • Markdown: easy coding of tables in markdown
        • Kable & kableExtra: Versatile R markdown coding of the table, but vertical text alignment obscure and figures are not included in PDF
        • Pander: achieves the most, but no vertical alignment and footnotes
        • Huxtable: most promising, but figures are not included in PDF

        推荐答案

        与为上述表格提供MWE相比,这不是一个答案

        ```{r}
        # create some random text
        library(stringi)
        some_text <- stri_rand_lipsum(1)
        some_text <- substr(some_text, 1, 75)
        
        # create dataframe with some stuff
        figpath <- "figure/"
        df <- data.frame(
          Citation = c("@R-base", "@R-bookdown"),
          Textfield = c("**Formatted** string<br/> -- _Everyone_ needs H^2^O", some_text),
          URL = c("[R-url](https://www.r-project.org/)", "[bookdown](https://bookdown.org/)"),
          fig_local_md = c(
            paste0("![](", figpath, "Rlogo.png){ width=10% height=5% }"),
            paste0("![](", figpath, "bookdownlogo.png){ height='36px' width='36px' }")
          )#,
          # not working:
          # fig_local_knitr = c("knitr::include_graphics('figure/Rlogo.png')", "knitr::include_graphics('figure/bookdownlogo.png')") 
        )
        
        # only include if output format is HTML, else pander throws error
        if (knitr::is_html_output()) {
          df$fig_web  <- c("![](https://www.picgifs.com/glitter-gifs/a/arrows/picgifs-arrows-110130.gif)")
          output_format <- "html"
        }
        if (knitr::is_latex_output()) {
          output_format <- "latex"
        }
        ```
        

        降价

        Table: markdown table: markdown styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})
        
          | Image                                                      | Description                                                  |
          | :--------------------------------------------------------- | :----------------------------------------------------------- |
          | ![](figure/Rlogo.png){ width=10% height=5% }               | **Image description** [@R-base]  <br/>Lorem ipsum dolor sit amet, ...  [R-url](https://www.r-project.org/) |
          | ![](figure/bookdownlogo.png){ height='36px' width='36px' } | **Image description** [@R-bookdown] <br/>Lorem ipsum dolor sit amet, ... [bookdown](https://bookdown.org/) |
        

        有线桌

          ```{r kable-table, echo=FALSE, out.width='90%', fig.align = "center", results='asis'}
        library(knitr)
        kable(df, 
              caption = "kable table: markdown styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})", 
              caption.short = "md styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})"
              )
        ```
        

        kableExtra表

        ```{r kableExtra-table, echo=FALSE, out.width='90%', fig.align = "center", results='asis'}
        library(kableExtra)
        # http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf 
        kable(
          df,
          caption = "kableExtra table: markdown styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})",
          output_format, booktabs = T, # output_format = latex, html (specify above)
          # align = "l", 
          valign = "top"
        ) %>% 
          kable_styling(full_width = F,
                        latex_options = c(#"striped",
                          "hold_position", # stop table floating
                          "repeat_header") # for long tables
          ) %>% 
          column_spec(1, bold = T, border_right = T, width = "30em") %>%
          column_spec(2, width = "50em") %>%
          column_spec(3, width = "5em") %>%
          column_spec(4, width = "10em") %>%
          column_spec(5, width = "10em") %>%
          footnote(general = "Here is a general comments of the table. ",
                   number = c("Footnote 1; ", "Footnote 2; "),
                   alphabet = c("Footnote A; ", "Footnote B; "),
                   symbol = c("Footnote Symbol 1; ", "Footnote Symbol 2"),
                   general_title = "General: ", number_title = "Type I: ",
                   alphabet_title = "Type II: ", symbol_title = "Type III: ",
                   footnote_as_chunk = T, title_format = c("italic", "underline")
          )
        ```
        

        平躺表

        ```{r pander-table, echo=FALSE, out.width='90%', fig.align = "center", results='asis'}
        library(pander)
        # https://cran.r-project.org/web/packages/pander/vignettes/pandoc_table.html
        
        pander(
          df,
          caption = "pander table: markdown styling works in HTML and PDF (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})",
          # style = "multiline", # simple
          split.table = Inf, # default = 80 characters; Inf = turn off table splitting
          split.cells = c(15, 50, 5, 5, 5), # default = 30
          # split.cells = c("25%", "50%", "5%", "10%", "10%"), # no difference
          justify = "left"
        )
        ```
        

        huxtable表

        ```{r huxtable-table, echo=FALSE, out.width='90%', fig.align = "center", results='asis'}
        library(dplyr)
        library(huxtable)
        # https://hughjonesd.github.io/huxtable/
        hux <- as_hux(df)                                                       %>%
          # huxtable::add_rownames(colname = '')                                  %>%
          huxtable::add_colnames()                                              %>%
          set_top_border(1, everywhere, 1)                                      %>%
          set_bottom_border(1, everywhere, 1)                                   %>%
          set_bottom_border(final(), everywhere, 1)                             %>%
          set_bold(1, everywhere, TRUE)                                         %>% # bold headlines
          set_italic(-1, 1, TRUE)                                               %>% # italics in first column (except the first row)
          set_valign("top")                                                     %>%
          set_width(1)                                                          %>%
          set_col_width(c(0.10,0.45,0.05,0.10,0.10))                             %>%
          set_wrap(TRUE)                                        %>%
          set_position('left')                                    %>% # fix table alignment (default is center)
          add_footnote("Sample Footnote")                                       %>% 
          set_font_size(4)
        
        table_caption <-  'huxtable table: markdown styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})'
        
        # Print table conditional on output type
        if (knitr::is_html_output()) {
          caption(hux) <- paste0('(#tab:huxtable-table-explicit) ', table_caption)
          print_html(hux) # output table html friendly (requires in chunk options "results='asis'")
        }
        if (knitr::is_latex_output()) {
          caption(hux) <- paste0('(\\#tab:huxtable-table-explicit) ', table_caption)
          hux   # if using chunk option "results='asis'" simply output the table with "hux", i.e. do not use print_latex(hux)
        }
        ```
        

        参考表格

        不同表类型的工作方式不同

        Referencing the tables

        works differently for different table types

        最后为数字表格添加一个简短的标题实际上并没有按预期工作 (ref:huxtable-table-caption)huxtable-table字幕 (ref:huxtable-table-scaption)huxtable-table短标题

        Finally adding a short caption for the table of figures is not really working as desired (ref:huxtable-table-caption) huxtable-table caption (ref:huxtable-table-scaption) huxtable-table short caption

        ```{r huxtable-table, echo=FALSE, out.width='90%', fig.align = "center", fig.cap='(ref:huxtable-table-caption)', fig.scap='(ref:huxtable-table-scaption)', results='asis'}
        ...
        ```
        

        这篇关于Bookdown/Huskydown中的表格具有多种功能(引文,标题,URL,PNG图等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆