问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/64934657/function-to-create-plot-and-knit-to-html-in-rmarkdown-not-working