如何以独立于操作系统的方式从R打开本地html文件?
出于演示目的,假设该文件名为test.html
并且位于工作目录中。
system('gnome-open test.html')
browseURL(paste('file://', getwd(),'test.html', sep='/'))
答案 0 :(得分:5)
您可能会发现我的open.file.in.OS
功能很有用,可以找到来源here。
关于此功能的简短摘要:
shell.exec
open
在Mac上使用system
xdg-open
system
shQuote
醇>
更新:现在查看openFileInOS
包中的pander
。
引用:此函数是David Hajage的分叉版本,convert
函数可以找到here。
答案 1 :(得分:1)
我只是想将@daroczig提供的回答从评论中拉出来并回答。如果@darcozig想将此作为单独的答案发布,我将删除此副本。
openHTML <- function(x) browseURL(paste0('file://', file.path(getwd(), x)))
答案 2 :(得分:0)
使用file.path
功能构建文件路径。
file.path(..., fsep = .Platform$file.sep)
...: character vectors.
fsep: the path separator to use.
默认情况下,它将使用当前的os路径分隔符。
例如
> file.path ("", "home", "phoxis", "paragraph")
[1] "/home/phoxis/paragraph"
这会生成我的文件“/ home / phoxis / paragraph”
注意开头的空白字符串“”。这迫使在我的情况下添加额外的“/”来生成绝对路径。根据需要调整以生成绝对路径或相对路径,并查看?file.path
我认为这将满足您的需求