通过R将浏览器用作ggplot2的查看器

通过R将浏览器用作ggplot2的查看器,r,ggplot2,R,Ggplot2,我想知道,当使用ggplot2在R中打印时,是否可以将打印发送到浏览器以供查看?对于具有与Firefox或Chrome等浏览器关联的.html文件的Windows用户,这里有一种方法: library(ggplot2) ggplotToBrowser <- function(p) { ggsave(filename = tf_img <- tempfile(fileext = ".svg"), plot = p) html <- sprintf('<html>

我想知道,当使用ggplot2在R中打印时,是否可以将打印发送到浏览器以供查看?

对于具有与Firefox或Chrome等浏览器关联的.html文件的Windows用户,这里有一种方法:

library(ggplot2)
ggplotToBrowser <- function(p) {
  ggsave(filename = tf_img <- tempfile(fileext = ".svg"), plot = p)
  html <- sprintf('<html><body><img src="%s"></body></html>', paste0("file:///", tf_img))
  cat(html, file = tf_html <- tempfile(fileext = ".html"))
  shell.exec(tf_html) # or system(sprintf("open %s", tf_html))
}
p <- ggplot(diamonds[1:100, ], aes(x = carat, y = price)) + geom_point(alpha = .1)
ggplotToBrowser(p)
库(ggplot2)

ggplotToBrowser对于那些拥有与Firefox或Chrome等浏览器关联的.html文件的Windows用户,这里有一种方法:

library(ggplot2)
ggplotToBrowser <- function(p) {
  ggsave(filename = tf_img <- tempfile(fileext = ".svg"), plot = p)
  html <- sprintf('<html><body><img src="%s"></body></html>', paste0("file:///", tf_img))
  cat(html, file = tf_html <- tempfile(fileext = ".html"))
  shell.exec(tf_html) # or system(sprintf("open %s", tf_html))
}
p <- ggplot(diamonds[1:100, ], aes(x = carat, y = price)) + geom_point(alpha = .1)
ggplotToBrowser(p)
库(ggplot2)

ggplotToBrowser您可以使用
ggsave
将绘图保存到文件中,在浏览器中打开该文件,并在更新绘图时刷新。您还可以使用knitr将带有ggplots的rmarkdown文档转换为HTML并在浏览器中查看。仅用浏览器替换R绘图窗口可能是不可能的。您可以使用
ggsave
将绘图保存到文件中,在浏览器中打开该文件,并在更新绘图时刷新。您还可以使用knitr将带有ggplots的rmarkdown文档转换为HTML并在浏览器中查看。简单地用你的浏览器替换R绘图窗口可能是不可能的。很好,我是mac用户,有没有办法修改它以适用于mac?@moku我肯定有,但因为我没有任何苹果产品,我不知道。让我们等待另一个答案。:)哈,很公平,我环顾四周,发现在mac上有一个类似的shell.exec命令用于R。。。找不到任何内容。
系统(sprintf(“打开%s”,tf_html))
应该可以it@lukeA为mac添加一个带有baptiste补丁的版本,我会给你答案!很好,我是一个mac用户。有没有办法修改它来为mac工作?@moku我肯定有,但因为我没有任何苹果产品,我不知道。让我们等待另一个答案。:)哈,很公平,我环顾四周,发现在mac上有一个类似的shell.exec命令用于R。。。找不到任何内容。
系统(sprintf(“打开%s”,tf_html))
应该可以it@lukeA为mac添加一个带有baptiste补丁的版本,我会给你答案!