简体   繁体   English

在RStudio查看器中查看markdown生成的html

[英]View markdown generated html in RStudio viewer

I would like to see html file that is generated using markdown in RStudio viewer , but rstudio::viewer('test.html') opens my file in browser outside RStudio. 我想查看在RStudio查看器中使用markdown生成的html文件,但是rstudio::viewer('test.html')在RStudio外部的浏览器中打开我的文件。 Could you tell me how can I achieve this? 您能告诉我如何实现吗?

THIS example works but I don't know why my example doesn't work in that way. 示例有效,但我不知道为什么我的示例不能以这种方式工作。

test.html file it'a an complied example that we get when we choose new file -> R Markdown. test.html文件,这是我们选择新文件-> R Markdown时得到的完整示例。

EDIT (according to Roman Luštrik comment) 编辑 (根据RomanLuštrik的评论)

library(knitr)
library(markdown)
f <- system.file("examples", "knitr-minimal.Rmd", package = "knitr")
knit(f)
markdownToHTML('knitr-minimal.md',output='knitr-minimal.html')
rstudio::viewer('knitr-minimal.html')

The key is using tempfile() , as explained here . 关键是使用tempfile()如解释在这里 Whenever the html file is outside the session temp dir, Rstudio won't display it. 只要html文件位于会话temp dir之外,Rstudio就不会显示它。

This, on the other hand, will work: 另一方面,这将起作用:

temp.f <- tempfile()
cat("Hello", file = temp.f)
rstudio::viewer(temp.f)

Edit 编辑

As was pointed out by @Sebastian Palma in his comment, the "rstudio" package has been replaced by "rstudioapi", so that the third line should now be: 正如@Sebastian Palma在其评论中指出的那样,“ rstudio”软件包已被“ rstudioapi”替换,因此第三行现在应为:

rstudioapi::viewer(temp.f)

On my version of RStudio (0.98.994), clicking on the small down arrow on the right side of the "knit HTML"button gives me the options "View in Pane" and "View in Window". 在我的RStudio(0.98.994)版本中,单击“编织HTML”按钮右侧的小向下箭头会为我提供“在窗格中查看”和“在窗口中查看”选项。 Selecting the first instead of the second fixed it for me. 选择第一个而不是第二个为我修复它。

A nice comprehensive solution can be found by modifying the response given here: Is it possible to view an HTML table in the viewer pane? 通过修改此处给出的响应,可以找到一个很好的综合解决方案: 是否可以在查看器窗格中查看HTML表? Or, for convenience, I have copied the full code at the end 或者,为了方便起见,我在最后复制了完整的代码

Specifically, modify the definition of print.htmlTable with the following three simple steps: 具体来说,请通过以下三个简单步骤修改print.htmlTable的定义:

(1) Add a flag to the function declaration as follows: (1)向功能声明添加标志,如下所示:

print.htmlTable<- function(x, useViewer = TRUE, as.file.path = FALSE, ...)

(2) In the function definition, add the following line: (2)在函数定义中,添加以下行:

if(as.file.path){ x <- read_file(x)}

(3) Create a wrapper function to view file: (3)创建一个包装函数以查看文件:

view.htmlFile <- function(x, ...){
     print.htmlTable(x, useViewer = TRUE, as.file.path = TRUE, ...) 
     }

(4) Now, you can use the wrapper to view HTML files using the file path (4)现在,您可以使用包装器通过文件路径查看HTML文件
(and, the original function to view unsaved html outputs): (并且,原始功能可查看未保存的html输出):

 view.htmlFile(filepath.to.html) #i.e. 'knitr-minimal.html' or any other html file

Reminder: this is tweak / modification of the original function written by Max Gordon in an earlier post . 提醒:这是Max Gordon较早的文章中编写的原始功能的调整/修改。 Credit given accordingly. 相应地给予信用。

print.htmlTable<- function(x, useViewer = TRUE, as.file.path = FALSE, ...){

  if(as.file.path){ x <- read_file(x)}

  # Don't use viewer if knitr package is loaded (assumes if you loaded knitr, you are using knitr and dont want to use Viewer)
if (useViewer && !"package:knitr" %in% search()){

    htmlFile <- tempfile(fileext=".html")
    htmlPage <- paste("<html>", 
                      "<head>",
                      "<meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">", 
                      "</head>",
                      "<body>", 
                      "<div style=\"margin: 0 auto; display: table; margin-top: 1em;\">",
                      x,
                      "</div>",
                      "</body>",
                      "</html>", sep="\n")
    cat(htmlPage, file=htmlFile)

    viewer <- getOption("viewer")
    if (!is.null(viewer) && is.function(viewer)){
          # (code to write some content to the file)
          viewer(htmlFile)
        }else{
          utils::browseURL(htmlFile)
        }
      }else{
        cat(x)
      }
    }

#Wrapper to allow viewing of files using path
view.htmlFile <- function(x, ...){
    print.htmlTable(x, useViewer = TRUE, as.file.path = TRUE, ...) 
}

view.htmlFile(filepath.to.html) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM