R Excel中Openxlsx超链接输出显示
我试图在一个数据框中加入一个excel表格,它有两列 列A包含商店的名称 列B包含这些存储的URL 我想采取列A,使其成为一个可点击的超链接,所以它不是纯文本,它是一个超链接到商店网站 我尝试使用openxlsx包生成正确的输出 我尝试使用以下代码snipR Excel中Openxlsx超链接输出显示,r,excel,openxlsx,R,Excel,Openxlsx,我试图在一个数据框中加入一个excel表格,它有两列 列A包含商店的名称 列B包含这些存储的URL 我想采取列A,使其成为一个可点击的超链接,所以它不是纯文本,它是一个超链接到商店网站 我尝试使用openxlsx包生成正确的输出 我尝试使用以下代码snip x <- c("https://www.google.com", "https://www.google.com.au") names(x) <- c("google", "google Aus") class(x) <-
x <- c("https://www.google.com", "https://www.google.com.au")
names(x) <- c("google", "google Aus")
class(x) <- "hyperlink"
writeData(wb, sheet = 1, x = x, startCol = 10)
x使用openxlsx
的方法:
library(openxlsx)
library(dplyr)
# 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")
)
# add new column that manually constructs Excel hyperlink formula
# note backslash is required for quotes to appear in Excel
df <- df %>%
mutate(
excel_link = paste0(
"HYPERLINK(\"",
site_url,
"\", \"",
site_name,
"\")"
)
)
# specify column as formula per openxlsx::writeFormula option #2
class(df$excel_link) <- "formula"
# create and write workbook
wb <- createWorkbook()
addWorksheet(wb, "df_sheet")
writeData(wb, "df_sheet", df)
saveWorkbook(wb, "wb.xlsx", overwrite = TRUE)
库(openxlsx)
图书馆(dplyr)
#创建示例数据
df为了使该文件可复制,您能否与head(dput(df))
共享部分df
?是的,我会给您一个reprex。我需要整理数据集。为了举例,我改变了我的数据。我会做一个假人,给你一个reprex。
library(openxlsx)
library(dplyr)
# 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")
)
# add new column that manually constructs Excel hyperlink formula
# note backslash is required for quotes to appear in Excel
df <- df %>%
mutate(
excel_link = paste0(
"HYPERLINK(\"",
site_url,
"\", \"",
site_name,
"\")"
)
)
# specify column as formula per openxlsx::writeFormula option #2
class(df$excel_link) <- "formula"
# create and write workbook
wb <- createWorkbook()
addWorksheet(wb, "df_sheet")
writeData(wb, "df_sheet", df)
saveWorkbook(wb, "wb.xlsx", overwrite = TRUE)