2017-02-14 41 views
1

我遇到類似於'R, knitr doesn't respect order of chunks and text'中提出的問題的問題。在我的代碼中,我有一個文本標題,後面跟着一個使用四個knitr :: kable函數調用來顯示四個表的塊。奇怪的是,表1和表2出現在標題上方,表3和表4出現在PDF中。看起來這與針織機允許物體漂浮有關。我可以最好在knitr :: kable函數或塊選項中關閉它嗎?這裏是我的代碼:如何控制rmarkdown/knitr文件的輸出順序(或如何防止浮動)

--- 
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 

我已經嘗試與浮動= FALSE在kable功能和塊選項選項fig.show和結果參數(包括結果=「持有」)。第1部分之前的新頁面也出現故障。注意我也檢查了this seemingly similar question

這是輸出圖像。顯示的單個塊內的文本不符合規則,因爲其中兩個表位於文本標題「Section 1」的上方。表格之間的圖表來自後續的塊。

Image of Output

+0

我通常包括'\ usepackage {placeins}'在我的頭,然後把'\ FloatBarrier'節標題之前,以防止漂浮的環境跨越該部分之外我希望他們進來。 – Benjamin

+0

我加'頭-includes: - \ usepackage {placeins}'並修改了塊的標題,使它們看起來像這樣:{r,echo = FALSE,warning = FALSE,results ='asis',out.extra ='FloatBarrier'}'根據@Benjamin的評論。我仍然有同樣的問題。我應該做什麼不同? – rmacey

+0

如果您提供可重複的示例,則可以更輕鬆地提出具體建議。 – Benjamin

回答

1

基礎上documentationout.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 
+0

感謝您的幫助。我對你的回覆速度感到敬畏。你的建議的確會產生不同的結果。我希望有4張桌子,然後是2張2格的4張圖表。現在我得到一排1x2的圖表,4張表格和接下來的2張圖表。添加另一個\ FloatBarrier似乎工作。 – rmacey

相關問題