【问题标题】:Working R script code not working in Markdown - RStudio hangs工作 R 脚本代码在 Markdown 中不起作用 - RStudio 挂起
【发布时间】:2015-11-19 10:48:48
【问题描述】:

有人可以帮助我了解有效的 r 脚本代码如何导致 rStudio 中的 Markdown 文档挂起并对我必须终止会话和 IDE 的点无响应吗?也没有编织任何文件

背景:

  • 在 2 台不同的机器上试过,运行最新的 rStudio 版本 Windows 7 和 Windows 10 上的 0.99.489 和 R 3.2.2(64 位)。
  • 尝试使用 packrat 管理包
  • 尝试从项目目录中加载形状文件
  • 尝试从 spdep 项目下载中加载不同的形状文件
  • 再次尝试重新安装软件包

R 脚本 获取所需的库并加载它们

#Install the GIS packages
install.packages("spdep",dependencies = TRUE)
install.packages("maptools",dependencies = TRUE)
#Load the libraries
library(spdep)
library(maptools)

这在 R 中有效(您需要在系统上为 shape 文件获取正确的位置)

#See where the library files are stored
.libPaths() 
#Load the Eire shape file which came in spdep package using the readShapePoly function from maptools (needed to change the slashes from windows to those supported in R)
eireMap <- readShapePoly("C:R/3.2/spdep/etc/shapes/eire.shp"[1],ID="names", proj4string=CRS("+proj=utm +zone=30 +units=km"))
#Plot the map as there was no problem reading the shape file correctly in R script
plot(eireMap)

R Markdown

---
title: "GIS using R"
author: "Me"
date: "18 November 2015"
output: word_document
---

This is an R Markdown document of the R worksheet for GIS. Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code contained in that snippet

```{r, echo=FALSE}
#Install the packages if not done already
install.packages("spdep",dependencies = TRUE)
install.packages("maptools",dependencies = TRUE)
```

Load the libraries 
```{r, echo=FALSE}
library(spdep)
library(maptools)
```


See where the library files are stored
```{r}
.libPaths() 
```

Try to Load the Eire shape file but it causes R Studio to hang and become unresponsive 
```{r}
eireMap <- readShapePoly("C:R/win-  library/3.2/spdep/etc/shapes/eire.shp"[1],ID="names", proj4string=CRS("+proj=utm +zone=30 +units=km"))
```

Can't plot the map because the system has hung by this stage
```{r}
plot(eireMap)
names(eireMap)
eireMap $names
```

【问题讨论】:

  • 创建“eireMap”的代码行似乎不同,这是故意的吗?
  • FWIW system.file("etc/shapes/eire.shp", package="spdep") 是获取该 shapefile 的正确方法。另外,为什么每次编织时都强制重新安装软件包?

标签: r rstudio r-markdown


【解决方案1】:

这将让您在重新安装之前检查软件包(并且在加载它们时不会显示任何消息)。然后它使用跨平台的方式获取您需要的 shapefile 的文件名,然后读取并绘制它。各个 R 代码行(即非 Rmd)在 R 控制台和 RStudio + Rmd 中运行良好,在我的 OS X 系统和 linux 系统上运行良好。有更优雅的方法来进行包检查/加载,但这可以让您快速检查系统是否存在更大的问题。

---
output: html_document
---

```{r echo=FALSE, message=FALSE}
if (!require(spdep)) install.packages("spdep",dependencies=TRUE)
if (!require(maptools)) install.packages("maptools",dependencies=TRUE)
require(spdep)
require(maptools)
```

```{r}
eire_shp <- system.file("etc/shapes/eire.shp", package="spdep")

eireMap <- readShapePoly(eire_shp, ID="names", 
                         proj4string=CRS("+proj=utm +zone=30 +units=km"))
```

```{r}
plot(eireMap)
names(eireMap)
eireMap$names
```

【讨论】:

  • 谢谢,你的方式更干净,但更重要的是在 markdown 中工作
猜你喜欢
  • 2014-07-24
  • 2021-04-13
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
  • 2022-10-19
  • 1970-01-01
  • 2018-02-04
  • 2020-07-25
相关资源
最近更新 更多