用R中的相应文本替换多个url

问题描述 投票:0回答:1

我的 Excel 中 A 列中有一系列 url 链接,在 B 列中它们旁边有相应的名称/标题。

例如我的Excel中有

          A                    B
https://www.bbc.co.uk      BBC_news

我想将 A 中的链接重命名为超链接 BBC_news

我有多个链接和名称。在R中有什么办法可以做到这一点吗?

r
1个回答
0
投票

我可以为您提供以下问题解决方案

library(openxlsx)

df <- read.xlsx("your_file.xlsx", colNames = FALSE)
# Create a new workbook to store the hyperlinks
new_workbook <- createWorkbook()
# Add a new sheet to the workbook
addWorksheet(new_workbook, "Hyperlinks")
# Loop through the data frame and add hyperlinks
for (i in 1:nrow(df)) {
  url <- df[i, 1] 
  text <- df[i, 2]  
  hyperlink_formula <- paste0('HYPERLINK("', url, '", "', text, '")')
  writeFormula(new_workbook, "Hyperlinks", hyperlink_formula, startRow = i, startCol = 1)
}
saveWorkbook(new_workbook, "hyperlinked_links.xlsx", overwrite = TRUE)`enter code here`
© www.soinside.com 2019 - 2024. All rights reserved.