Search code examples
rflextableformattable

hyperlinks to each row in flextable


I have a data frame

df = data.frame("A" = c("a","b","c","d"), "B" = c(1,2,3,4), "link" = c("www.a.com", "www.b.com", "www.c.com", "www.d.com"))

A B link

a 1 www.a.com

b 2 www.b.com

c 3 www.c.com

d 4 www.d.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.

I tried

compose(x = dt.ft, j = "A", value = as_paragraph( hyperlink_text(x = A, url = link)))

and I got following error:

Error in $<-.data.frame(*tmp*, "url", value = c(1L, 2L, 3L, 4L, 1L, : replacement has 16 rows, data has 4

The above R statement works if there is only 1 row in the table, but fails to work on multiple rows. Can you please help me. Also, is there any way to hide/remove the column "link", after linking.


Solution

  • First, there was an issue, the version 0.5.2 should fix it.

    library(flextable)
    
    df = data.frame("A" = c("a","b","c","d"), "B" = c(1,2,3,4), "link" = c("www.a.com", "www.b.com", "www.c.com", "www.d.com"))
    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)))
    dt.ft
    

    The example show also how to select columns you want to be displayed by using argument col_keys.