根据R参数对LaTeX进行条件输出

根据R参数对LaTeX进行条件输出

我正在使用 knitr 编写一份报告,它会循环遍历多个相同调查的结果,并为每个调查生成相同的表格和图表。但是,有些调查没有某些表格的数据。在这些情况下,我想隐藏表格或图表以及相关标题。

我可以使用条件语句来评估表中是否有数据,从而在 R 代码中隐藏 xtable 输出。但是,我看不出如何以类似的方式隐藏标题。我可以设置标题作为 R 块的一部分,但如果有其他方法,我宁愿不这样做。

我已在下面的代码中复制了我想做的事情(更改 n>0 以获取表格输出)。提前致谢。

\documentclass{article}

\usepackage{caption,setspace}
\captionsetup{font={small,stretch=0.80}}

\begin{document}

<<echo=FALSE, results='hide', cache=FALSE>>=
library(xtable)
n <- 0
x <- rnorm(n)
y <- 2*x + rnorm(n)
out<-data.frame(x,y, stringsAsFactors = FALSE)
@

\captionsetup{labelformat=empty, skip=0pt}
\captionof{table}{Caption above the table}
<<results='asis', echo=FALSE>>=

if (nrow(out)!=0) {
  print(xtable(out, digits=c(2, 2,2)))
  }else {cat("No Table")}
@
\captionsetup{labelformat=empty, skip=0pt}
\captionof{table}{Caption below the table}
\end{document}  

相关内容