简体   繁体   English

在rmarkdown和knitr中打印自定义对象(PDF和HTML)

[英]Print custom object in rmarkdown and knitr (PDF and HTML)

Let's say I have a package with a function that returns an S3 object: 假设我有一个带有返回S3对象的函数的包:

new_myclass <- function() {
  return(structure(list(a=1, b=2), class = "myclass"))
}

I also have two functions that take a myclass object and return respectively a HTML representation and a LaTeX representation of the object. 我也有两个函数,它们带有一个myclass对象,并分别返回该对象的HTML表示形式和LaTeX表示形式。

myclass2html <- function(obj) { return("<p>MyClass object</p>")}
myclass2latex <- function(obj) { return("\begin{em}MyClass\end{em} object $x$")}

What functions/methods should I define to provide consistent and transparent knitr and rmarkdown support? 我应该定义哪些函数/方法来提供一致和透明的knitrrmarkdown支持? I would like to support both .Rmd files and .R files with a header like: 我想同时支持.Rmd文件和.R文件,其标题如下:

#'---
#' title: My document
#' output: pdf_output
#'---

So far my approach goes through the knit_print method: 到目前为止,我的方法是通过knit_print方法进行的:

knit_print.myclass <- function(x, ...) {
  rmarkdown_fmt <- rmarkdown::metadata$output
  knitr_fmt <- knitr::opts_knit$get("out.format")
  # should I use these heuristics with both variables?
  if (rmarkdownfmt == "pdf_document") {
    return(knitr::asis_output(myclass2latex(x)))
  }
  if (knitr_fmt %in% c("html", "markdown")) {
    return(knitr::asis_output(myclass2html(x)))
  } else {
    stop("Format not supported!")
  }
}

My main issue is that there may be two variables rmarkdown::metadata$output and knitr::opts_knit$get("out.format") that may or may not be defined (depending on whether or not rmarkdown is being used). 我的主要问题是可能存在或可能rmarkdown::metadata$output两个变量rmarkdown::metadata$outputknitr::opts_knit$get("out.format") (取决于是否使用rmarkdown )。 I find this confusing. 我觉得这很混乱。

  • Is knit_print the right method to customize for this purpose? knit_print是否是为此目的定制的正确方法?
  • What is the right way to know the output format? 什么是知道输出格式的正确方法? Is there a get_output_format function that tells me the output format? 是否有一个get_output_format函数可以告诉我输出格式?

To put the question in context, I am working on the condformat package that allows to visualize DataFrames with conditional formatting rules. 为了解决这个问题,我正在研究condformat包,该包允许使用条件格式设置规则可视化DataFrame。

EDIT: So far I have been doing my own voodoo to detect the output format: 编辑:到目前为止,我一直在做我自己的伏都教以检测输出格式:

#' @importFrom rmarkdown metadata
#' @importFrom knitr opts_knit
guess_output_format <- function() {
  rmd_output <- tryCatch({rmarkdown::metadata$output},
                         error = function(e) {NULL})
  if (is.null(rmd_output)) {
    rmd_output = ""
  }
  if (is.list(rmd_output)) {
    rmd_output <- names(rmd_output)[1]
  }
  if (rmd_output == "pdf_document") {
    return("latex")
  } else if (rmd_output %in% c("html_document", "html_vignette")) {
    return("html")
  } else if (rmd_output != "") {
    stop("Unsupported rmarkdown output format:", rmd_output)
  }
  # No rmarkdown, let's try with knitr:
  format <- knitr::opts_knit$get("out.format")
  if (format %in% c("html", "markdown")) {
    return("html")
  } else if (format %in% c("latex")) {
    return("latex")
  } else {
    stop("Format not supported!")
  }
}

EDIT : Further discussion here: https://github.com/rstudio/rmarkdown/issues/649 编辑 :这里有进一步的讨论: https : //github.com/rstudio/rmarkdown/issues/649

With knitr 1.18 this is much simpler: 使用knitr 1.18,这要简单得多:

guess_output_format <- function() {
  if (knitr::is_html_output()) {
    return("html")
  } else if (knitr::is_latex_output()) {
    return("latex")
  } else {
    return("unsupported")
 }
}

Related issue: https://github.com/rstudio/rmarkdown/issues/649 相关问题: https//github.com/rstudio/rmarkdown/issues/649

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM