我有一个数据框
df = data.frame("A" = c("a","b, c","c","d, e, f"), "B" = c(1,2,3,4), "link" = c("www.a.com", "www.b.com, www.c.com", "www.c.com", "www.d.com, www.e.com, www.f.com"))
A B链接
a 1 www.a.com
b,c 2 www.b.com,www.c.com
c 3 www.c.com
d,e,f 4 www.d.com,www.e.com,www.f.com
我制作格式表
dt.ft <- regulartable(data = dt[, list(A, B, link)])
我想让“ A”列中的值与“链接”列中的相应值超链接,这是我在@DavidGohel的帮助下使用以下命令完成的
dt.ft <- flextable(data = df, col_keys = c("A", "B"))
dt.ft <- compose(x = dt.ft, j = 1, value = as_paragraph( hyperlink_text(x = A, url = link)))
d
那很好。但是您可以看到,“ A”列中有逗号分隔的值,而“ link”列中有逗号分隔的链接。如何使对应的超链接显示在同一单元格中 因此,弹性表格的“ A”列为第二行,而“ b”和“ c”超链接为“ www.b.com”和“ www.c.com”
如果还有其他设计DF的方法,我可以做到。它只是我有动态的内容,并且会有所不同,即。我不会事先知道链接的数量。
答案 0 :(得分:0)
到目前为止,这是不可能的。解决的办法是创建多个具有相似内容的行,但链接和链接值除外,然后合并这些行。这样,您将在垂直方向上为合并的行拥有多个超链接。
答案 1 :(得分:0)
对于像这样的不规则结构,您需要特定的功能。
library(flextable)
df = data.frame("A" = c("a","b, c","c","d, e, f"),
"B" = c(1,2,3,4),
"link" = c("http://www.a.com", "http://www.b.com, http://www.c.com",
"http://www.c.com", "http://www.d.com, http://www.e.com, http://www.f.com"),
stringsAsFactors = FALSE)
ft <- flextable(data = df, col_keys = c("A", "B"))
multi_hyperlink_text <- function(labels, links){
out <- mapply(
function(text, url){
dat <- hyperlink_text(text, url = url)
dat <- split(dat, seq_along(text))
as_paragraph(list_values = dat)
},
text = strsplit(labels, split = ","),
url = strsplit(links, split = ","), SIMPLIFY = FALSE, USE.NAMES = FALSE
)
# the following is necessary to comply with expected
# flextable structure !
Reduce(append, out)
}
ft <- compose(x = ft, j = 2, value = multi_hyperlink_text(A, link))
ft <- color(x = ft, j = 2, color = "#0077CC")
ft <- add_footer_lines(x = ft, "column B is made of links")
ft