2017-02-14 12 views
1

'R, knitr doesn't respect order of chunks and text'に記載されている問題と同様の問題が発生しています。私のコードでは、4つのテーブルを表示するために4つのknitr :: kable関数呼び出しを使用するチャンクが続くテキスト見出しがあります。不思議なことに、表1と表2は見出しの上に、表3と表4はPDFへの出力に現れます。これは、オブジェクトが浮動することを可能にするknitrと関係しているようです。できれば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 

私はチャンクオプションでKABLE機能で浮動= FALSEオプションと(結果=「保留」を含む)fig.showと結果パラメータを試してきました。セクション1の前の新しいページも順不同です。私はthis seemingly similar questionもチェックアウトしました。

ここに出力のイメージがあります。 1つのチャンク内のテキストは、2つのテーブルがテキストヘッダー「セクション1」の上にあるため、順序が正しくありません。テーブル間のチャートは、後続のチャンクからのものです。 documentationに基づいて

Image of Output

+0

私は通常、 '\のUSEPACKAGE {placeins}'私のヘッダーで、その後のセクションは、私は彼らで欲しい部分の外交配から浮い環境を防ぐために向かう前に '\ FloatBarrier'を置いてあります。 – Benjamin

+0

私は'ヘッダを追加しました-sys '、out.extra =' FloatBarrier '} '\'のように見えるように、チャンクのヘッダを修正しました。 @Benjaminのコメントごとに。私はまだ同じ問題があります。どうすれば違うのですか? – rmacey

+0

再現可能な例を提供すると、特定の推奨を行う方が簡単になります。 – Benjamin

答えて

1

out.extra "フィギュアのための追加オプション" を提供

out.extraを使用する代わりに\FloatBarrierをチャンクの上に配置すると、私はあなたが望むと思っているもののようになります。

--- 
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つのテーブルの後に2x2のグリッドで4つのチャートが続くことを望んでいました。今度は、1x2行のグラフ、4つのテーブル、次の2つのグラフを取得します。別の\ FloatBarrierを追加するとうまくいくようです。 – rmacey

関連する問題