我试图从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
我以这种方式呈现文档:
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))
时它仍会显示情节。
答案 0 :(得分:0)
我找到了。虽然结果=&#39;隐藏&#39;什么都不做,包括= 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
```