knitr: code in chunks gets wrapped unexpectedly

谁都会走 提交于 2019-12-02 05:27:09

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}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!