有没有一种方法可以将html文件,例如https://cran.r-project.org/web/packages/tidytext/vignettes/tidytext.html,转换为可执行的R Markdown文件(rmd)?
这是我使用的解决方案:
pandoc ./test.html -o test.md
mv test.md test.rmd
# 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 '![](data:image' (html plot)
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
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
<!-- -->
为 ```{r}
并完成代码块与 ```
.md
)就足够了,那么如果你还没有安装的话,请下载并安装pandoc
。然后从命令行运行该命令,或从R中使用system("pandoc ...")
或shell("pandoc ...")
。pandoc https://cran.r-project.org/web/packages/tidytext/vignettes/tidytext.html -o out.md
:~$ ## convert .html to .md :
:~$ pandoc Assessment-Week2B.html -o Assessment-Week2B.md
:~$
:~$ ## rename .md to .rmd
:~$ mv Assessment-Week2B.md Assessment-Week2B.rmd
:~$
:~$ ## edit via RStudio
:~$ rstudio Assessment-Week2B.Rmd
尝试通过终端进行以下简短的MV修改,但通过RStudio进行修改会更容易。