0

I have a function in a 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:

#Outside of function, I have my unfiltered dataset:
as.data.frame(my.dataset)

build_plots <- function(unique_case_id) {

    my.dataset.filtered <- my.dataset %>%
        filter(case_id==unique_case_id)

    #draw plot
    data <- as.data.frame(my.dataset.filtered)
    p <- vistime(data)
    pb <- plotly_build(p)

    pb #display plot 

    # Output plot to html doc
    rmarkdown::render(input = "myFile.Rmd",
                  output_format = "html_document",
                  output_file = 'myFile.html',
                  output_dir = "//myDir/myFolder")
}

build_plots("1234")

This seems to just process the file on an infinite loop:

processing file: myFile.Rmd


processing file:myFile.Rmd

And produces a blank output in the html doc, with no plot.

halfer
  • 19,824
  • 17
  • 99
  • 186
DiamondJoe12
  • 1,879
  • 7
  • 33
  • 81
  • What is `myFile.Rmd`? That's the file you're rendering... doesn't seem like you do anything in your function to save your plot `pb` to `myFile.Rmd`, nor do you pass it as a parameter, or do anything to make it available outside the scope of this function in this R session. How is the result supposed to know about the plot you've created inside a function inside a different file? – Gregor Thomas Nov 20 '20 at 17:44
  • the plot is the output of the function, i.e the function parses some data and then produces the plot. How might I save the plot pb to myFile.Rmd? – DiamondJoe12 Nov 20 '20 at 17:48
  • Also, I'm confused by *"seems to just process the file on an infinite loop"* and *"produces a blank output in the html doc"*. Usually things either (a) run forever and produce no output or (b) stop, and do produce output (even if blank). Yours does both? And processes multiple times? Are you calling `build_plots` in a loop? Does the output file update multiple times in the process (look at "last modified" time in your OS)? – Gregor Thomas Nov 20 '20 at 17:48
  • A function's output is indicated by `return()` or, if there is no `return()` call, the last line of the function. Once a function outputs it is done and it stops. Your `pb()` is an intermediate object in the function, it doesn't exist outside of the function because you don't `return()` it and it's not the last line of the function. – Gregor Thomas Nov 20 '20 at 17:52
  • One simple way to do this would be to use `saveRDS` in your function to write `pb` out to a file. Then have `myFile.Rmd` read in the RDS file and display the plot in an R code chunk. – Gregor Thomas Nov 20 '20 at 17:55
  • Or you might want to look into [parametrized rmarkdown reports](https://rmarkdown.rstudio.com/developer_parameterized_reports.html%23parameter_types%2F). – Gregor Thomas Nov 20 '20 at 17:56
  • Thank you. I re-posted with a replicable example here: https://stackoverflow.com/questions/64934657/function-to-create-plot-and-knit-to-html-in-rmarkdown-not-working – DiamondJoe12 Nov 20 '20 at 18:08

0 Answers0