RStudio Viewer Pane not working?

2019-02-19 23:41发布

问题:

I have been trying to learn R to work on some network analysis. I found the networkD3 package and ran their example code (below) to get acquainted. It would switch to the "Viewer" tab on the right side of the console, but it would appear blank. If I used the "Export -> Save As Web Page..." then I could open that saved html document in my browser and see what I expected to see.

I've tried a couple other things I think would open in that Viewer pane but it launches a tab in my browser. I've even tried the rstudio::viewer("document.html") approach and it still goes to my browser. Any ideas?

# Create fake data
src <- c("A", "A", "A", "A", "B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J", "E", "F", "G", "H", "I")
networkData <- data.frame(src, target)

# Plot
simpleNetwork(networkData)

回答1:

It appears that in order for the internal viewer to work, your source documents must actually reside in "the session temporary directory" - as stated in the support document. Thus - assuming you have a file test.html in your home directory - the following will open the file in your default browser ...

myViewer <- getOption("viewer")
myViewer("~/test.html")

... but to open it in the internal viewer pane you need this:

file.copy("~/test.html", file.path(tempdir(), "test.html"))
myViewer(file.path(tempdir(), "test.html"))

This also works with .jpg but not with .pdf (.pdf's open in your default pdf viewer.) Incidentally, file.show() has related functionality: it will open .html and .jpg files in the edit pane - but not .pdf either.



标签: r rstudio