mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-09 18:31:04 +00:00
Add support for R package knitr (a Sweave alternative)
This fixes #7887. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40674 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
99c8eecaf9
commit
7335b3193b
@ -252,8 +252,9 @@ dist_examples_DATA = \
|
||||
examples/iecc05.fen \
|
||||
examples/iecc07.fen \
|
||||
examples/iecc12.fen \
|
||||
examples/instant_preview.lyx \
|
||||
examples/landslide.lyx \
|
||||
examples/instant_preview.lyx \
|
||||
examples/knitr.lyx \
|
||||
examples/lilypond.lyx \
|
||||
examples/linguistics.lyx \
|
||||
examples/listerrors.lyx \
|
||||
@ -1468,6 +1469,7 @@ dist_layouts_DATA =\
|
||||
layouts/svmult.layout \
|
||||
layouts/svprobth.layout \
|
||||
layouts/sweave.module \
|
||||
layouts/knitr.module \
|
||||
layouts/tabs-within-sections.module \
|
||||
layouts/tarticle.layout \
|
||||
layouts/tbook.layout \
|
||||
@ -1502,6 +1504,7 @@ dist_layouts_DATA =\
|
||||
scriptsdir = $(pkgdatadir)/scripts
|
||||
dist_scripts_DATA = \
|
||||
scripts/bash_completion \
|
||||
scripts/lyxknitr.R \
|
||||
scripts/lyxstangle.R \
|
||||
scripts/lyxsweave.R
|
||||
# We use DATA now instead of PYTHON because automake 1.11.2 complains.
|
||||
|
@ -548,6 +548,7 @@ def checkFormatEntries(dtl_tools):
|
||||
\Format literate nw NoWeb N "" "%%" "document,menu=export"
|
||||
\Format sweave Rnw "Sweave" S "" "%%" "document,menu=export"
|
||||
\Format r R "R/S code" "" "" "%%" "document,menu=export"
|
||||
\Format knitr Rnw "Rnw (knitr)" "" "" "%%" "document,menu=export"
|
||||
\Format lilypond ly "LilyPond music" "" "" "%%" "vector"
|
||||
\Format lilypond-book lytex "LilyPond book (LaTeX)" "" "" "%%" "document,menu=export"
|
||||
\Format latex tex "LaTeX (plain)" L "" "%%" "document,menu=export"
|
||||
@ -669,10 +670,19 @@ def checkConverterEntries():
|
||||
\converter sweave pdflatex "%%" ""
|
||||
\converter sweave xetex "%%" ""
|
||||
\converter sweave luatex "%%" ""'''])
|
||||
#
|
||||
checkProg('a knitr -> LaTeX converter', ['Rscript --verbose --no-save --no-restore $$s/scripts/lyxknitr.R $$p$$i $$p$$o $$e $$r'],
|
||||
rc_entry = [r'''\converter knitr latex "%%" ""
|
||||
\converter knitr pdflatex "%%" ""
|
||||
\converter knitr xetex "%%" ""
|
||||
\converter knitr luatex "%%" ""'''])
|
||||
#
|
||||
checkProg('a Sweave -> R/S code converter', ['Rscript --verbose --no-save --no-restore $$s/scripts/lyxstangle.R $$i $$e $$r'],
|
||||
rc_entry = [ r'\converter sweave r "%%" ""' ])
|
||||
#
|
||||
checkProg('a knitr -> R/S code converter', ['Rscript --verbose --no-save --no-restore $$s/scripts/lyxknitr.R $$p$$i $$p$$o $$e $$r tangle'],
|
||||
rc_entry = [ r'\converter knitr r "%%" ""' ])
|
||||
#
|
||||
checkProg('an HTML -> LaTeX converter', ['html2latex $$i', 'gnuhtml2latex $$i',
|
||||
'htmltolatex -input $$i -output $$o', 'htmltolatex.jar -input $$i -output $$o'],
|
||||
rc_entry = [ r'\converter html latex "%%" ""' ])
|
||||
|
246
lib/examples/knitr.lyx
Normal file
246
lib/examples/knitr.lyx
Normal file
@ -0,0 +1,246 @@
|
||||
#LyX 2.1 created this file. For more info see http://www.lyx.org/
|
||||
\lyxformat 424
|
||||
\begin_document
|
||||
\begin_header
|
||||
\textclass article
|
||||
\use_default_options true
|
||||
\begin_modules
|
||||
knitr
|
||||
\end_modules
|
||||
\maintain_unincluded_children false
|
||||
\language english
|
||||
\language_package default
|
||||
\inputencoding auto
|
||||
\fontencoding global
|
||||
\font_roman palatino
|
||||
\font_sans lmss
|
||||
\font_typewriter lmtt
|
||||
\font_default_family default
|
||||
\use_non_tex_fonts false
|
||||
\font_sc false
|
||||
\font_osf false
|
||||
\font_sf_scale 100
|
||||
\font_tt_scale 100
|
||||
|
||||
\graphics default
|
||||
\default_output_format default
|
||||
\output_sync 0
|
||||
\bibtex_command default
|
||||
\index_command default
|
||||
\paperfontsize default
|
||||
\spacing single
|
||||
\use_hyperref false
|
||||
\papersize default
|
||||
\use_geometry true
|
||||
\use_package amsmath 1
|
||||
\use_package esint 1
|
||||
\use_package mathdots 1
|
||||
\use_package mathtools 0
|
||||
\use_package mhchem 1
|
||||
\use_package undertilde 1
|
||||
\cite_engine basic
|
||||
\cite_engine_type numerical
|
||||
\biblio_style plain
|
||||
\use_bibtopic false
|
||||
\use_indices false
|
||||
\paperorientation portrait
|
||||
\suppress_date false
|
||||
\justification true
|
||||
\use_refstyle 1
|
||||
\index Index
|
||||
\shortcut idx
|
||||
\color #008000
|
||||
\end_index
|
||||
\leftmargin 2.5cm
|
||||
\topmargin 2.5cm
|
||||
\rightmargin 2.5cm
|
||||
\bottommargin 2.5cm
|
||||
\secnumdepth 3
|
||||
\tocdepth 3
|
||||
\paragraph_separation indent
|
||||
\paragraph_indentation default
|
||||
\quotes_language english
|
||||
\papercolumns 1
|
||||
\papersides 1
|
||||
\paperpagestyle default
|
||||
\tracking_changes false
|
||||
\output_changes false
|
||||
\html_math_output 0
|
||||
\html_css_as_file 0
|
||||
\html_be_strict false
|
||||
\end_header
|
||||
|
||||
\begin_body
|
||||
|
||||
\begin_layout Title
|
||||
Using knitr with LyX
|
||||
\end_layout
|
||||
|
||||
\begin_layout Author
|
||||
Yihui Xie
|
||||
\begin_inset Foot
|
||||
status collapsed
|
||||
|
||||
\begin_layout Plain Layout
|
||||
Department of Statistics, Iowa State University.
|
||||
Email:
|
||||
\begin_inset CommandInset href
|
||||
LatexCommand href
|
||||
name "xie@yihui.name"
|
||||
target "xie@yihui.name"
|
||||
type "mailto:"
|
||||
|
||||
\end_inset
|
||||
|
||||
|
||||
\end_layout
|
||||
|
||||
\end_inset
|
||||
|
||||
|
||||
\end_layout
|
||||
|
||||
\begin_layout Standard
|
||||
The R (
|
||||
\begin_inset Flex URL
|
||||
status collapsed
|
||||
|
||||
\begin_layout Plain Layout
|
||||
|
||||
http://www.r-project.org
|
||||
\end_layout
|
||||
|
||||
\end_inset
|
||||
|
||||
) package
|
||||
\series bold
|
||||
knitr
|
||||
\series default
|
||||
is an alternative tool to Sweave based on a different design with more
|
||||
features.
|
||||
LyX has native support to Sweave since version 2.0.0, and the support to
|
||||
|
||||
\series bold
|
||||
knitr
|
||||
\series default
|
||||
was also added since 2.0.3.
|
||||
The usage is basically the same as the
|
||||
\family sans
|
||||
Sweave
|
||||
\family default
|
||||
module
|
||||
\begin_inset Foot
|
||||
status open
|
||||
|
||||
\begin_layout Plain Layout
|
||||
read the LyX/Sweave manual from
|
||||
\family sans
|
||||
Help\SpecialChar \menuseparator
|
||||
Specific Manuals
|
||||
\end_layout
|
||||
|
||||
\end_inset
|
||||
|
||||
:
|
||||
\end_layout
|
||||
|
||||
\begin_layout Enumerate
|
||||
Open a new LyX document;
|
||||
\end_layout
|
||||
|
||||
\begin_layout Enumerate
|
||||
Go to
|
||||
\family sans
|
||||
Document\SpecialChar \menuseparator
|
||||
Settings\SpecialChar \menuseparator
|
||||
Modules
|
||||
\family default
|
||||
and insert the module named
|
||||
\family sans
|
||||
Rnw (knitr)
|
||||
\family default
|
||||
;
|
||||
\end_layout
|
||||
|
||||
\begin_layout Enumerate
|
||||
Then insert R code in the document with either
|
||||
\family sans
|
||||
Insert\SpecialChar \menuseparator
|
||||
TeX Code
|
||||
\family default
|
||||
or the
|
||||
\family sans
|
||||
Chunk
|
||||
\family default
|
||||
environment;
|
||||
\end_layout
|
||||
|
||||
\begin_layout Standard
|
||||
The package website
|
||||
\begin_inset Flex URL
|
||||
status collapsed
|
||||
|
||||
\begin_layout Plain Layout
|
||||
|
||||
http://yihui.github.com/knitr
|
||||
\end_layout
|
||||
|
||||
\end_inset
|
||||
|
||||
has full documentation and demos of
|
||||
\series bold
|
||||
knitr
|
||||
\series default
|
||||
; many of the examples have links to the LyX source documents.
|
||||
\end_layout
|
||||
|
||||
\begin_layout Standard
|
||||
Note the
|
||||
\series bold
|
||||
knitr
|
||||
\series default
|
||||
package requires R >= 2.14.1, so you need to update R if you are using an
|
||||
old version of R.
|
||||
Here we show one chunk as a simple example:
|
||||
\end_layout
|
||||
|
||||
\begin_layout Standard
|
||||
\begin_inset ERT
|
||||
status open
|
||||
|
||||
\begin_layout Plain Layout
|
||||
|
||||
<<show-off>>=
|
||||
\end_layout
|
||||
|
||||
\begin_layout Plain Layout
|
||||
|
||||
rnorm(5)
|
||||
\end_layout
|
||||
|
||||
\begin_layout Plain Layout
|
||||
|
||||
df=data.frame(y=rnorm(100), x=1:100)
|
||||
\end_layout
|
||||
|
||||
\begin_layout Plain Layout
|
||||
|
||||
summary(lm(y~x, data=df))
|
||||
\end_layout
|
||||
|
||||
\begin_layout Plain Layout
|
||||
|
||||
@
|
||||
\end_layout
|
||||
|
||||
\end_inset
|
||||
|
||||
|
||||
\end_layout
|
||||
|
||||
\begin_layout Standard
|
||||
Please contact the package author in case of any problems.
|
||||
\end_layout
|
||||
|
||||
\end_body
|
||||
\end_document
|
82
lib/layouts/knitr.module
Normal file
82
lib/layouts/knitr.module
Normal file
@ -0,0 +1,82 @@
|
||||
#\DeclareLyXModule[knitr->latex]{Rnw (knitr)}
|
||||
#DescriptionBegin
|
||||
#Uses the knitr package in R for dynamic report generation. This R package has to be installed for this module to work: install.packages('knitr'). Note it depends on R >= 2.14.1.
|
||||
#For more info see http://yihui.github.com/knitr
|
||||
#DescriptionEnd
|
||||
#Category: literate
|
||||
#Excludes: lilypond | sweave
|
||||
|
||||
Format 35
|
||||
OutputType literate
|
||||
OutputFormat knitr
|
||||
|
||||
Style Chunk
|
||||
Category Sweave
|
||||
LatexType Paragraph
|
||||
LatexName dummy
|
||||
Margin static
|
||||
Align Left
|
||||
AlignPossible Block, Left, Right, Center
|
||||
TopSep 0.7
|
||||
BottomSep 0.7
|
||||
NewLine 0
|
||||
FreeSpacing 1
|
||||
PassThru 1
|
||||
ParbreakIsNewline 1
|
||||
Spellcheck 0
|
||||
## What is LabelType used for?
|
||||
LabelType Static
|
||||
TextFont
|
||||
Color latex
|
||||
Family Typewriter
|
||||
EndFont
|
||||
End
|
||||
|
||||
Style Scrap
|
||||
ObsoletedBy Chunk
|
||||
End
|
||||
|
||||
InsetLayout "Flex:Sweave Options"
|
||||
LabelString "Sweave opts"
|
||||
LatexType Command
|
||||
LaTexName SweaveOpts
|
||||
LyXType Custom
|
||||
Decoration Classic
|
||||
Font
|
||||
Color latex
|
||||
Family typewriter
|
||||
EndFont
|
||||
LabelFont
|
||||
Color latex
|
||||
Size Small
|
||||
EndFont
|
||||
MultiPar false
|
||||
PassThru 1
|
||||
ParbreakIsNewline 1
|
||||
Spellcheck 0
|
||||
FreeSpacing true
|
||||
ForceLTR true
|
||||
End
|
||||
|
||||
InsetLayout "Flex:S/R expression"
|
||||
LabelString "S/R expr"
|
||||
LatexType Command
|
||||
LaTexName Sexpr
|
||||
LyXType Custom
|
||||
Decoration Conglomerate
|
||||
Font
|
||||
Color latex
|
||||
Family typewriter
|
||||
EndFont
|
||||
LabelFont
|
||||
Color latex
|
||||
Size Small
|
||||
EndFont
|
||||
MultiPar false
|
||||
PassThru 1
|
||||
ParbreakIsNewline 1
|
||||
Spellcheck 0
|
||||
FreeSpacing true
|
||||
ForceLTR true
|
||||
End
|
||||
|
48
lib/scripts/lyxknitr.R
Normal file
48
lib/scripts/lyxknitr.R
Normal file
@ -0,0 +1,48 @@
|
||||
## This program is free software; you can redistribute it and/or
|
||||
## modify it under the terms of the GNU General Public License as
|
||||
## published by the Free Software Foundation; either version 2 of the
|
||||
## License, or (at your option) any later version.
|
||||
##
|
||||
## This program is distributed in the hope that it will be useful, but
|
||||
## WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
## General Public License for more details.
|
||||
|
||||
## author Yihui Xie
|
||||
|
||||
## knitr is an alternative package to Sweave, and has more features
|
||||
## and flexibility; see https://yihui.github.com/knitr
|
||||
|
||||
## Rscript $$s/scripts/lyxknitr.R $$p$$i $$p$$o $$e $$r
|
||||
## $$p the path of the output (temp dir)
|
||||
## $$i the file name of the input Rnw
|
||||
## $$o the tex output
|
||||
## $$r path to the original input file (the lyx document)
|
||||
## $$e encoding (e.g. 'UTF-8')
|
||||
|
||||
library(knitr)
|
||||
|
||||
.cmdargs = commandArgs(TRUE)
|
||||
|
||||
.orig.enc = getOption("encoding")
|
||||
options(encoding = .cmdargs[3])
|
||||
|
||||
## the working directory is the same with the original .lyx file; you
|
||||
## can put your data files there and functions like read.table() can
|
||||
## work correctly without specifying the full path
|
||||
setwd(.cmdargs[4])
|
||||
|
||||
## copy the Rnw file to the current working directory if it does not exist
|
||||
.tmp.file = tempfile(); .rnw.file = basename(.cmdargs[1])
|
||||
.rnw.exists = file.exists(.rnw.file)
|
||||
if (.rnw.exists) file.rename(.rnw.file, .tmp.file)
|
||||
file.copy(.cmdargs[1], '.')
|
||||
## run knit() to get .tex or .R
|
||||
knit(.rnw.file, tangle = 'tangle' %in% .cmdargs)
|
||||
|
||||
setwd(.cmdargs[4])
|
||||
## remove the copied .Rnw if it did not exist, otherwise move the original one back
|
||||
if (.rnw.exists) file.rename(.tmp.file, .rnw.file) else unlink(.rnw.file)
|
||||
file.rename(basename(.cmdargs[2]), .cmdargs[2]) # move .tex to the temp dir
|
||||
rm(.tmp.file, .rnw.file, .rnw.exists) # clean up these variables
|
||||
|
Loading…
Reference in New Issue
Block a user