如何在knitr中使用ggmap库的get_map函数? [英] How do I use the ggmap library's get_map function inside of knitr?

查看:93
本文介绍了如何在knitr中使用ggmap库的get_map函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的所有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)

我收到一个错误:

标签:未命名的块2
sink()中的警告:没有要删除的接收器
标签:unname-chunk-2
process_file(text)中的错误:
从第53-64行退出:close.connection(con)中的错误:无效的连接
调用:knit-> process_file
执行停止 编织器以状态1终止

label: unnamed-chunk-2
Warning in sink() : no sink to remove
label: unnamed-chunk-2
Error in process_file(text) :
Quitting from lines 53-64: Error in close.connection(con) : invalid connection
Calls: knit -> process_file
Execution halted knitr terminated with status 1

有什么想法为什么knitr和get_map的表现不好?

Any ideas why knitr and get_map aren't playing nice?

推荐答案

我花了一段时间才弄清楚问题所在.根本原因是ggmapcloseAllConnections()的四个功能中被粗鲁":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函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆