Make all row-entries in a column hyperlinks (Excel file production in R) -


many people use excel , exporting xlsx unfortunately important. want produce "user friendly data outputs" have hyperlinks.

how can produce in r excel file 2 columns first column hyperlinked (the desired link specified in third column). (this on demo data)

(in read dataset , output file,i need 10 such columns "hyperlink enhanced"). has excel! html not cut it! (data set 20k rows , browser freeze , scrolling , column freezing important)

see sample data here:

library(dplyr) df<-select(iris,species,sepal.length) library(stringr) df$hyperlink <-str_c('http://species.org/',df$species)  > df[49:54,]       species sepal.length                     hyperlink 49     setosa          5.3     http://species.org/setosa 50     setosa          5.0     http://species.org/setosa 51 versicolor          7.0 http://species.org/versicolor 52 versicolor          6.4 http://species.org/versicolor 53 versicolor          6.9 http://species.org/versicolor 54 versicolor          5.5 http://species.org/versicolor 

edit: ideally, code allow making column in data.frame link specified formula (see formula in code use function str_c ). assume df has 20+ columns , manually composing again (for sake of xlsx should best avoided).

solution in xlxs fine, potentially looking other packages (or doing in python) option.

it should this:

enter image description here

there method in xlsx package adds hyperlinks cells. here how add hyperlinks using loop.

if else knows how without usage of loop (i.e. adding entire vector column of cells) bit more efficient.

library(xlsx) wb <- createworkbook() sheet1 <- createsheet(wb, "sheet1") rows <- createrow(sheet1, seq_along(df$hyperlink))  cells <- createcell(rows, colindex=1:2) # 2 columns (i in seq_along(cells[,1])){   col1 <- cells[[i,1]]   setcellvalue(col1, df$species[i])   addhyperlink(col1,df$hyperlink[i])   col2 <- cells[[i,2]]   setcellvalue(col2,df$sepal.length[[i]]) } saveworkbook(wb, file="workbook.xlsx") # add file path here 

enter image description here


Comments