【问题标题】:View markdown generated html in RStudio viewer在 RStudio 查看器中查看 markdown 生成的 html
【发布时间】:2014-04-09 06:37:01
【问题描述】:

我想在 RStudio 查看器 中查看使用 markdown 生成的 html 文件,但 rstudio::viewer('test.html') 在 RStudio 外部的浏览器中打开我的文件。你能告诉我怎样才能做到这一点吗?

THIS 示例有效,但我不知道为什么我的示例不能以这种方式工作。

test.html file 这是我们选择新文件时得到的一个编译示例 -> R Markdown。

编辑(根据 Roman Luš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')

【问题讨论】:

  • 你能发布一个不适合你的、可重复的小例子吗?
  • 好的,这里的例子取自knitr和使用markdown包生成的html

标签: r r-markdown


【解决方案1】:

关键是使用tempfile(),正如here 解释的那样。只要 html 文件位于会话临时目录之外,Rstudio 就不会显示它。

另一方面,这将起作用:

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

编辑

正如@Sebastian Palma 在他的评论中指出的那样,“rstudio”包已被“rstudioapi”取代,因此第三行现在应该是:

rstudioapi::viewer(temp.f)

【讨论】:

  • 它可能与当前版本不同。请改用rstudioapi::viewer(...)
【解决方案2】:

在我的 RStudio 版本 (0.98.994) 上,单击“knit HTML”按钮右侧的小向下箭头,可以选择“在窗格中查看”和“在窗口中查看”选项。选择第一个而不是第二个为我修复了它。

【讨论】:

    【解决方案3】:

    通过修改此处给出的响应,可以找到一个很好的综合解决方案: Is it possible to view an HTML table in the viewer pane?或者,为了方便,我把完整的代码复制到了最后

    具体来说,修改print.htmlTable的定义,简单三步:

    (1)在函数声明中添加标志如下:

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

    (2) 在函数定义中,添加以下行:

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

    (3) 创建一个包装函数来查看文件:

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

    (4) 现在,您可以使用包装器通过文件路径查看 HTML 文件
    (以及查看未保存的 html 输出的原始功能):

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

    提醒:这是对Max Gordonearlier post 中编写的原始函数的调整/修改。相应地给予信用。

    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) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多