RStudio Knit - explicit R command for Preview option after Rmd is knit

rmarkdown
knit

#1

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.

Thanks for any clarification here.
AW


#2

I too am not aware of documentation on this process more complete than what you referenced by Garrett (R Markdown integration in the RStudio IDE).

If you're inclined, the code to the RStudio IDE is on github here "markdown+render".


#3

To clarify, you are interested in the Viewer pane, not the sequency of events that begets the output file from RMarkdown?

If no, then I highly recommend Yihui's knitr book (and RMarkdown book, at that). I think you've already seen the "How it works" section of the R Markdown documentation from Garrett (unless I'm wrong about what you're referencing above)

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.


#4

If you’re going spelunking in the RStudio source code, this overview of how the parts fit together might be helpful:

The Viewer pane info @mara linked to is also on the nascent RStudio Extensions site, along with RStudio API documentation (such as it is thus far):


#5

Aloha jcblum:
you made me review (again) the RStudio Extensions page for RStudio Viewer
URL: https://rstudio.github.io/rstudio-extensions/rstudio_viewer.html

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


#6

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


#7

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

EconomiCurtis pointed me to the github code for rstudio, and I reviewed the java code there:

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

rstudio/studio/client/workbench/views/output/renderrmd/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.

AW


#8

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?

Otherwise, you can always make a feature request for what you have in mind at the rstudioapi repo? If you choose to do that, it would probably be a good idea to start with the RStudio IDE Team's feature request guide: https://github.com/rstudio/rstudio/wiki/Writing-Good-Feature-Requests


#9

Aloha jcblum

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)
}

Aloha, AW