[英]Suppress function output with markdown
我正在尝试从R创建降价文档。下面是我的代码。 它工作完美,但是我从tryFun.R中获取了一个函数(tryFun())。 我将此函数的输出存储在变量中。 当我这样做时,它也会自动打印输出。 但是,我想确定自己在哪里使用该变量的输出,因此稍后我将其称为。 将结果分配给变量时,如何抑制函数的输出?
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
```
tryFun.R中的代码是:
tryFun <- function(x){
res = 3*x
plot(1:1000)
p <- recordPlot()
return(list(res=res,p=p))
}
和我的输出是以下形式:
标题
文本
情节(1:100)
通过调用x1 <- invisible(tryFun(xvar))
绘制(1:1000)(这是我不想显示的)
9(从呼叫cat(x1$res))
通过调用x1$p
绘制图(1:1000)
我以这种方式渲染文档:
library(rmarkdown)
dire = "D:/"
filename = paste(dire, "tryRmd.r", sep="/")
rmarkdown::render(filename, params=list(xvar=3))
更新资料
我将代码更改为此:
#' ---
#' 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
```
但是当调用x1 <- invisible(tryFun(xvar))
时,它仍然显示该图。
我找到了。 虽然result ='hide'没有执行任何操作,但是include = FALSE却没有执行。
所以我的代码看起来像这样:
#' ---
#' 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.