数据可以与Rmd / RStudio中的cat命令组合打印 [英] Datatable is not printed in combination with cat command in Rmd / RStudio

查看:848
本文介绍了数据可以与Rmd / RStudio中的cat命令组合打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么在Rmd代码(使用RStudio生成HTML报告)下面仅显示 cat 命令?当我在之外移动 cat 命令时,如果子句或者注释它,则打印表。我相信使用库(printr)时会发生同样的情况,但是我没有通过一个最小化的样本来确认。



似乎如果子句中的代码以某种方式解释在一起,并且 cat 不会好的,与 datatable



如果你能给我一些关于如何调试的线索,这将是有帮助的也是。因为没有任何警告/错误消息。

  --- 
标题:test
输出:
html_document
---


```{r}
if(TRUE){
DT :: datatable虹膜)
cat(我和我的猫在一起)
}

```


解决方案

这与 knitr#1137 HTML小部件(包括DT / DataTables)仅在从顶级 R表达式生成。这是因为只有顶级表达式实际上是打印。不在最高级别的表达式仅评估。两种情况有很大差异。打印涉及调用打印功能。在大多数情况下,对于S4对象,这个功能并不奇怪,对于S4对象, print()(或 show())。在 knitr 世界中,它比这更复杂:默认打印功能是 knitr :: knit_print ,这与 print(),但它除了生成正在打印的对象的元数据(例如HTML依赖)的文本输出之外还有一件事情。编织完成后, rmarkdown 将元数据解析为适当的HTML代码(例如< script src =dataTables.js>< / script> in < head> )。



总而言之,如果表达式不在顶级,既不打印也不是元数据收集,所以没有办法实现微件的渲染。



一个简单的例子来说明这一点:

  if(TRUE){

1:10#未打印

11:20 #打印,因为这是由if()



返回的可见值

Could someone please explain why in the Rmd code (to generate a HTML report using RStudio) below only the cat command is being displayed? When I move the cat command outside the if clause or comment it out the table is printed. I believe the same thing happens when using library(printr), but I haven't confirmed this with a minimal sample.

It seems that the code inside the if clause is somehow interpreted together and that the cat doesn't go well with the datatable.

If you could give me some clues on how to debug this, it would be helpful, too. As there are no warnings/error messages anywere.

---
title: "test"
output: 
  html_document
---


```{r}
if(TRUE){
  DT::datatable(iris)
  cat("I am here with my cat")
}

```

解决方案

This is essentially the same issue as knitr#1137. HTML widgets, including DT/DataTables, only work when they are generated from top-level R expressions. This is because only top-level expressions are actually printed. Expressions that are not at the top level are only evaluated. There is a big difference between the two cases. Printing involves calling a printing function. In most cases, this function is, not surprisingly, print() (or show() for S4 objects). In the knitr world, it is more complicated than that: the default printing function is knitr::knit_print, which is very similar to print(), but it does one more thing besides generating the text output, which is collecting the metadata (e.g. HTML dependencies) of the objects being printed. After knitting is done, rmarkdown will resolve the meta data into appropriate HTML code (e.g. <script src="dataTables.js"></script> in <head>).

To sum up, if the expression is not at the top level, neither printing nor metadata collection is done, so there is no way for the widget to be actually rendered.

A simple example to illustrate this:

if (TRUE) {

  1:10  # not printed

  11:20 # printed because this is the visible value returned by if()

}

这篇关于数据可以与Rmd / RStudio中的cat命令组合打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆