zoukankan      html  css  js  c++  java
  • 使用LaTeX和KnitR自动生成报告

    扩展名为.Rnw(Rtex)的文件就是包含了R代码的LaTeX文档。编译的时候,先用Rscript调用Knitr处理,生成.TeX文档,然后用pdfLaTeX/XeLaTeX编译成PDF。
    最方便的编译.Rnw文档的工具是使用RStudio!(TeXStudio/SublimeText/VSC都比较慢),TeXStudio(2.12.16)最适合编写.Rnw文档
    谢益辉为LyX制作了可用Knitr的模板,使用也十分方便。

    Knitr主页:https://yihui.name/knitr/
    Knitr示例:https://yihui.name/knitr/demos/
    编辑器配置:https://yihui.name/knitr/demo/editors/

    下面结合一些工具说明Knitr的使用方法。在TeXStudio/ST/VSC中编辑,在RStudio编译!

    使用RStudio

    1. 安装某个LaTeX发行版,如TeXLive(https://tug.org/texlive/acquire.html)
    2. 安装R(https://r-project.org),并安装KnitR包
      install.packages('knitr')
      
    3. 安装RStudio Desktop(https://rstudio.com/)
    4. 启动RStudio,点击“Tools-Global setting: Sweave”:
      • Wave Rnw files using "Knitr"
      • Typeset LaTeX into PDF using "pdfLaTeX"(中文选用"XeLaTeX")
    5. 新建Rnw文件:“File-New File-R Sweave”,在文档区输入
      <<>>=
      plot(rnorm(100),type='l')
      @
      
      保存文件为test.rnw。扩展名必须为.rnw!
    6. 点击"Compile PDF",生成PDF文档。

    RStudio的优点:方便的编译环境;R代码自动提示;样式比较漂亮。
    :没有LaTeX自动补全、提示。
    上面步骤1、2是必须的。

    使用 TeXStudio

    • "Options-Config-Build:user Commands"添加命令Knitr:Knitr,命令为
      Rscript -e "library(knitr); knit('%.Rnw')"|xelatex -synctex=1 -shell-escape -interaction=nonstopmode %.tex | txs:///view-pdf

      Rscript -e "library(knitr); knit('%.Rnw')" | txs:///xelatex | txs:///xelatex | txs:///view-pdf
      如果有参考文献,用如下命令:
      Rscript -e "library(knitr); knit('%.Rnw')"|xelatex %.tex |bibtex %|xelatex -synctex=1 -shell-escape -interaction=nonstopmode %.tex | txs:///view-pdf

      Rscript -e "library(knitr); knit('%.Rnw')" | txs:///xelatex | txs:///bibtex | txs:///xelatex | txs:///view-pdf
    • 文件扩展名必须为.rnw!
    • "Tools - user:Knitr"编译文件(要定义快捷键!)。

    TeXStudio(2.12.16)的优点:方便的编译环击;LaTeX提示、自动补全;样式比较漂亮。
    不足:没有R代码自动提示补全。感觉是最好的.Rnw的编辑/译器

    编译问题
    设置后,编译总是先调用pdflatex,然后才是rscript??

    如果用knit2pdf替换knit,XeLaTeX编译时总提示:"tlmgr search --file --global "/article.cls",然后寻找文件或安装(导致速度慢)。

    TeXStudio的配色:https://tex.stackexchange.com/questions/108315/how-can-i-set-a-dark-theme-in-texstudio
    TeXStudio的配置文件为config eXStudio.ini,配色部分位于[formats]
    使用配色

    1. 打开TeXStudio,"Options > Save profile",保存为user-default.txsprofile
    2. 再执行上面操作,保存为custom-dark1.txsprofile
    3. 打开custom-dark1.txsprofile,并找到[formats]段(在最后),会看到
      [formats]
      version=1.0
      
    4. 将其他的配色设置复制粘贴到其后,保存。
    5. TeXStudio中执行:"Options > Load profile",找到保存的custom-dark1.txsprofile,确定。
    6. 关闭并重启TeXStudio,打开一个文件,就能看到配色生效。
      PS:以前都是将配色设置复制到teXstudio.ini后的[formats]段。

    使用Sublime Text 3

    • 安装Knitr插件(以及R-box, senttextplus, latexing/latextools)
    • 文件扩展名必须为.Rnw或.Rmd!(是大写的R)

    能识别.Rnw的样式。
    "set syntax:latexing(knitr)" 或 latex后,按LaTeX/R文档处理,有自动完成、提示。

    VS code

    LaTeX workshop 8.4.2增加了.rnw格式的支持。或ctrl+shift+p“change language mode”中选R Sweave(也支持R markdown)。
    settings.json的对应位置添加如下设置:

    "latex-workshop.latex.recipes": [
     {
       "name": "Knitr",
       "tools": [
         "Rscript",
         "xelatex"
       ]
     },
     {
       "name": "Knitr (biblatex)",
       "tools": [
         "Rscript",
         "xelatex",
         "biblatex",
         "xelatex"
       ]
     }
    ],
    
    "latex-workshop.latex.tools":[
     {
     "name": "Rscript",
     "command": "Rscript",
     "args": [
       "-e",
       "library(knitr); knit('%DOC%')",
     ],
     "env": {}
     }
    ],
    

    %DOC%=%DOCFILE%,都是全名?vscode传递给xelatex的文件是"*.rnw.tex"
    此处取文件名的变量${fileBasenameNoExtension}不能被识别!

    使用LyX

    直接使用模板即可。
    以前常用LyX,后来发现还是不用这种可视化的比较好些,现在基本使用VS code编辑LaTeX。

    示例文档

    %最小的例子实现Latex-Knitr
    %在Rstudio中,新建Rsweave文件并保存为.rnw
    %complile PDF
    % https://texblog.org/2013/08/20/rknitr-automatic-bibliography-generation-with-biblatex-in-rstudio/
    documentclass{article}
    usepackage{hyperref}
    usepackage[backend=bibtex, sorting=none]{biblatex}
    ibliography{references}
     
    egin{filecontents*}{references.bib}
    @Manual{knitr2013,
        title = {knitr: A general-purpose package for dynamic report
          generation in R},
        author = {Yihui Xie},
        year = {2013},
        note = {R package version 1.4.1},
        url = {http://yihui.name/knitr/},
      }
    end{filecontents*}
     
    egin{document}
     
    section*{Automatic biblatex bibliography generation in RStudio using knitr}
     
    <<setup, include=FALSE, cache=FALSE, echo=FALSE>>=
    opts_chunk$set(fig.path='figures/plots-', fig.align='center', fig.show='hold', eval=TRUE, echo=TRUE)
    options(replace.assign=TRUE,width=80)
    Sys.setenv(TEXINPUTS=getwd(),
               BIBINPUTS=getwd(),
               BSTINPUTS=getwd())
    @
     
    <<sample-data-hist-and-box, out.width='0.48\textwidth'>>=
    sampleData <- rnorm(1000, 0,1)
    hist(sampleData)
    boxplot(sampleData)
    @
     
    This document was produced in RStudio using the knitr package cite{knitr2013} by url{http://texblog.org}.
     
    printbibliography
     
    end{document}
    
  • 相关阅读:
    强大的C#图形处理组件
    Spring整合Mybatis的注意事项
    Umbraco常见陷阱与错误模式
    Umbraco 官网阅读理解
    Umbraco模型默认属性
    无缝隙滚动跑马灯组件
    这才是正确删除 office 的方式
    SVN利用Hooks自动发布网站
    Umbraco安装权限问题
    EPiServer网文
  • 原文地址:https://www.cnblogs.com/ourweiguan/p/11694488.html
Copyright © 2011-2022 走看看