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

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

许多人使用 Excel 不幸的是,导出到 xlsx 很重要。 我想生成具有 hyperlinks.

的 "user friendly data outputs"

我如何在 R 中生成一个包含 2 列的 excel 文件,其中第一列将是 hyperlinked(所需的 link 在第三列中指定) . (这都是演示数据)

(在我读取的数据集和输出文件中,我需要大约 10 个这样的列 "hyperlink enhanced")。 它必须是 excel! 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 中的任何列设为由公式指定的 link(请参阅我使用函数 str_c 的代码中的公式)。假设 df 有 20+ 列并再次手动组合它(为了 xlsx 最好避免)。

xlxs 中的解决方案很好,但可能研究其他包(或在 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