如何在使用R rmarkdown渲染功能时抑制pdfcrop打印?

时间:2015-03-16 12:41:57

标签: r pdf render crop r-markdown

我试图在调用R rmarkdown渲染函数时禁止打印到pdfcrop生成的stdout和stderr,但不知道该怎么做。

我已经使用quiet选项来禁止打印pandoc命令行,但它不会禁止打印pdfcrop。

有人有一些提示要解决它吗?

这是我运行的脚本:

Rscript RNASeq_QC_run.R -v 1 --count ~/devel/R/projects/rnaseq_qc/data/DataTest_Count_expression_generic2.txt  --format generic --design ~/devel/R/projects/rnaseq_qc/data/DataTest_Design.txt --outdir test/out5 &>test/out5.log

这是对render函数的调用:

generic_report_path <- system.file("report", "QC_RNASeq_Count_generic.Rmd", package="qc4rnaseq")
generic_report_file <- paste(unlist(strsplit(basename(generic_report_path),".Rmd")), ".pdf", sep="")
render(input=generic_report_path, output_format="pdf_document", output_file=generic_report_file, output_dir=outdir_abs_path, intermediates_dir=outdir_abs_path, quiet=TRUE)

以下是test / out5.log的内容:stdout(常规)和stderr(粗体:**之间)输出

**cropping /private/tmp/qc4rnaseq_run/test/out5/QC_RNASeq_Count_generic_files/figure-latex/unnamed-chunk-2-1.pdf**
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
==> 1 page written on `/private/tmp/qc4rnaseq_run/test/out5/QC_RNASeq_Count_generic_files/figure-latex/unnamed-chunk-2-1.pdf'.
**cropping /private/tmp/qc4rnaseq_run/test/out5/QC_RNASeq_Count_generic_files/figure-latex/unnamed-chunk-3-1.pdf**
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
==> 1 page written on `/private/tmp/qc4rnaseq_run/test/out5/QC_RNASeq_Count_generic_files/figure-latex/unnamed-chunk-3-1.pdf'.
**cropping /private/tmp/qc4rnaseq_run/test/out5/QC_RNASeq_Count_generic_files/figure-latex/unnamed-chunk-4-1.pdf**
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
==> 1 page written on `/private/tmp/qc4rnaseq_run/test/out5/QC_RNASeq_Count_generic_files/figure-latex/unnamed-chunk-4-1.pdf'.
Execution time :  7.120109 seconds

3 个答案:

答案 0 :(得分:0)

我找到了一种使用suppressMessages函数来抑制消息的解决方案,即使它不会抑制所有消息,只会抑制发送到stderr的消息。

以下是我的修改:

suppressMessages(render(input=generic_report_path, output_format="pdf_document", output_file=generic_report_f
ile, output_dir=outdir_abs_path, intermediates_dir=outdir_abs_path, quiet=TRUE))

现在发送到stderr的所有消息都被抑制,但不会被发送到我脚本的stdout。

仍然是那些与pdfcrop相关的印刷:

PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
==> 1 page written on `/private/tmp/qc4rnaseq_run/test/out7/QC_RNASeq_Count_generic_files/figure-latex/unnamed-chunk-2-1.pdf'.
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
==> 1 page written on `/private/tmp/qc4rnaseq_run/test/out7/QC_RNASeq_Count_generic_files/figure-latex/unnamed-chunk-3-1.pdf'.
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
==> 1 page written on `/private/tmp/qc4rnaseq_run/test/out7/QC_RNASeq_Count_generic_files/figure-latex/unnamed-chunk-4-1.pdf'.

答案 1 :(得分:0)

render()中封装suppressMessages()只会抑制message("cropping ", x)函数引发的plot_crop()。 要隐藏pdfcrop本身的输出,还需要在内部系统调用中设置ignore.stdout = TRUE。我刚刚在GitHub上提交了此功能请求作为knitr issue #1031

答案 2 :(得分:0)

对我有用的(不是最漂亮的解决方案)是将函数分配给变量。它将不会打印任何内容:

q = knitr::plot_crop(pdf_file,quiet = TRUE)