问题描述
我的所有R代码均在独立脚本中按预期运行.一旦进入R Markdown文件,get_map()调用就会中断.
All of my R code runs as expected in a standalone script. Once inside an R Markdown file the get_map() call breaks down.
map <- get_map(location = 'minneapolis', zoom = 9)
我收到一个错误:
有什么想法为什么knitr和get_map的表现不好?
Any ideas why knitr and get_map aren't playing nice?
推荐答案
我花了一段时间才弄清楚问题所在.根本原因是ggmap
在closeAllConnections()
的四个功能中被粗鲁":mapdist()
,geocode()
,revgeocode()
和route()
; knitr
使用 evaluate
包评估R代码,这将打开文本连接以记录R输出.因为ggmap
已关闭所有连接,所以evaluate
将无法再次关闭其连接,这导致了您看到的错误.参见 https://github.com/hadley/evaluate/blob/master/R/watcher.r 了解详情.
It took me a while to figure out the problem. The root reason is ggmap
was being "rude" to closeAllConnections()
in four of its functions: mapdist()
, geocode()
, revgeocode()
and route()
; knitr
uses the evaluate
package to evaluate R code, which opens text connections to record R output. Because ggmap
has closed all connections, evaluate
will not be able to close its connections again, which caused the error you saw. See https://github.com/hadley/evaluate/blob/master/R/watcher.r for details.
通常应该使用close()
函数明确指出哪些连接将关闭,并且使用closeAllConnections()
是危险的,因为这可能会关闭不应被关闭的连接.我不明白为什么作者必须使用它,我想您需要将此问题报告给他.最后,我们应该能够运行此程序而不会出现错误:
Normally one should be explicit about which connections to close using the close()
function, and it is dangerous to use closeAllConnections()
because this may close connections which are not supposed to be closed. I do not understand why the author has to use it, and I guess you need to report this issue to him. Finally we should be able to run this without errors:
library(evaluate); library(ggmap)
evaluate("map <- get_map(location = 'minneapolis', zoom = 9)")
这篇关于如何在knitr中使用ggmap库的get_map函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!