I would like to know the exact & complete sequence of "hidden" R commands which are executed when I click the "Knit" button in RStudio for a selected RMarkdown file.
Specifically: what R commands automatically launch a Preview display version of the just-rendered .html file.
While RStudio Support tries to be as transparent as possible, I cannot find any reference anywhere - beginning with Garrett Grolemund's excellent but dated (2014) Overview "R Markdown integration in the RStudio IDE", where he states "The “Knit HTML” button works in the same way. It renders your .Rmd file and launches a preview of your output document."
So: what is the R command or function argument employed to actually launch a preview of the output document ?
What's more puzzling is scouring the R package documentation and finding nothing that suggests how a separate popup window can be created to display a just-rendered-by-pandoc .html file. OR how a just-rendered .html file can be displayed optionally in Viewer pane of the RStudio IDE.
For the viewer pane, I recommend the answer from Ian, below:
The Viewer is invoked with:
# (code to write some content to the file)
viewer <- getOption("viewer")
viewer(htmlFile)
I don't think this generates the pop-out, though, to which you are referring. As @EconomiCurtis mentioned, above, the source code for that is available. The ViewerPreviewRmdEvent might be relevant.
which suggests the following code for viewing [a file] in the RStudio Viewer pane:
tempDir <- tempfile()
dir.create(tempDir)
htmlFile <- file.path(tempDir, "index.html")
# (code to write some content to the file)
viewer <- getOption("viewer")
viewer(htmlFile)
OK not the fastest thinker, it took me time to connect dots and realize: I have to make that htmlFile have real content - the contents of the actual .html file I just rendered from RMarkdown
The solution is as simple as using file.copy()
Step by Step:
I capture the name of the just-generated-from-Rmd .html file in x
then I use the generic RStudio Viewer code which creates a still-generic htmlFile in the tempDir
and THEN I copy the real .html file (x) to this abstract file named htmlFile
and FINALLY I view it - in the Viewer pane.
x <- "RMD-Demo-Viridis-002x.html"
tempDir <- tempfile()
dir.create(tempDir)
htmlFile <- file.path(tempDir, x)
file.copy(x, htmlFile)
# (code to write some content to the file)
viewer <- getOption("viewer")
viewer(htmlFile)
So it boils down to the obvious:
where the generic template code has a comment line:
# (code to write some content to the file)
I replaced it with my file.copy() command:
file.copy(x, htmlFile)
Voila: solution to viewing a (small) .html file generated from RMarkdown in the Viewer pane.
Next: how to view the .html file in a separate Rstudio popup window (my preference)
Thank you all for responding so quickly.
AW
Oops - sorry about the reply message format: I forgot the pound sign gets interpreted in the message display
So wherever you see the BOLD statement it really is the original pound-sign / hash-sign statement found in the original RStudio Viewer generic code. Apologies, AW
Aloha gang, and again thanks for all your comments
As I replied above to jcblum, I solved the means for viewing (previewing) an .html file generated by Knit in a Preview pane.
I was left trying to understand how to get a separate popup RStudio window to view the Knit file, since that is the 2nd option for viewing the Knit result available in the RStudio options under the gear icon next to the Knit icon
and after inspecting it I think my conclusion is: the popup RStudio window option executes JAVA code
as possibly suggested by this specific meaty code section named: RenderRmdOutputPresenter.java
Unless there is relevant rstudioapi R language commands I am missing that wrap this java code option, I think there is no (easy) means of calling the rendered .html file to view it in an RStudio popup window.
You can edit your post to reformat the code (strongly encouraged!) by clicking the tiny gray pencil icon at the bottom of the post. Single backticks work for inline code, if that helps in this case?
I'm curious what your use case is here — maybe if you explain a bit more, somebody might have an idea for a different solution?
I edited my post as you advised - much cleaner now.
As to USE CASE
It's rather simple:
As a newbie to RMarkdown and Knit, I like to see exactly what R commands are issued whenever they are "hidden" behind an icon such as "Knit"
More to the point, my use case is this:
I plan to create & test & Knit a series of .Rmd files
and then Knit them to generate (for now) .html files.
Having done so and then wanting to eat some lunch or attend a meeting or some such
I may want to review the html files at a later date - inside RStudio:
and I don't think it is useful to Knit them all over again just to view the already-generated .html files. So I wanted a separate function/procedure in R language with which to view - inside RStudio, NOT jumping out to a web browser - the displayed form of the .html file.
And now I have that, even wrapped into a standalone function, as crude as mine may be at the moment, and still being tested & developed:
viewerpane.html <- function(xfile, vsize=NULL){
# Function: viewerpane.html version 1.00 23July2018
# Purpose: view RMarkdown Knit-generated html file in RStudio Viewer pane
# Status: Dev/Test
# Args:
# xfile = quoted name of html file (and path if not located in current directory)
# vsize = viewer arg height, default=NULL; alt values: "maximize", or numeric {3 to 8}
# Example: x <- "RMD-Demo-Viridis-002x.html"
# References:
# 1. https://rstudio.github.io/rstudio-extensions/rstudio_viewer.html
# 2. https://rstudio.github.io/rstudio-extensions/pkgdown/rstudioapi/reference/viewer.html
# 3. https://rstudio.github.io/rstudio-extensions/rstudioapi.html
#
# library(rstudioapi)
xfile.b <- basename(xfile)
tempDir <- tempfile()
dir.create(tempDir)
htmlFile <- file.path(tempDir, xfile.b)
# (code to write some content to the file) -- see next line
file.copy(xfile, htmlFile)
viewer <- getOption("viewer")
viewer(htmlFile, height = vsize)
}