[英]Suppress function output with markdown
I'm trying to create a markdown document from R. Below is my code. 我正在尝试从R创建降价文档。下面是我的代码。 It works perfect, however I source a function (tryFun() from tryFun.R).
它工作完美,但是我从tryFun.R中获取了一个函数(tryFun())。 The output of this function I store in a variable.
我将此函数的输出存储在变量中。 When I do this, it automatically also prints the output.
当我这样做时,它也会自动打印输出。 However, I want to determine myself where I use the output of that variable, so later on, I call it.
但是,我想确定自己在哪里使用该变量的输出,因此稍后我将其称为。 How can I suppress the output of the function when I assign the results to a variable?
将结果分配给变量时,如何抑制函数的输出?
Code in tryRmd.R : tryRmd.R中的代码:
#' ---
#' title: "Try for the first time"
#' output: word_document
#' params:
#' xvar: ""
#' ---
#' ## Introduction
#' Hi, this is me trying something for the first time
#' #Hello again
```{r, echo=FALSE}
plot(1:100)
#x <- 3
source("tryFun.r")
cat("hi")
xvar <- params$xvar
x1 <- invisible(tryFun(xvar))
### shows a plot here (want to suppress this)
cat(x1$res)
plot.new()
x1$p
### shows the same plot here
```
and the code in tryFun.R is: tryFun.R中的代码是:
tryFun <- function(x){
res = 3*x
plot(1:1000)
p <- recordPlot()
return(list(res=res,p=p))
}
and my output is of the form: 和我的输出是以下形式:
Title 标题
Text 文本
Plot (1:100) 情节(1:100)
Plot (1:1000) from calling x1 <- invisible(tryFun(xvar))
(This is what I don't want to show) 通过调用
x1 <- invisible(tryFun(xvar))
绘制(1:1000)(这是我不想显示的)
9 (from calling cat(x1$res))
9(从呼叫
cat(x1$res))
Plot (1:1000) from calling x1$p
通过调用
x1$p
绘制图(1:1000)
I render the document in this way: 我以这种方式渲染文档:
library(rmarkdown)
dire = "D:/"
filename = paste(dire, "tryRmd.r", sep="/")
rmarkdown::render(filename, params=list(xvar=3))
Update 更新资料
I changed the code to this: 我将代码更改为此:
#' ---
#' title: "Try for the first time"
#' output: word_document
#' params:
#' xvar: ""
#' ---
#' ## Introduction
#' Hi, this is me trying something for the first time
#' #Hello again
```{r, echo=FALSE, results='hide'}
plot(1:100)
direct = "G:/Documents/WarehouseMovements"
filename <- paste(direct, "pickingsregels_wmp700_monthly_201711.txt", sep="/")
#x <- 3
source("tryFun.r")
xvar <- params$xvar
x1 <- invisible(tryFun(xvar))
### shows a plot here (wants to suppress this)
```
```{r, echo=FALSE}
cat("hi")
cat(x1$res)
plot.new()
x1$p
### shows the same plot here
```
But it still shows the plot when calling x1 <- invisible(tryFun(xvar))
. 但是当调用
x1 <- invisible(tryFun(xvar))
时,它仍然显示该图。
I found it. 我找到了。 While results='hide' did do nothing, include=FALSE did.
虽然result ='hide'没有执行任何操作,但是include = FALSE却没有执行。
So my code looks like this: 所以我的代码看起来像这样:
#' ---
#' title: "Try for the first time"
#' output: word_document
#' params:
#' xvar: ""
#' ---
#' ## Introduction
#' Hi, this is me trying something for the first time
#' #Hello again
```{r, echo=FALSE,include=FALSE}
plot(1:100)
direct = "G:/Documents/WarehouseMovements"
filename <- paste(direct, "pickingsregels_wmp700_monthly_201711.txt", sep="/")
#x <- 3
source("tryFun.r")
xvar <- params$xvar
x1 <- invisible(tryFun(xvar))
### shows a plot here (wants to suppress this)
```
```{r, echo=FALSE}
cat("hi")
cat(x1$res)
plot.new()
x1$p
### shows the same plot here
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.