【问题标题】:Force rstudio to use browser instead of viewer强制 rstudio 使用浏览器而不是查看器
【发布时间】:2016-01-23 14:42:32
【问题描述】:

考虑如果y = TRUE 和浏览器中y = FALSE 的任一功能(对于rstudio)将在查看器中打开某些内容。您可以通过options(viewer = NULL) 强制whatever 在浏览器中打开(然后您需要重置为之前),但我无法使用普通的on.exit 方法使其在函数内部工作。在 windows 和 osx 上测试。

f <- function(x, y = TRUE) {
  if (y) {
    oo <- getOption('viewer')
    on.exit(options(viewer = oo))
    options(viewer = NULL)
  } else options(viewer = NULL)
  print(getOption('viewer'))
  DT::datatable(x)
}

g <- function(x, y = TRUE) {
  if (y) {
    oo <- getOption('viewer')
    on.exit(options(viewer = oo))
    options(viewer = NULL)
  } else options(viewer = NULL)
  print(getOption('viewer'))
  htmlTable::htmlTable(x)
}

## in rstudio, returns the viewer function
getOption('viewer')
# function (url, height = NULL) 
# ...

## opens in viewer despite `options(viewer = NULL)`
g(mtcars)
# NULL

## again returns the function, ie, reset my options to before g call successfully
getOption('viewer')
# function (url, height = NULL) 
# ...

## opens in browser but leaves `options(viewer = NULL)` after exiting
g(mtcars, FALSE)
# NULL

getOption('viewer')
# NULL

似乎查看器不尊重我在函数环境中的选择,无论是仅使用一些 html (g) 还是小部件 (f)。我认为两者都会在函数内使用viewer = NULL,并以退出时的方式返回我的选项,以便我可以控制要查看结果的位置。

或者对于 html 和小部件有更好的方法吗?我试过DT::datatable 中的options 参数无济于事,但这对htmlTable::htmlTable 的情况没有帮助。

我能想到的唯一其他方法是将所有代码写入临时文件并使用if (rstudio) rstudio::viewer(tempfile) else browseURL(tempfile),我认为这对于看似如此简单的事情来说是很多工作。

【问题讨论】:

  • 如果有人可以查看我的编辑历史并告诉我差异如何知道我以两种不同的方式删除了相同的字符,则加分
  • 我向 Ushey 发送了“蝙蝠信号”。希望他在西雅图看云。

标签: r rstudio


【解决方案1】:

虽然这不是解决办法,但我认为它说明了正在发生的事情。尝试在on.exit() 处理程序中添加Sys.sleep() 调用:

f <- function(x) {
  viewer <- getOption("viewer")
  on.exit({
    print("Restoring viewer...")
    Sys.sleep(3)
    options(viewer = viewer)
  }, add = TRUE)
  options(viewer = NULL)
  DT::datatable(x)
}

## opens in viewer despite `options(viewer = NULL)`
f(mtcars)

您会注意到,直到on.exit() 处理程序完成执行之后,RStudio 才“决定”如何处理DT::datatable() 调用的结果。这意味着,当 RStudio 想要弄清楚如何处理结果时,查看器已经恢复了!奇怪的是,RStudio 会等到 R 不再“忙”时才决定如何显示结果内容,到那时临时更改 viewer 选项已经太晚了。

请注意,这并不能解释 htmlTable 的行为。我最好的猜测是发生了某种竞争条件。丢失的viewer 选项似乎随着战略性的Sys.sleep() 调用而消失......

不幸的是,解决这个问题意味着避免使用on.exit() 调用——当然,除非我们能想办法在 RStudio 中处理这个问题。

【讨论】:

    【解决方案2】:

    您可以通过将代码写入临时文件并使用browseURL 或任何您喜欢的方式来获得此功能。

    fg 的要点是相同的,所以我想你可以有一个函数来处理任何类型的 html 代码或小部件。并且可能小部件需要selfcontained = TRUE

    f <- function(x, y = TRUE) {
      x <- if ((inherits(x, 'iplot'))) x else DT::datatable(x)
      if (!y) {
        htmlFile <- tempfile(fileext = '.html')
        htmlwidgets::saveWidget(x, htmlFile, selfcontained = TRUE)
        utils::browseURL(htmlFile)
      } else x
    }
    
    g <- function(x, y = TRUE) {
      x <- htmlTable::htmlTable(x)
      if (!y) {
        htmlFile <- tempfile(fileext = '.html')
        writeLines(x, con = htmlFile)
        utils::browseURL(htmlFile)
      } else x
    }
    
    ## opens in viewer
    g(mtcars)
    ## opens in browser
    g(mtcars, FALSE)
    
    ## same for widgets
    f(mtcars)
    f(mtcars, FALSE)
    
    f(qtlcharts::iplot(1:5, 1:5), FALSE)
    
    ## and my options haven't changed
    getOption('viewer')
    # function (url, height = NULL) 
    # ...
    

    请注意,这实际上是让htmlTable::htmlTable 使用不同查看器的正确方法,但g 应该适用于任何html。

    library('htmlTable')
    print(htmlTable(mtcars), useViewer = utils::browseURL)
    

    【讨论】:

      猜你喜欢
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 2020-06-18
      相关资源
      最近更新 更多