R markdown:简化创建图形和文本表格

问题描述 投票:2回答:2

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

example image

我知道我可以在原始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
2个回答
3
投票

可能忘了逃避报价?这对我来说很好:

---
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)
```

enter image description here


0
投票

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

我不熟悉bootstrap <div>构造,所以我使用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.")
    )
)
```

我仍然需要调整填充,但这给了我更多或更少的追求:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.