-1

I have a function in an R Markdown document. The function takes a "case number" variable, which is used to filter some data frames, produce a plot, and output the plot to a knit html document. Here's the basic idea:

---
title: "Plot"
output: 
  html_document:
    toc: true
    toc_float: true
    toc_depth: 2

---

```{r echo=FALSE, results='hide',message=FALSE,warning=FALSE}

library(dplyr)
library(ggplot2)
library(scales)
library(lubridate)
library(plotly)
library(vistime)



```

```{r echo = FALSE, warning=FALSE, fig.align='CENTER', fig.width=10, fig.height=5}




data <- read.csv(text="event,group,start,end,color
                       Phase 1,Project,2016-12-22,2016-12-23,#c8e6c9
                       Phase 2,Project,2016-12-23,2016-12-29,#a5d6a7
                       Phase 3,Project,2016-12-29,2017-01-06,#fb8c00
                       Phase 4,Project,2017-01-06,2017-02-02,#DD4B39
                       Room 334,Team 1,2016-12-22,2016-12-28,#DEEBF7
                       Room 335,Team 1,2016-12-28,2017-01-05,#C6DBEF
                       Room 335,Team 1,2017-01-05,2017-01-23,#9ECAE1
                       Group 1,Team 2,2016-12-22,2016-12-28,#E5F5E0
                       Group 2,Team 2,2016-12-28,2017-01-23,#C7E9C0
                       3-200,category 1,2016-12-25,2016-12-25,#1565c0
                       3-330,category 1,2016-12-25,2016-12-25,#1565c0
                       3-223,category 1,2016-12-28,2016-12-28,#1565c0
                       3-225,category 1,2016-12-28,2016-12-28,#1565c0
                       3-226,category 1,2016-12-28,2016-12-28,#1565c0
                       3-226,category 1,2017-01-19,2017-01-19,#1565c0
                       3-330,category 1,2017-01-19,2017-01-19,#1565c0
                       1-217.0,category 2,2016-12-27,2016-12-27,#90caf9
                       4-399.7,moon rising,2017-01-13,2017-01-13,#f44336
                       8-831.0,sundowner drink,2017-01-17,2017-01-17,#8d6e63
                       9-984.1,birthday party,2016-12-22,2016-12-22,#90a4ae
                       F01.9,Meetings,2016-12-26,2016-12-26,#e8a735
                       Z71,Meetings,2017-01-12,2017-01-12,#e8a735
                       B95.7,Meetings,2017-01-15,2017-01-15,#e8a735
                       T82.7,Meetings,2017-01-15,2017-01-15,#e8a735")

data <- as.data.frame(data)

#event_id <- "category 1"
build_plot <- function(event_id) {
   data.filtered <- data%>%
    filter(group==event_id)

   p <- vistime(data.filtered)
   pb <- plotly_build(p)
   pb #Display plot
   
   rmarkdown::render(input = "stack_overflow_example.Rmd",
                  output_format = "html_document",
                  output_file = 'stack_overflow_example.html',
                  output_dir = "//mydir/myfolder")
}

build_plot("category 1")                        
 
```

I'm getting this error, and no document is output:

Error in sink(con, split = debug) : sink stack is full
halfer
  • 19,824
  • 17
  • 99
  • 186
DiamondJoe12
  • 1,879
  • 7
  • 33
  • 81
  • my bad, edited now. – DiamondJoe12 Nov 20 '20 at 18:15
  • So, it's weird to me that you're in an RMarkdown document and you're using it to `render` a different RMarkdown document... what's the reason for doing this set-up in an RMarkdown document instead of a regular R script? – Gregor Thomas Nov 20 '20 at 19:10
  • i'm just trying to knit to html programmatically ... – DiamondJoe12 Nov 20 '20 at 19:12
  • my aim is to use the function to write the plot to an html document using knit to html. Maybe render isn't the right way? – DiamondJoe12 Nov 20 '20 at 19:13
  • Does this make sense? I followed this documentation, and that's what it said to do (using render does the same thing that "knit to html" does): https://stackoverflow.com/questions/27246746/how-to-replicate-knit-html-in-a-command-line – DiamondJoe12 Nov 20 '20 at 19:16
  • You’d want to run the `rmarkdown::render()` at the terminal with R as opposed to inside the rmarkdown chunk. Like mentioned above, your `build_plot()` with the event_id as an index should build your plots and plotted if done correctly when knitted – Daniel_j_iii Nov 20 '20 at 20:30

1 Answers1

2

Running render on a document does the same thing as knit to html. That includes running all the R code in the document.

But you shouldn't put render in the document itself - you've created a recursive loop because you call render on the document, which runs the code on the document, which calls render on the document, which runs the code in the document....

Remove the render call from your document, instead put it in a separate R script. Then, running that script will render the document.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thank you. The reason I placed render in the function was to render the hmtl page for each plot produced. If I take render out of the function, won't I need to somehow put render in a loop, in order to render each plot? – DiamondJoe12 Nov 20 '20 at 19:29
  • i.e., I want to to pass an array of values into build_plot, and for each plot, output to html. – DiamondJoe12 Nov 20 '20 at 19:39
  • 2
    You don't render a page or a plot - you render a whole document. If you want a bunch of HTML files, each with one plot, then yes you would call `render` in a loop. Parameterized reports are the way to go in this case, you can pass the `ID` in as an argument. – Gregor Thomas Nov 20 '20 at 19:40
  • 2
    Alternately, if you want one big HTML document with lots of plots, then we call `build_plot` in a loop inside the document, and call `render` once from outside the document. – Gregor Thomas Nov 20 '20 at 19:40
  • 1
    I don't have time to add more details now, but I'll try to add some tonight if you're still confused. But have a look at [this question and answer](https://stackoverflow.com/q/54969070/903061) - the asker has a nice simple reproducible example and just needed a little help with the loop aspect. – Gregor Thomas Nov 20 '20 at 19:42
  • Gotcha, this is more complex than I initially thought but I'm reading into parameterized reports and it's making sense. I'm going to work on this and will let you know if I get it to work. Thanks. – DiamondJoe12 Nov 20 '20 at 20:11
  • You could also create all these plots with your R script separately and render them to .png and then import them all in your Rmarkdown report as a graphics. – Daniel_j_iii Nov 20 '20 at 20:34
  • @DanielJachetta OP is using plotly, so presumably OP wants the interactive capabilities. – Gregor Thomas Nov 20 '20 at 20:54
  • 2
    Gregor - correct. Got it to work! I just created a new parameter in the r markdown, and included it as a variable in the r markdown. In a separate script, I looped through the data frame and called the render method on each unique value. Thanks again! – DiamondJoe12 Nov 20 '20 at 21:00