【问题标题】:R markdown: simplify creating tables of figures and textR markdown:简化创建图表和文本表格
【发布时间】:2017-12-17 21:39:26
【问题描述】:

对于 R markdown Rmd 网页,我想生成包含在第一列缩略图中的表格(链接到更大的图像或网站)和 第 2 列中的描述性文本。一个例子是下图:

我知道我可以在原始 HTML 中手动创建它,但这非常繁琐且耗时。一定有更简单的方法。

在另一个页面上,我尝试了一个 markdown / pandoc 表,但没有成功,我恢复为 HTML 的手动编码

icon                                              | title
--------------------------------------------------+--------------------------
<img src="images/books/R-Graphics.jpg" height=50> |Paul Murrell, *R Graphics*, 2nd Ed.
<img src="images/books/R-graphics-cookbook.jpg" height=50> | Winston Chang, R Graphics Cookbook
<img src="images/books/lattice.png" height=50> | Deepayan Sarkar, *lattice*
<img src="images/books/ggplot2.jpg" height=50> | Hadley Wickham, *ggplot2*

也许htmltools 包在这里会很有用,但我不太明白如何在我的Rmd 文件中为此应用程序使用它。

【问题讨论】:

    标签: html-table r-markdown pandoc


    【解决方案1】:

    可能忘记了转义引号?这对我来说很好:

    ---
    title: "The Mighty Doge"
    output: html_document
    ---
    
    ```{r}
    library(knitr)
    create_thumbnail <- function(file) {
      paste0("<a href=\"", file, "\"><img src=\"", file, "\" style=\"width: 50px;\"/></a>")
    }
    df <- data.frame(Image       = rep("unnamed.png", 5), 
                     Description = rep("Doge", 5))
    
    df$Image <- create_thumbnail(df$Image)
    kable(df)
    ```
    

    【讨论】:

    • 这很有帮助。我没有尝试过data.framekable。当然,接下来的挑战就是将我的示例信息放入 data.frame
    • 我在编织到 PDF 时遇到了类似的困难(带有文本和图像的简单表格,不涉及缩略图)。请在此处查看我的相关帖子:stackoverflow.com/questions/58204272/… 非常感谢任何帮助
    【解决方案2】:

    这是一种使用htmltools 的方法,看起来更灵活,因为我可以更轻松地控制细节。

    我不熟悉 bootstrap &lt;div&gt; 构造,所以我使用了 HTML 表格构造。我必须为tr()td() 等定义函数。

    ```{r html-setup, echo=FALSE}
    library(htmltools)
    # table tags
    tab <- function (...)
    tags$table(...)
    
    td <- function (...) 
        tags$td(...)
    tr <- function (...) 
        tags$tr(...)
    
    # an <a> tag with href as the text to be displayed
    aself <- function (href, ...)
        a(href, href=href, ...)
    
    ```
    

    然后按我想要的方式构造我的表条目:

    ```{r table-functions, echo=FALSE}
    # thumnail figure with href in a table column
    tabfig <- function(name, img, href, width) {
            td(
          a(class = "thumbnail", title = name, href = href,
            img(src = img, width=width)
           )
        )
    }
    tabtxt <- function(text, ...) {
            td(text, ...)
    }
    ```
    

    最后,用它们来输入条目:

    ## Blogs
    
    ```{r do-blogs, echo=FALSE}
    width="160px"
    tab(
      tr(
        tabfig("FlowingData", "images/blogs/flowingdata.png", "http://flowingdata.com/", width=width),
        tabtxt("Nathan Yau,", aself("flowingdata.com/"),
               "A large number of blog posts illustrating data visualization methods with tutorials on how do do these with R and other software.")
        ),
    
      tr(
        tabfig("Junk Charts", "images/blogs/junkcharts.png", "http://junkcharts.typepad.com/", width=width),
        tabtxt("Kaiser Fung,", aself("http://junkcharts.typepad.com/"),
               "Fung discusses a variety of data displays and how they can be improved.")
        ),
    
      tr(
        tabfig("Data Stories", "images/blogs/datastories.png", "http://datastori.es/", width=width),
        tabtxt("A podcast on data visualization with Enrico Bertini and Moritz Stefaner,",
               aself("http://datastori.es/"), 
               "Interviews with over 100 graphic designers & developers.")
        )
    )
    ```
    

    我仍然需要调整填充,但这或多或少地为我提供了我所追求的:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2014-02-19
      • 1970-01-01
      • 2021-06-20
      • 2021-12-31
      • 2017-12-28
      • 1970-01-01
      相关资源
      最近更新 更多