简体   繁体   English

flextable的给定单元格中的多个超链接

[英]Multiple hyperlinks in a given cell of a flextable

I have a data frame 我有一个数据框

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

AB link AB链接

a 1 www.a.com 1 www.a.com

b,c 2 www.b.com, www.c.com b,c 2 www.b.com,www.c.com

c 3 www.c.com c 3 www.c.com

d,e,f 4 www.d.com, www.e.com, www.f.com d,e,f 4 www.d.com,www.e.com,www.f.com

I make the format table 我制作格式表

dt.ft <- regulartable(data = dt[, list(A, B, link)])

I want to have the values in column "A" hyperlinked with corresponding values in "link" column, which I did with the help of @DavidGohel using following command 我希望列“A”中的值与“link”列中的相应值进行超链接,我在@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 d

That works fine. 这很好。 But as you can see there are comma separated values in column "A" and comma separated links in "link" column. 但正如您所见,“A”列中有逗号分隔值,“link”列中有逗号分隔的链接。 How can I make the correspsoding hyperlink and show in the same cell So, the flextable would have a column "A" and second row would have "b" and "c" hyperlinked to "www.b.com" and "www.c.com" 如何制作相应的超链接并显示在同一单元格中因此,flextable将有一列“A”,第二行将具有“b”和“c”超链接到“www.b.com”和“www.c” .COM”

If there is any other alternative to design the DF, I can do that. 如果有任何其他替代设计DF,我可以做到这一点。 Its just I have dynamic content and it would vary from different values, ie. 它只是我有动态内容,它会因不同的值而异,即。 i wouldnt know the number of the links prior hand. 我不知道之前的链接数量。

As of now, that is not possible. 截至目前,这是不可能的。 The way around would be the create multiple rows with similar content, except for the link and link value and then merge these rows. 解决方法是创建具有相似内容的多行,除了链接和链接值,然后合并这些行。 that way you would have multiple hyperlinks in vertical order for a merged row. 这样,对于合并的行,您将按垂直顺序拥有多个超链接。

For an irregular structure like this one, you need a specific function. 对于像这样的不规则结构,您需要一个特定的功能。

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM