Error opening SHP file in R using maptools readShapePoly - r

I am new to R and was following the following tutorial on the ggplot2 package found here. However the readShapePoly() function throws an error whenever I try to load the basic shapefile. I have used the following code:
library("ggplot2")
library("gpclib")
library("maptools")
setwd("~/Documents/R Projects/Intro to ggplot2")
#Intro to ggplot 2 contains the .shp file and associated data
sport <- readShapePoly("london_sport.shp")
which gets me:
Error in getinfo.shape(filen) : Error opening SHP file
I have tried omitting the file extension. I have also tried downloading other .shp files which throw the same error too. I have also tried calling readShapePoly using the full file path, which doesn't work either. I am using R studio (mac OSX), but I get the same error using the standard R window. I have tried the suggestions on the previous closed threat "Error opening SHP file in Rstudio", but to no avail.
Edit: the error was with a missing .dbf file. Thanks to #Spacedman for the fix.

I had a similar issue, and it was because there were several other files along with the '*.shp' shape file in the zip package that I downloaded. Then I only moved the shape file to another folder and it didn't work. When including all files together, it was fine and I could readShapeSpatial() function okay.

Forget ggplot and gpcclib. Stick to maptools and rgdal that actually provide tools for reading a shapefile.
Don't just say you've tried "this and that", outline the details. For example, does `file.exists("london_sport.shp") return TRUE?
Also, what makes you think readShapePoly() is the right function? It only knows how to read polygon shapefiles, try readShapePoints() and readShapeLines() as well.
If you can, try readOGR which can read a shapefile despite many caveats (including the geometry type).
library(rgdal)
readOGR("~/Documents/R Projects/Intro to ggplot2", "london_sport")
If you can report on all of those things it's likely someone could help.

There is one more, but not much automatic solution which helped me:
file<- readShapePoly(file.choose())
Then just find your *.shp file and run it.

Include three of those files (extensions: dbf, shp, shx) in the same folder.

In case this helps anyone:
I had the same problem but none of the solutions worked. Worst, I the same was going on with an script that I'm 100% sure was working previously.
Turns out that it could also be that the shapefile gets damaged. Apparently this can happen while the file is being manipulated (or so that say my geographer friends), so next time you try to open it won't work for no apparent reason. Downloading it again worked fine, but makes me think to have a copy of the more precious ones just in case.

I just managed to fix this problem with a shapefile I was trying to read by typing:
file<- readShapeSpatial("filename.shp")
instead of typing in the full file path.
Before then, I tried all the suggestions, including making sure that the .dbf and .shx files were also present. Don't know why this should be the case.

I had the same problem. I found out that basically you need three of those files: .shp, .shx and .dbf

Related

Missing command in an R package

So to get to the point: I need to use an R package called machuruku. To get familiar with the package I used the dataset provided in the original paper (https://academic.oup.com/sysbio/article/70/5/1033/6171196). While trying to run the code for the simulation I get an error message saying that the command "machu.simulation" doesn't exist. Any of you have any idea why that's happening? Am I missing a package?
I downloaded the dataset zip file, dove into the second nested zip file Guillory_and_Brown_simulation-validation.zip, then into its file code_simulation-validation.R, and noticed that this source file uses machu.simulation several times before defining the function starting in line 519.
Suggestions:
Grab lines 519 through the end, save into a different file, source that new file, then try to run the code in the beginning of the file again.
Complain (not quietly?) to the authors, the fact that they think this is reproducible means they might have missed something else, too.

Cannot open data source. .GDB in R

I am trying to load the data from the National Address Database provided by
Transportation.gov into R. The data can be downloaded by anyone after accepting the disclaimer at this link: https://www.transportation.gov/gis/nad/disclaimer
I download the data, unzipped it into a directory I called data and then tried to use rgdal to list all the layers present in the data via:
fc_list<- rgdal::ogrListLayers("./data/NAD_20180215.gdb").
However, I cannot get rgdal to return anything other than an error saying "Cannot Open Data Source"....
I am wondering how I would go about listing the layers present in the .gdb folder as well as reading them into R?
I'm very grateful for any help. Thank you in advance.
-nate
Following the suggestion here, using a full path to the gdb folder helped in my case.
# check for package and install if needed
if(!require(rgdal)){
install.packages("rgdal", dep=T)
library(rgdal)
}
# full path to the geodatabase required
fgdb <- "C:/full/path/to/the/geodatabase.gdb"
# list all feature classes in a file geodatabase
subset(ogrDrivers(), grepl("GDB", name))
ogrListLayers(fgdb)
paths in mac are written differently.
https://rpubs.com/bpattiz/Directories_Paths_Workspaces
And especially:
https://derekyves.github.io/2016/05/10/codeshare.html
First you need to set your working directory.
And then probably:
fc_list<- rgdal::ogrListLayers("/data/NAD_20180215.gdb")
or
fc_list<- rgdal::ogrListLayers("~/data/NAD_20180215.gdb")
will work

Text mining with tm in R antiword error

So I'm rather new to R, and I'm learning how to mine text from this handy website: https://eight2late.wordpress.com/2015/05/27/a-gentle-introduction-to-text-mining-using-r/
I do have my own text set of .doc, .docx, and .xlsx files and I'm trying to mine them. They're located in a folder in my working directory called 'files', but I have already encountered an error after simply writing a few lines of code.
The code I have so far is:
library(tm)
library(readtext)
data = readtext('files')
At this point, after waiting for 25 seconds or so, I get the error:
Error: System call to 'antiword' failed (1): The Big Block Depot is damaged
and the code stops running there.
I have tried searching online for solutions but it seems like a fairly rare error and so I only found 1 possible solution at https://github.com/ropensci/antiword/issues/1 but that did not work for me.
This solution suggested that one of my files were corrupt, and suggested using the code
fixInNamespace(antiword, pos="package:antiword")
to change the error to a warning to not interrupt the reading of the files. I tried that, and at first it raised the error of
Error in as.environment(pos):
no item called "package:antiword" on the search list
After which, I loaded the antiword library with a library(antiword) and changed the stop( to a warning(. However, when I ran the data = readtext('files') line again, it immediately raised the error
Error in is_windows() : could not find function "is_windows"
I'm at a loss here! Any help would be appreciated. Should I be using another package in this case?
I had the same problem with my code, where I tried to get a doc. file in R. I also used the readtext library. What helped me was converting the Word documents I was trying to get into R from doc. to docx. When I ran the same code after it worked.

\code{\link{function-name}} in roxygen2

It is my first experience in writing an R-package. I used roxygen2 by following the instructions given in this link http://kbroman.org/pkg_primer/
Everythig is working fine except few things.. there could be a simpler solution to solve the issues, but I am not finding clues what I am doing wrong. I hope someone here in this blog can give a solution to solve my issues.
First issue is about {\code\link{function-name}} in roxygen2:
In .R script I inlcuded this line:
#' #seealso \code{\link{s2a}}
After documenting (generating .Rd files) there is no hyperlink to s2a ,
in documentation s2a shows like a normal text not like hyperlink..
export(s2a) is listed out in NAMESPACE.
Is there any other place i need to modify ?
Second issue is about data():
I saved the dataset in .Rdata format and placed in the data/ in package directory. I also created the .R script in R/ as like following steps here http://kbroman.org/pkg_primer/pages/data.html
In DESCRIPTION file LazyData: true .
but when I type data(shh) in R console gives a warning message
data(shh)
Warning message:
In data(shh) : data set ‘shh’ not found
Any ideas is of great help:)
It's been a while since you asked this, but I was having the exact same problem with hyperlinks in documentation not appearing correctly, so for anyone who might be having a similar problem: Are you possibly viewing the development documentation? The links don't seem to work there. (You'll know this is the case if you see Using development documentation for your_function_name in your console output when you run ?your_function_name.)
The links should appear in the non-development documentation. To generate this you can try building and reloading your package, for instance by following the steps here: http://r-pkgs.had.co.nz/man.html#man-workflow-2

Problems while reproducing Sankey chart example with d3_sankey

I am trying to reproduce simple example with rCharts library to plot sankey chart. I found this example from scratch and tried to reproduce it, however, I came up with some problems.
Firstly I have tried running this code without nothing. Then I found out and realized, that I need d3_sankey in my computer. So, I have downloaded it from here and copied to C:\Users\adomas\Documents\R\win-library\3.0\rCharts\libraries\widgets\d3_sankey.
Then I've tried that unchanged code once more and still got the following error:
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file '/config.yml': No such file or directory
Since that didn't help too, I have tried changing paths from:
sankeyPlot$setLib('libraries/widgets/d3_sankey')
sankeyPlot$setTemplate(script = "libraries/widgets/d3_sankey/layouts/chart.html")
to
sankeyPlot$setLib('C:/Users/adomas/Documents/R/win-library/3.0/rCharts/libraries/widgets/d3_sankey')
sankeyPlot$setTemplate(script = "C:/Users/adomas/Documents/R/win-library/3.0/rCharts/libraries/widgets/d3_sankey/layouts/chart.html")
However, this time, the blank screen in Viewer appeared but no actual Sankey graph and I got the following in console:
Warning message:
In readLines(file, warn = warn, ...) :
invalid input found on input connection 'C:/Users/adomas/Documents/R/win-library/3.0/rCharts/libraries/widgets/d3_sankey/layouts/chart.html
I have tried changing paths and etc, but still something is wrong. And I am not sure if I need to use sankeyPlot$setTemplate at all.
I want to use Sankey chart in my shiny application, but firstly I want to reproduce simply in R. Suggestions would help a lot!
Versions of libraries:
rCharts_0.4.2
igraph_0.7.0
R version: 3.0.2
EDITED:
I have tried this code on different system. And everything works there. Versions of libraries and R are the same and I've copied same documents and just specified full path. I have tried that again on my own computer, but still got the same error yet.
I've experienced the same issues. The solution for me was to upgrade rCharts :
devtools::install_github("rCharts", "ramnathv")
Second, I didn't include the sankeyPlot$setTemplate() line.
Finally, the path in $setLib() should be a direct path (as discribed in the solution of #adomasb or the following line can be used to link directly to the original github:
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey')
A downside of this last approach is that internet is always required to run the script.
Alright, eventually it works as it should be.
I just rebooted my system and that's it. However, if anyone would come up with same problem, just be sure you specify direct path, where you placed all necessary files for d3_sankey. Therefore, you rather use
sankeyPlot$setLib('C:/Users/adomas/Documents/R/win-library/3.0/rCharts/libraries/widgets/d3_sankey')
This is wonderful chart type. Good luck!

Resources