如何在RMarkdown中显示代码但隐藏输出? [英] How to show code but hide output in RMarkdown?

查看:1778
本文介绍了如何在RMarkdown中显示代码但隐藏输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的html文件显示代码,但不显示该块的输出:

I want my html file to show the code, but not the output of this chunk:

```{r echo=True, include=FALSE}
fun <- function(b)
    {
    for(a in b)
        {print(a)
        return(a * a)}
    }
y <- fun(b)
```

运行代码时,我需要打印才能看到进度(实际上这是一个很长的功能).

When I run the code, i need the print to see the progress (it is quite a long function in reality).

但是在knitr文件中,我将输出用于另一个块中,所以我不想在此块中看到它(并且没有进度的概念,因为代码已经运行了.)

But in the knitr file, I use the output in a further chunk, so I do not want to see it in this one (and there's no notion of progress, since the code has already been run).

echo=True, include=FALSE在这里不起作用:整个东西都被隐藏了(这是include=FALSE的正常行为).

This echo=True, include=FALSE here does not work: the whole thing is hidden (which is the normal behavior of include=FALSE).

我可以使用哪些参数来隐藏打印内容,但显示我的代码?

推荐答案

@ J_F在注释中使用{r echo = T, results = 'hide'}进行了回答.

As @ J_F answered in the comments, using {r echo = T, results = 'hide'}.

我想扩展他们的答案-您可以使用大量资源来确定块和输出显示的所有可能选项-我将印刷版保存在办公桌上!

I wanted to expand on their answer - there are great resources you can access to determine all possible options for your chunk and output display - I keep a printed copy at my desk!

您可以在RStudio网站上的备忘单下找到它们(查找R Markdown)速查表 R Markdown参考指南),或者在RStudio中,导航到帮助"标签,选择速查表",然后在其中查找相同的文档.

You can find them either on the RStudio Website under Cheatsheets (look for the R Markdown cheatsheet and R Markdown Reference Guide) or, in RStudio, navigate to the "Help" tab, choose "Cheatsheets", and look for the same documents there.

最后设置默认块选项,如果您希望大多数块具有相同的行为,则可以在第一个块中运行类似以下代码的内容:

Finally to set default chunk options, you can run (in your first chunk) something like the following code if you want most chunks to have the same behavior:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T,
                      results = "hide")
```

稍后,您可以像这样修改单个块的行为,这将替换 just 结果选项的默认值.

Later, you can modify the behavior of individual chunks like this, which will replace the default value for just the results option.

```{r analysis, results="markup"}
# code here
```

这篇关于如何在RMarkdown中显示代码但隐藏输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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