R语言 | 如何使用R书写html文档?

更灵活的书写方式,可以直接看3.

1. 可用函数

  • cat()函数
  • writeLines()函数
  • sink()函数重定向输出到HTML文件

小结:cat()适合简单HTML,writeLines()适合多行内容,sink()适合复杂场景。

说明:尽可能不用R包,减少依赖变动风险。

方法1: 使用cat()直接输出

复制代码
cat('<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>', file="output.html")

方法2: 使用writeLines()

复制代码
html_content <- c('<!DOCTYPE html>','<html>','<head>',
                 '<title>My Page</title>','</head>',
                 '<body>','<h1>Hello World</h1>',
                 '</body>','</html>')
writeLines(html_content, "output.html")

方法3: 使用sink()

复制代码
sink("output.html")
cat('<!DOCTYPE html>\n')
cat('<html>\n')
cat('<head>\n')
cat('<title>My Page</title>\n')
cat('</head>\n')
cat('<body>\n')
cat('<h1>Hello World</h1>\n')
cat('</body>\n')
cat('</html>\n')
sink()

2. 逐句拼凑html文件

如果不同R文件、同一个R文件的不同位置都要输出信息到同一个html报告文件中呢?

  • 使用函数 cat的 append=T参数: cat('\n</body>\n</html>', file = filepath, append = TRUE)

(1)先定义库函数:

复制代码
# 初始化HTML文件
init_html <- function(filepath) {
  writeLines('<!DOCTYPE html>\n<html>\n<head>\n<title>Project Output</title>\n</head>\n<body>', 
             filepath)
}

# 添加HTML片段
add_html_section <- function(filepath, content, section_title) {
  section <- paste0('\n<h2>', section_title, '</h2>\n<div>', content, '</div>')
  cat(section, file = filepath, append = TRUE)
}

# 完成HTML文件
finalize_html <- function(filepath) {
  cat('\n</body>\n</html>', file = filepath, append = TRUE)
}

逐个写入函数有局限性,需要定义好h2和子内容。

(2)在不同位置写文档:

复制代码
 项目不同位置使用示例
output_file <- "project_output.html"

# 位置1:初始化文件
init_html(output_file)

# 位置2:数据分析模块
analysis_result <- "<p>数据分析结果...</p>"
add_html_section(output_file, analysis_result, "分析报告")

# 位置3:可视化模块
plot_html <- "<img src='plot.png' alt='分析图表'>"
add_html_section(output_file, plot_html, "可视化结果")

# 位置4:最终完成
finalize_html(output_file)

3. 自由写html文件,自定义各种标签

如果想更自由的写各种html标签呢?

(1)核心函数

复制代码
con <- file(outputFile, "w") #打开文件,如果想追加,使用oepn="a"
writeLines(something, con) #写文本
close(con) #关闭文件

(2)包装函数

复制代码
# functions
html=function(text, tag, fw=con){
    rs=sprintf("<%s>%s</%s>", tag, text, tag)
    writeLines(rs, fw)
}
htmlRaw=function(text, fw=con){
    writeLines(text, fw)
}
# 类似的,可以包装更多函数
h1=function(text){ html(text, "h1")}
h2=function(text){ html(text, "h2")}

R2=function(num){
    round(num, 2)
}

now=function(){
  as.character( format(Sys.time(), '%Y%m%d_%H%M%S') )
}

(3)用法

复制代码
html("End --", "p")

htmlRaw("<div class=box>")

Ref:

相关推荐
iReachers6 小时前
HTML打包APK(安卓APP)中下载功能常见问题和详细介绍
前端·javascript·html·html打包apk·网页打包app·下载功能
Java陈序员6 小时前
告别手写礼簿!一款开源免费的电子红白喜事礼簿系统!
javascript·css·html
唐叔在学习8 小时前
insertAdjacentHTML踩坑实录:AI没搞定的问题,我给搞定啦
前端·javascript·html
小则又沐风a8 小时前
数据结构->链表篇
前端·html
晓得迷路了9 小时前
栗子前端技术周刊第 112 期 - Rspack 1.7、2025 JS 新星榜单、HTML 状态调查...
前端·javascript·html
jinmo_C++9 小时前
从零开始学前端 · HTML 基础篇(一):认识 HTML 与页面结构
前端·html·状态模式
kisshuan1239610 小时前
【深度学习】【目标检测】基于Mask R-CNN的鱼类尾巴检测与识别
深度学习·目标检测·r语言
winfredzhang10 小时前
从零构建:基于 Node.js 的全栈视频资料管理系统开发实录
css·node.js·html·音视频·js·收藏,搜索,缩略图
松涛和鸣1 天前
49、智能电源箱项目技术栈解析
服务器·c语言·开发语言·http·html·php
智航GIS1 天前
10.5 PyQuery:jQuery 风格的 Python HTML 解析库
python·html·jquery