簡體   English   中英

降價抑制功能輸出

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM