0

I tried the code below for giving hyperlinks to another file

library(xlsx)

wb <- createWorkbook()
sheet1 <- createSheet(wb, "Sheet1")
rows <- createRow(sheet1, 1:10) # 10 rows
cells <- createCell(rows, colIndex=1:8) # 8 columns
links <- c("D://r datasets/sales data.xlsx")
names(links) <- c("hyperlinks")
for (row in 1:length(links)) {
  setCellValue(cells[[row,1]], names(links)[row])
  addHyperlink(cells[[row,1]], links[row])
}
saveWorkbook(wb, "links.xlsx")
shell.exec("links.xlsx")

but am getting error

Error in .jcall(cell, "V", "setHyperlink", link) : java.lang.IllegalArgumentException: Address of hyperlink must be a valid URI

lmo
  • 37,904
  • 9
  • 56
  • 69

1 Answers1

0

Problem seems to be with the spaces. Replace the links with

links <- c("D:/rdatasets/salesdata.xlsx")

and it works. If you really need the spaces, you should be able to encode them them as %20, and it seems that excel figures it out.

links <- c("D:/r%20datasets/sales%20data.xlsx")
DGKarlsson
  • 1,091
  • 12
  • 18