有没有办法转换html文件,如https://cran.r-project.org/web/packages/tidytext/vignettes/tidytext.html,并将其转换为可执行的R Markdown文件(rmd)?
小智 6
这是我使用的解决方案:
pandoc ./test.html -o test.md
Run Code Online (Sandbox Code Playgroud)
mv test.md test.rmd
Run Code Online (Sandbox Code Playgroud)
# chunks r marker: replace ' {\.sourceCode \.r}' by '{r}'
sed -i 's/ {\.sourceCode \.r/{r/' test.rmd
# delete lines beginning wit ':::'
sed -i '/^:::/d' test.rmd
# delete lines beginning '
sed -i '/^\!\[\](data:image/d' test.rmd
# delete paragraph separator lines
sed -i '/^=====/d' test.rmd
sed -i '/^-----/d' test.rmd
# replace paragraph marks
#'[1]{.header-section-number}' by '#'
sed -i 's/\[[0-9]\+\]{\.header-section-number}/#/' test.rmd
#'[1.1]{.header-section-number}' by '##'
sed -i 's/\[[0-9]\+\.[0-9]\+\]{\.header-section-number}/##/' test.rmd
#'[1.1.1]{.header-section-number}' by '###'
sed -i 's/\[[0-9]\+\.[0-9]\+\\.[0-9]\+]{\.header-section-number}/###/' test.rmd
Run Code Online (Sandbox Code Playgroud)
echo "$(echo -e "\n" | cat - test.rmd)" > test.rmd
echo "$(echo '---' | cat - test.rmd)" > test.rmd
echo "$(echo 'title: '\"'test'\" | cat - test.rmd)" > test.rmd
echo "$(echo '---' | cat - test.rmd)" > test.rmd
Run Code Online (Sandbox Code Playgroud)
当然,您可以将这些行放在 .sh 中以简化任务
如果 Markdown 文件 ( .md) 就足够了,那么pandoc如果您还没有它,请下载并安装。然后从命令行或使用system("pandoc ...")或shell("pandoc ...")从 R 中运行它。
pandoc https://cran.r-project.org/web/packages/tidytext/vignettes/tidytext.html -o out.md
Run Code Online (Sandbox Code Playgroud)
对于特定文件,可以对源代码和输出部分进行后处理,但需要付出一些额外的努力,这可能是相当大的。
您可以通过以下方式获得 98% 的结果:
为了获得最后 2%,您需要确保 R 代码块被识别:
<!-- -->为 ```{r}并在完成代码块后使用 ```并确保数据按代码要求可用。祝你好运!
*要切换到 Markdown 文档的可视模式,请使用编辑器工具栏右上角带有指南针图标的按钮 - 此处描述: https: //blog.rstudio.com/2020/09/30/rstudio-v1 -4-预览-视觉-markdown-编辑/