rma*_*cey 5 r knitr r-markdown
我遇到的问题类似于“ R,knitr 不尊重块和文本的顺序”中提出的问题。在我的代码中,我有一个文本标题,后跟一个块,该块使用四个 knit::kable 函数调用来显示四个表。奇怪的是,在 PDF 输出中,表 1 和表 2 出现在标题上方,表 3 和表 4 出现在下方。看来这与针织物允许物体漂浮有关。我可以最好在 knitr::kable 函数或块选项中关闭它吗?这是我的代码:
---
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
Run Code Online (Sandbox Code Playgroud)
我已经尝试了 kable 函数中的 float = FALSE 选项以及块选项中的Fig.show 和 results 参数(包括 results='hold')。第 1 部分之前的新页面也出现故障。请注意,我也确实检查了这个看似相似的问题。
这是输出的图像。显示的单个块中的文本是无序的,因为其中两个表位于文本标题“第 1 节”上方。表之间的图表来自后续块。
根据文档,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
Run Code Online (Sandbox Code Playgroud)