gpt4 book ai didi

r - 如何控制 rmarkdown/knitr 文件的输出顺序(或如何防止 float )

转载 作者:行者123 更新时间:2023-12-04 20:11:42 26 4
gpt4 key购买 nike

我遇到了与“R, knitr doesn't respect order of chunks and text”中提出的问题类似的问题'。在我的代码中,我有一个文本标题,后跟一个块,它使用四个 knitr::kable 函数调用来显示四个表。奇怪的是,表 1 和表 2 出现在标题上方,而下面的表 3 和表 4 出现在 PDF 输出中。这似乎与 knitr 允许对象 float 有关。我可以最好在 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

我已经尝试了 kable 函数中的 float = FALSE 选项以及块选项中的 fig.show 和结果参数(包括 results='hold')。第 1 节之前的新页面也出现故障。注意我确实检查了 this seemingly similar question也。

这是输出的图像。显示的单个块中的文本是乱序的,因为其中两个表位于文本标题“第 1 节”上方。表之间的图表来自后续块。

Image of Output

最佳答案

基于 documentation , 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

关于r - 如何控制 rmarkdown/knitr 文件的输出顺序(或如何防止 float ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com