library(openxlsx)
# create sample data
df <- data.frame(
site_name = c("Zero Hedge", "Free Software Foundation"),
site_url = c("https://www.zerohedge.com", "https://www.fsf.org")
)
# create and write workbook
wb <- createWorkbook()
addWorksheet(wb, "df_sheet")
class(df$site_url)<-"hyperlink" # mark as a hyperlink
writeData(wb,"df_sheet",df$site_url,startCol = which(colnames(df)=="site_url"), startRow = 2)
writeData(wb, "df_sheet", df) # overwrite the sheet
saveWorkbook(wb, "wb.xlsx", overwrite = TRUE)
2.将超链接应用到site_name列
library(openxlsx)
# create sample data
df <- data.frame(
site_name = c("Zero Hedge", "Free Software Foundation"),
site_url = c("https://www.zerohedge.com", "https://www.fsf.org")
)
# create and write workbook
wb <- createWorkbook()
addWorksheet(wb, "df_sheet")
class(df$site_url)<-"hyperlink" # mark as a hyperlink
writeData(wb,"df_sheet",df$site_url,startCol = which(colnames(df)=="site_name"), startRow = 2)
class(df$site_url)<-"character" # change back to a character which is optional
writeData(wb, "df_sheet", df) # overwrite the sheet to get the new pretty name overlaying the hyperlink
saveWorkbook(wb, "wb.xlsx", overwrite = TRUE)
3.新列,其中的链接应用于site_name中的名称
library(openxlsx)
# create sample data
df <- data.frame(
site_name = c("Zero Hedge", "Free Software Foundation"),
site_url = c("https://www.zerohedge.com", "https://www.fsf.org")
)
df$pretty_link<-df$site_name #new column for pretty link
# create and write workbook
wb <- createWorkbook()
addWorksheet(wb, "df_sheet")
class(df$site_url)<-"hyperlink" # mark as a hyperlink
writeData(wb,"df_sheet",df$site_url,startCol = which(colnames(df)=="pretty_link"), startRow = 2)
class(df$site_url)<-"character" # change back to a character which is optional
writeData(wb, "df_sheet", df) # overwrite the sheet to get the new pretty name overlaying the hyperlink
saveWorkbook(wb, "wb.xlsx", overwrite = TRUE)
3条答案
按热度按时间mkshixfv1#
xlsx
包中有addHyperlink
函数,正是为了这个目的。example(addHyperlink)
应该可以帮助您入门。yacmzcpb2#
现代的方法可能是使用
openxlsx
包。请参阅?makeHyperlinkString
的文档,如下所示:vawmfj5a3#
类似于Excel中的Openxlsx超链接输出显示......我使用openxlsx对此提出了三个解决方案
1.将url列标记为超链接
2.将超链接应用到site_name列
3.新列,其中的链接应用于site_name中的名称