我正在创建一个程序,该程序将生成数据集样本以进行质量审核。实际数据将在我们的内联网上提供。此示例以excel格式输出,具有非常特定的用户友好格式。我想使用XLconnect根据示例添加到Excel文档的超链接。我一直无法使用多次搜索找到答案。我希望这可以使用XLconnect或类似的包,将格式保持在Excel。我的下面的代码只添加了文本,但没有添加超链接......
library(XLConnect)
Full_data_set = read.csv(paste(my.file.location, my.set, sep= "/"))
my.sample <- sample(Full_data_set$groupid, 50)
my.link <- paste("ourwebsite.org/group/" my.sample, sep = "")
wb <- loadWorkbook(filename = "my.file.location/Template2.xlsx",
create = TRUE)
writeWorksheet(wb, my.links, sheet= 1,
startRow=3, startCol=3,
header=FALSE)
saveWorkbook(wb)
答案 0 :(得分:1)
你必须使用
setCellFormula()
library("XLConnect")
df <- data.frame(
v1 = c("wiki", "google", "so"),
v2 = c("https://www.wikipedia.org/",
"https://www.google.com/",
"http://stackoverflow.com/"),
link = NA ,
stringsAsFactors = F)
# Load workbook (create if not existing)
wb <- loadWorkbook("hlink_example.xlsx", create = TRUE)
# Create a sheet
createSheet(wb, name = "Sheet1")
# Create a named region on sheet
createName(wb, name = "Sheet1", formula = "Sheet1!$A$1")
# Write data set to the above defined named region
writeNamedRegion(wb, df, name = "Sheet1")
# Create an excel column ref. for formula and links in data frame
excelCol <- letters[which(names(df) == "v2")]
# Construct the input range & formula
formula <- paste("HYPERLINK(",excelCol, 2:(nrow(df)+1),")", sep = "")
# Create the link column in data frame
df$link <- formula
# Set the cell formulas using index for rows/col
setCellFormula(wb, "Sheet1", 2:(nrow(df)+1), which(names(df) == "link"),
formula)
# Save workbook
saveWorkbook(wb)