简体   繁体   中英

Suppress function output with markdown

I'm trying to create a markdown document from R. Below is my code. It works perfect, however I source a function (tryFun() from tryFun.R). 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 :

#' ---
#' 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 <- 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)

Plot (1:1000) from calling x1 <- invisible(tryFun(xvar)) (This is what I don't want to show)

9 (from calling cat(x1$res))

Plot (1:1000) from calling x1$p

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)) .

I found it. While results='hide' did do nothing, include=FALSE did.

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
```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM