使列中的所有行条目超链接(R中的Excel文件生成)

很多人使用Excel和输出到xlsx不幸的是重要的。 我想生成具有超链接的“用户友好的数据输出”。

如何在R中生成一个带有2列的Excel文件,其中第一列将被超链接(所需链接在第三列中指定)。 (这是所有演示数据)

(在我读的数据集和输出文件中,我需要大约10个这样的列“超链接增强”)。 它必须是一个优秀的! HTML不会削减它! (数据集是20k行,浏览器将冻结,滚动和列冻结是重要的)

在这里查看示例数据:

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 

编辑:理想情况下,代码将允许在data.frame中的任何列由公式指定的链接(请参阅代码中使用函数str_c的公式)。 假设df有20多列,并且再次手动编写(最好避免使用xlsx)。

在xlxs中的解决scheme是好的,但也可能考虑到其他包(或在Python中)。

它应该是这样的:

在这里输入图像说明

xlsx包中有一个将超链接添加到单元格的方法。 这里是如何使用for循环添加超链接。

如果其他人知道如何做到这一点,而不使用for循环(即将整个向量作为一列单元格添加),这样会更高效一些。

 library(xlsx) wb <- createWorkbook() sheet1 <- createSheet(wb, "Sheet1") rows <- createRow(sheet1, seq_along(df$hyperlink)) cells <- createCell(rows, colIndex=1:2) # 2 columns for (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 your file path here 

在这里输入图像描述