knitr:块中的代码被意外包装 [英] knitr: code in chunks gets wrapped unexpectedly
问题描述
会发现块中的代码被包装,尽管我已经设置了
tidy = FALSE
全球。例如,这个块: \item使用\func {glm}:
<<< ; berk-logit2,size ='footnotesize'>> =
berkeley< - as.data.frame(UCBAdmissions)
berk.logit2< - glm(Admit ==Admitted Dept + Gender,
data = berkeley,weights = Freq,family =binomial)
@
显示如下:
<请注意,所有三行都被包装,就像在段落模式下一样。代码块中的缩进行使用空格,而不是制表符。
当我查看产生的.tex文件时,没有什么奇怪的,就是给< c $ c> alltt 看起来OK。
\item使用\func {glm }:
\ begin {knitrout} \ footnotesize
\ definecolor {shadecolor} {rgb} {1,0.961,0.933} \color {fgcolor} \ begin {kframe}
\ begin {alltt}
\hlstd {berkeley} \hlkwb {< - } \hlkwd {as.data.frame} \hlstd {(UCBAdmissions)}
\hlstd {berk.logit2} \hlkwb {< - } \hlkwd {glm} \hlstd {(Admit} \hlopt {==} \hlstr {Admitted} \hlopt {〜} \hlstd {Dept} \hlopt {+} \hlstd {Gender,}
\hlkwc {data} \hlstd {= berkeley,} \hlkwc {权重} \hlstd {= Freq,} \ hlkwc {family} \hlstd {=} \hlstr {binomial} \hlstd {)}
\ end {alltt}
\ end {kframe}
\ end {knitrout}
大多数其他块产生格式正确的输出。例如,
<< mice-tab,size ='footnotesize'>> =
data(小鼠,包=vcdExtra)
mice.tab <-xtabs(Freq_ litter +治疗+死亡,数据=小鼠)
ftable(凋落物+治疗〜死亡,数据= mice.tab)
@
给出:
In a beamer presentation using knit2pdf() and LaTeX, I sometimes
find that code in chunks gets wrapped, even though I have set
tidy=FALSE
globally. For example, this chunk:
\item Fit this using \func{glm}:
<<berk-logit2, size='footnotesize'>>=
berkeley <- as.data.frame(UCBAdmissions)
berk.logit2 <- glm(Admit == "Admitted" ~ Dept + Gender,
data = berkeley, weights = Freq, family = "binomial")
@
Appears like this:
Note that all three lines are wrapped, as if in paragraph mode. The indented line in the code chunk uses spaces, not tabs.
When I look at the .tex file produced, nothing looks strange, that is the lines given to alltt
look OK.
\item Fit this using \func{glm}:
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{1, 0.961, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{berkeley} \hlkwb{<-} \hlkwd{as.data.frame}\hlstd{(UCBAdmissions)}
\hlstd{berk.logit2} \hlkwb{<-} \hlkwd{glm}\hlstd{(Admit} \hlopt{==} \hlstr{"Admitted"} \hlopt{~} \hlstd{Dept} \hlopt{+} \hlstd{Gender,}
\hlkwc{data} \hlstd{= berkeley,} \hlkwc{weights} \hlstd{= Freq,} \hlkwc{family} \hlstd{=} \hlstr{"binomial"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
Most other chunks produce the properly formatted output. E.g.,
<<mice-tab, size='footnotesize' >>=
data(Mice, package="vcdExtra")
mice.tab <- xtabs(Freq ~ litter + treatment + deaths, data=Mice)
ftable(litter + treatment ~ deaths, data=mice.tab)
@
gives:
What could be causing this? My setup is complex, so I don't have a MWE, but it would be helpful if I knew what to look for.
You probably use the fragile
frame option on slides where the wrapping works as expected.
Setting \begin{frame}[fragile]
produces the correct result you showed for the mice-tab
chunk:
\documentclass{beamer}
\begin{document}
<<echo = FALSE>>=
library(knitr)
opts_chunk$set(size = "footnotesize",
eval = FALSE,
tidy = FALSE)
@
\begin{frame}[fragile]
With fragile:
<<berk-logit2-fragile>>=
berkeley <- as.data.frame(UCBAdmissions)
berk.logit2 <- glm(Admit == "Admitted" ~ Dept + Gender,
data = berkeley, weights = Freq,
family = "binomial")
@
<<mice-tab-fragile>>=
data(Mice, package="vcdExtra")
mice.tab <- xtabs(Freq ~ litter + treatment + deaths, data=Mice)
ftable(litter + treatment ~ deaths, data=mice.tab)
@
\end{frame}
\begin{frame}
Not fragile:
<<berk-logit2>>=
berkeley <- as.data.frame(UCBAdmissions)
berk.logit2 <- glm(Admit == "Admitted" ~ Dept + Gender,
data = berkeley, weights = Freq,
family = "binomial")
@
<<mice-tab>>=
data(Mice, package="vcdExtra")
mice.tab <- xtabs(Freq ~ litter + treatment + deaths, data=Mice)
ftable(litter + treatment ~ deaths, data=mice.tab)
@
\end{frame}
\end{document}
这篇关于knitr:块中的代码被意外包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!