如何控制rmarkdown/knitr文件的输出顺序(或如何防止浮动)
我遇到的问题与“”中提出的问题类似。在我的代码中,我有一个文本标题,后跟一个块,它使用四个knitr::kable函数调用来显示四个表。奇怪的是,表1和表2显示在标题上方,表3和表4显示在PDF格式的输出中。这似乎与knitr允许对象浮动有关。我可以最好在knitr::kable函数或chunk选项中关闭它吗?这是我的密码:如何控制rmarkdown/knitr文件的输出顺序(或如何防止浮动),r,knitr,r-markdown,R,Knitr,R Markdown,我遇到的问题与“”中提出的问题类似。在我的代码中,我有一个文本标题,后跟一个块,它使用四个knitr::kable函数调用来显示四个表。奇怪的是,表1和表2显示在标题上方,表3和表4显示在PDF格式的输出中。这似乎与knitr允许对象浮动有关。我可以最好在knitr::kable函数或chunk选项中关闭它吗?这是我的密码: --- title: "Reproducible Error" author: "Rex Macey" date: "February 14, 2017" output:
---
title: "Reproducible Error"
author: "Rex Macey"
date: "February 14, 2017"
output: pdf_document
header-includes:
- \usepackage{placeins}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
# Section 1
```{r CompTables, echo=FALSE, warning=FALSE, results='asis', out.extra='FloatBarrier'}
comptables1<-matrix(rep(c(100,200,300,400,500),3),nrow=5,ncol=3)
colnames(comptables1)<-c("Conservative","Moderate","Aggressive")
row.names(comptables1)<-c("5th %-tile","25th %-tile","50th %-tile","75th %-tile","95th %-tile")
comptables2<-matrix(0,nrow=6,ncol=3)
comptables2[1,]<-comptables1[1,]
for (i in 2:5){
comptables2[i,]<-comptables1[i,]-comptables1[i-1,]
}
comptables<-list()
comptables[[1]]<-list(comptables1,comptables2)
comptables[[2]]<-list(comptables1,comptables2)
comptables[[3]]<-list(comptables1,comptables2)
comptables[[4]]<-list(comptables1,comptables2)
knitr::kable(comptables[[1]][1],
caption="Table of Forecast Wealth Values: Suggested One Year",
format.args=list(big.mark=",",floating=FALSE))
knitr::kable(comptables[[2]][1],
caption="Table of Forecast Wealth Values: Suggested Three Years",
format.args=list(big.mark=",",floating=FALSE))
knitr::kable(comptables[[3]][1],
caption="Table of Forecast Wealth Values: Suggested Five Years",
format.args=list(big.mark=",",floating=FALSE))
knitr::kable(comptables[[4]][1],
caption="Table of Forecast Wealth Values: Suggested Ten Years",
format.args=list(big.mark=",",floating=FALSE))
```
```{r CompChart, echo=FALSE, warning=FALSE, fig.height=4, fig.show='hold', results='asis'}
horizons <- c(1,3,5,10)
par(mfrow=c(1,2))
minv<-min(unlist(comptables[[1]][1]),unlist(comptables[[2]][1]),
unlist(comptables[[3]][1]),unlist(comptables[[4]][1]))
maxv<-max(unlist(comptables[[1]][1]),unlist(comptables[[2]][1]),
unlist(comptables[[3]][1]),unlist(comptables[[4]][1]))
for (h in 1:length(horizons)){
wealth.pl.comp<-matrix(unlist(comptables[[h]][2],use.names = FALSE),ncol=3,byrow=FALSE)
colnames(wealth.pl.comp)<-c("Cons.","Mod.","Aggr.")
barplot(wealth.pl.comp,col=c("white","red","yellow","green","blue"),border=NA,
main=paste("Forecast A/T Values -",horizons[h],"Years"),
ylim=c(minv/2,maxv+minv/2))
abline(h=250,col="darkgray")
}
par(mfrow=c(1,1))
```
\newpage
#Section 2
---
标题:“可再现错误”
作者:“雷克斯·梅西”
日期:“2017年2月14日”
输出:pdf\U文件
标题包括:
-\usepackage{placeins}
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
```
\新页
#第一节
```{r CompTables,echo=FALSE,warning=FALSE,results='asis',out.extra='FloatBarrier'}
Comptable1基于,out.extra
提供了“图形的额外选项”
当我将\FloatBarrier
放在块上方而不是使用out.extra
时,我得到的东西更像我认为您想要的东西
---
title: "Reproducible Error"
author: "Rex Macey"
date: "February 14, 2017"
output: pdf_document
header-includes:
- \usepackage{placeins}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
\FloatBarrier
# Section 1
```{r CompTables, echo=FALSE, warning=FALSE, results='asis'}
comptables1<-matrix(rep(c(100,200,300,400,500),3),nrow=5,ncol=3)
colnames(comptables1)<-c("Conservative","Moderate","Aggressive")
row.names(comptables1)<-c("5th %-tile","25th %-tile","50th %-tile","75th %-tile","95th %-tile")
comptables2<-matrix(0,nrow=6,ncol=3)
comptables2[1,]<-comptables1[1,]
for (i in 2:5){
comptables2[i,]<-comptables1[i,]-comptables1[i-1,]
}
comptables<-list()
comptables[[1]]<-list(comptables1,comptables2)
comptables[[2]]<-list(comptables1,comptables2)
comptables[[3]]<-list(comptables1,comptables2)
comptables[[4]]<-list(comptables1,comptables2)
knitr::kable(comptables[[1]][1],
caption="Table of Forecast Wealth Values: Suggested One Year",
format.args=list(big.mark=",",floating=FALSE))
knitr::kable(comptables[[2]][1],
caption="Table of Forecast Wealth Values: Suggested Three Years",
format.args=list(big.mark=",",floating=FALSE))
knitr::kable(comptables[[3]][1],
caption="Table of Forecast Wealth Values: Suggested Five Years",
format.args=list(big.mark=",",floating=FALSE))
knitr::kable(comptables[[4]][1],
caption="Table of Forecast Wealth Values: Suggested Ten Years",
format.args=list(big.mark=",",floating=FALSE))
```
```{r CompChart, echo=FALSE, warning=FALSE, fig.height=4, fig.show='hold', results='asis'}
horizons <- c(1,3,5,10)
par(mfrow=c(1,2))
minv<-min(unlist(comptables[[1]][1]),unlist(comptables[[2]][1]),
unlist(comptables[[3]][1]),unlist(comptables[[4]][1]))
maxv<-max(unlist(comptables[[1]][1]),unlist(comptables[[2]][1]),
unlist(comptables[[3]][1]),unlist(comptables[[4]][1]))
for (h in 1:length(horizons)){
wealth.pl.comp<-matrix(unlist(comptables[[h]][2],use.names = FALSE),ncol=3,byrow=FALSE)
colnames(wealth.pl.comp)<-c("Cons.","Mod.","Aggr.")
barplot(wealth.pl.comp,col=c("white","red","yellow","green","blue"),border=NA,
main=paste("Forecast A/T Values -",horizons[h],"Years"),
ylim=c(minv/2,maxv+minv/2))
abline(h=250,col="darkgray")
}
par(mfrow=c(1,1))
```
\newpage
#Section 2
---
标题:“可再现错误”
作者:“雷克斯·梅西”
日期:“2017年2月14日”
输出:pdf\U文件
标题包括:
-\usepackage{placeins}
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
```
\新页
\浮栅
#第一节
```{r CompTables,echo=FALSE,warning=FALSE,results='asis'}
Comptable1有点迟了,但我刚刚在做这个。加载kableExtra
包,并在kable_样式设置
选项中包含一些限制,似乎可以达到目的。特别是latex\u options=c(“保持位置”)
---
标题:“可再现错误”
作者:“雷克斯·梅西”
日期:“2017年2月14日”
输出:pdf\U文件
标题包括:
-\usepackage{placeins}
---
```{r设置,include=FALSE}
图书馆(“knitr”)
图书馆(“tidyverse”)
图书馆(“kableExtra”)
knitr::opts_chunk$set(echo=TRUE)
```
\新页
#第一节
```{r CompTables,echo=FALSE,warning=FALSE,results='asis',out.extra='FloatBarrier'}
comptable1i通常在我的标题中包含\usepackage{placeins}
,然后将\FloatBarrier
放在节标题之前,以防止浮动环境越过我想要的节之外。我添加了标题包括:-\usepackage{placeins}
并修改了区块的标题,使其看起来像@Benjamin的注释所示的{r,echo=FALSE,warning=FALSE,results='asis',out.extra='FloatBarrier'}
。我仍然有同样的问题。我应该怎么做?如果你提供一个可复制的例子,就更容易提出具体的建议。@Benjamin当你的[sic]正确时,你是对的。我道歉。我用可复制的代码重做了。如果有帮助的话,我的knitr是v1.15.1,rmarkdown是1.3,RStudio是1.0.136,R是3.3.2谢谢你的帮助。我对你的答复速度感到敬畏。你的建议确实产生了一些不同的结果。我曾希望在2x2的网格中有4个表格和4个图表。现在我得到一个1x2行的图表,4个表格,然后是接下来的2个图表。添加另一个\FloatBarrier似乎有效。
---
title: "Reproducible Error"
author: "Rex Macey"
date: "February 14, 2017"
output: pdf_document
header-includes:
- \usepackage{placeins}
---
```{r setup, include=FALSE}
library("knitr")
library("tidyverse")
library("kableExtra")
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
# Section 1
```{r CompTables, echo=FALSE, warning=FALSE, results='asis', out.extra='FloatBarrier'}
comptables1<-matrix(rep(c(100,200,300,400,500),3),nrow=5,ncol=3)
colnames(comptables1)<-c("Conservative","Moderate","Aggressive")
row.names(comptables1)<-c("5th %-tile","25th %-tile","50th %-tile","75th %-tile","95th %-tile")
comptables2<-matrix(0,nrow=6,ncol=3)
comptables2[1,]<-comptables1[1,]
for (i in 2:5){
comptables2[i,]<-comptables1[i,]-comptables1[i-1,]
}
comptables<-list()
comptables[[1]]<-list(comptables1,comptables2)
comptables[[2]]<-list(comptables1,comptables2)
comptables[[3]]<-list(comptables1,comptables2)
comptables[[4]]<-list(comptables1,comptables2)
knitr::kable(comptables[[1]][1],
caption="Table of Forecast Wealth Values: Suggested One Year",
format.args=list(big.mark=",",floating=FALSE)) %>%
kable_styling(c("bordered","condensed"),
latex_options = c("hold_position"),
font_size = 10,
full_width = F)
knitr::kable(comptables[[2]][1],
caption="Table of Forecast Wealth Values: Suggested Three Years",
format.args=list(big.mark=",",floating=FALSE))%>%
kable_styling(c("bordered","condensed"),
latex_options = c("hold_position"),
font_size = 10,
full_width = F)
knitr::kable(comptables[[3]][1],
caption="Table of Forecast Wealth Values: Suggested Five Years",
format.args=list(big.mark=",",floating=FALSE))%>%
kable_styling(c("bordered","condensed"),
latex_options = c("hold_position"),
font_size = 10,
full_width = F)
knitr::kable(comptables[[4]][1],
caption="Table of Forecast Wealth Values: Suggested Ten Years",
format.args=list(big.mark=",",floating=FALSE))%>%
kable_styling(c("bordered","condensed"),
latex_options = c("hold_position"),
font_size = 10,
full_width = F)
```
```{r CompChart, echo=FALSE, warning=FALSE, fig.height=4, fig.show='hold', results='asis'}
horizons <- c(1,3,5,10)
par(mfrow=c(1,2))
minv<-min(unlist(comptables[[1]][1]),unlist(comptables[[2]][1]),
unlist(comptables[[3]][1]),unlist(comptables[[4]][1]))
maxv<-max(unlist(comptables[[1]][1]),unlist(comptables[[2]][1]),
unlist(comptables[[3]][1]),unlist(comptables[[4]][1]))
for (h in 1:length(horizons)){
wealth.pl.comp<-matrix(unlist(comptables[[h]][2],use.names = FALSE),ncol=3,byrow=FALSE)
colnames(wealth.pl.comp)<-c("Cons.","Mod.","Aggr.")
barplot(wealth.pl.comp,col=c("white","red","yellow","green","blue"),border=NA,
main=paste("Forecast A/T Values -",horizons[h],"Years"),
ylim=c(minv/2,maxv+minv/2))
abline(h=250,col="darkgray")
}
par(mfrow=c(1,1))
```
\newpage
#Section 2