I have tried a number of things including print(leafletMap) and just calling the object itself. Is there a specific function that I am to wrap my object in in order to get it to render in the document?
2 Answers
if you use Rstudio create a RMarkdown File or RHMTL file then include into a chunk code like this (you will need package knitr to convert into hmtl)
```{r setup, include=TRUE}
library(leaflet)
m<-leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m
```
otherwise you can save like an html document with the following code like suggested in this post https://stackoverflow.com/a/31642030/7600734
library(htmlwidgets)
saveWidget(m, file="m.html")

- 1,130
- 1
- 7
- 11
I have found a solution to this problem here, thanks to Jonathan: How to use objects from global environment in Rstudio Markdown
The issue is that R Markdown requires the objects being rendered to be produced inside the code chunk, i.e. being produced every time you knit your document. The makers of R Markdown did this intentionally to maximize the reproducibility of reports (which I can appreciate!). A new, "invisible" environment is created and used every time you knit a new document, causing the process to not recognize objects in other environments.
Unfortunately, my report is a small portion of a much larger analysis, and the code would be too cumbersome to run completely from the Rmd file.
Solution: Knit and save your document from the command line.
rmarkdown::render("path/your_doc.Rmd")

- 473
- 4
- 15