【问题标题】:How to suppress pdfcrop printing when using R rmarkdown render function?使用 R rmarkdown 渲染功能时如何抑制 pdfcrop 打印?
【发布时间】:2015-05-18 13:59:42
【问题描述】:

我在调用 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

这里是渲染函数的调用:

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

【问题讨论】:

    标签: r pdf render crop r-markdown


    【解决方案1】:

    我找到了一种使用 suppressMessages 函数抑制消息的解决方案,即使它不会抑制所有消息,只抑制那些发送到标准错误的消息。

    这是我的修改:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 嗨,Wastl,感谢您的回答和有关 knitr 的报告。我找到了一个使用sink 的替代解决方案,方法是用 pre sink("/dev/null") 包围渲染调用以抑制输出和 post sink() 将输出重置为正常。我不会从系统调用中调用渲染函数。但我同意一个更方便的解决方案是按照您的要求为 plot_crop 函数提供一个安静的选项。
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2013-06-21
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-07
        • 2021-11-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多