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
Related
Beginner to R! Bit of a bizarre question, but is anyone familiar with the compareGroups package in R? There's a function to export files, but I have no idea where the file ends up in - can't seem to find it doing a computer search. (I use RStudio). The CRAN guide wasn't particularly helpful.
The functions I tried were export2csv() and export2word()
Thank you!
Found a solution. Oddly the formula doesn't follow instructions in the package's manual. In case anyone else gets the same problem, export2word(createTable(compareGroups(x ~ y1 + y2, data=dataset), file="filename.doc")
If you run
getwd()
you should get back the directory path that RStudio is using for your current project at that moment. If you do not specify a special destination for the export, I believe it should output it in that directory. You can also run
list.files()
to list the files in your current working project directory
I'm trying to read the RDS file I downloaded from here:
https://github.com/jcheng5/googleCharts/tree/master/inst/examples/bubble
However, when I try to load it into RStudio via:
data<- readRDS('/Users/nathanf/shinyCharts/healthexp.rds')
I get the Error: unknown input format.
I've searched and found a possible solution already posted on StackOverflow, but the solutions mentioned in it do not work.
Does not work:
readRDS(file) in R
Please note, I'm trying to do this with a freshly installed copy of R (3.2.1) on a Mac running Yosemite.
I've found articles online that say the readRDS function is now defunct. https://stat.ethz.ch/R-manual/R-devel/library/base/html/base-defunct.html
Sooooooo....dearest community, what should I do? Is there another way to read RDS files using a new function?
Any help would be much appreciated.
Thank you,
Nathan
I faced exactly the same problem and I can recommend switching from .rds objects into .RData objects. Simply:
save(random_forest1, "random_forest2.RData")
and then
load("random_forest2.RData")
Just for clarity, you will find your object named as random_forest1 after using load function
I have written an R package in which the names of the functions are in Turkish.
I wanna take that package to CRAN with internalization. I changed all of the Turkish names (of functions, of data sets) to English so that everybody can easily use the package. After that, I followed regular "library(roxygen2); library(devtools); library(digest); roxygenize("causfinder"); build("causfinder"); install("causfinder"); library(causfinder)" way in process.
At the end, all of the functions appear with their English names this time in the Object Browser of Revolution R (version 7.1.0 Academic License). So, for the conversion of the names of the functions, all are OK.
Problem:
But, interestingly, not all of the names of data files in the package are not converted to English.
What I did to solve the problem till now:
I tried every sort of trick I know:
1. I deleted package from library location (I have only 1 such location: "C:\Revolution\R-Enterprise-7.1\R-3.0.2\library") completely and rebuild package and install again.
2. I deleted package from working directory "C:\Users\erdogan\Documents\Revolution", and triggered "library(roxygen2); library(devtools); library(digest); roxygenize("causfinder"); build("causfinder"); install("causfinder"); library(causfinder)" process
3. By giving possibility to Buffer effects, I deleted "C:\Users\erdogan\Documents\Revolution\32_7.1" so that "PackageXMLs\causfinder.xml" in that folder does not meddle in improperly. I had R created "32_7.1" folder by restarting Revo R.
4. I applied tricks suggested by "Dirk Eddelbuettel" here:
Update the dataset in an installed package
"updating the source and re-installing with a new distinctive version number": Not worked.
"by forcefully overwriting it, possibly. Not the proper way to do it.": How to apply that force?
My findings that may perhaps give an idea to professionals to solve the problem:
Only one of the data files correctly got renamed, and at the end of that data file in object browser "[Package causfinder version 1.0 Index]" appears. The names of all the other data files are still in Turkish and at the end of those data files, the phrase "[Package causfinder version 1.0 Index]" does not appear! I did not do anything peculiar to that data file whose internalization was done correctly.
Any help will be greatly appreciated.
Step by Step Solution: (Notebook with 32-bit Windows; GUI: Revolution R Enterprise (32-bit))
1. Prepare the environment by cleaning related folders:
1a. Delete the package folder in R's library location via Windows Explorer:
(I have only 1 such location: I deleted "C:\Revolution\R-Enterprise-7.1\R-3.0.2\library\causfinder" folder)
(Run ".libPaths()" to see R's library locations and delete the package's folder from all R's library locations)
1b. Delete the package folder in R's working directory via Windows Explorer:
(I have only 1 such location: I deleted "C:\Users\erdogan\Documents\Revolution\causfinder" folder)
(Run "getwd()" to see R's working directory and delete the package's folder from all R's working directories)
1c. Delete the "32_7.1" or "64_7.1" folder (32bit, 64 bit R, whichever you use) from R's working directory via Windows Explorer:
The .xml file of the package in discussion in this folder may sometimes meddle in and affect the results of R commands, and gives unexpected results.
Delete "C:\Users\erdogan\Documents\Revolution\32_7.1" folder where "PackageXMLs\causfinder.xml" is located.
(When Revolution R restarted, 32_7.1 (or 64_7.1) folder is created automatically if it doesn't exist)
Restart Revolution R now.
2. Create .rda and .Rd files via R and put them in relevant location.
((For English ones, I created .rda and .Rd files like this:
V6Stationary43Obs.df <-
read.csv("C:/Users/erdogan/Documents/Revolution/V6Stationary43ObsWithoutX.csv", header = TRUE, stringsAsFactors = FALSE)
# create V6Stationary43Obs.df.rda dataset file; df to denote data frame
save(V6Stationary43Obs.df, file="V6Stationary43Obs.df.rda")
prompt(V6Stationary43Obs.df) # creates V6Stationary43Obs.df.Rd help file))
((For Turkish ones, I performed the followings once upon a time
D6Duragan43Gozlem.dvc<- read.csv("C:/Users/erdogan/Documents/Revolution/D6Duragan43GozlemXsiz.csv", header = TRUE, stringsAsFactors = FALSE)
# create D6Duragan43Gozlem.vc.rda dataset file; df to denote data frame
save(D6Duragan43Gozlem.vc, file="D6Duragan43Gozlem.vc.rda")
prompt(D6Duragan43Gozlem.vc) # create D6Duragan43Gozlem.vc.Rd help file))
3. Take the .rda and .Rd files (created in Step2) to the "data" and "man" folder in R's working directory via Windows Explorer:
V6Stationary43Obs.df.rda dataset file --> C:\Users\erdogan\Documents\Revolution\causfinder\data
V6Stationary43Obs.df.Rd help file --> C:\Users\erdogan\Documents\Revolution\causfinder\man
4. Fill at least the "Title" and "Description" tags of .Rd files (created in Step3) via R:
"File - Open - File... - V6Stationary43Obs.df.Rd"
\title{
V6Stationary43Obs is..... .
}
\description{
V6Stationary43Obs does..... .
}
5. Apply roxygenization:
library(roxygen2)
library(devtools)
library(digest)
roxygenize("causfinder")
build("causfinder")
install("causfinder")
library(causfinder)
Solution: (Notebook with 64-bit Windows; GUI: Revolution R Enterprise (32-bit))
The above process is performed with the following additional actions:
1. The datasets are created as usual (for example; ".df" to denote data frame):
X.df <- read.csv("C:/Users/erdogan/Documents/Revolution/X.csv", header = TRUE, stringsAsFactors = FALSE)
save(X.df, file="X.df.rda") # X.df.rda dataset is created
prompt(X.df) #X.df.Rd help file is created.
2. Revolution R is closed, and in the working directory, the folder "32_7.3" is deleted. We delete this folder because: The information (functions, data sets, etc.) of our package (here: causfinder) are stored as .xml file in 32_7.3. However, this .xml file cannot update itself once we add our data sets one by one.
3. Revolution R is opened (the folder "32_7.3" is re-created). At this point, Step 5 above (Applying roxygenization) is performed. Once we do this, the datasets are visible in Revolution R's Object Browser.
4. The control of everything realized seamlessly: Go to the "data" folder of package's library location. Here, you must see only ".rdb, .rds, .rdx" files.
Solution: (Notebook with 64-bit Windows; GUI: Revolution R Enterprise (64-bit))
Apply the steps for 32-bits. If everything is OK, then fine: you are done. If not OK (i.e. datasets are not visible in Object Browser of Revo R and data folder of the package in the library location does not include only the .rdb, .rds, .rdx files), then do not panic:
You can still work with the datasets you created that are seen as .rda files in the package's library location: Use "data" command:
data(YourDatasetName, package = causfinder, lib.loc = YourLibraryLocation)
Learn about data command more. Upon using this command, you get the dataset in the Object Browser as an object of Global Environment.
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
I want to find the location of the script .R files which are used for computation in R.
I know that by typing the object function, I will get the code which is running and then I can copy and edit and save it as a new script file and use that.
The reason for asking to find the foo.R file is
Curiosity
Know what is the algorithm used in the numerical computations
More immedietly, the function from stats package I am using, is running results for two of the arguments and not the others and have to figure out how to make it work.
Error shown by R implies that there might be some modification required in the script file.
I am looking for a more general answer, if its possible.
Edit: As per the comments so far, here is the code to compute spectrum of a time series using autoregressive methods. The data input is a univariate series.
x = ts(data)
spec.ar(x, method = "yule-walker") 1
spec.ar(x, method = "burg") 2
command 1 is running ok.
command 2 gives the following error.
Error in ar.burg.default(x, aic = aic, order.max = order.max, na.action = na.action, :
Burg's algorithm only implemented for univariate series
I did try specify all the arguments correctly like na.action=na.fail, order.max = NULL etc but the message is the same.
Kindly suggest possible solutions.
P.S. (This question is posted after searching the library folder where R is installed and zip files which come with packages, manuals, and opening .rdb, .rdx files)
See FAQ 7.40 How do I access the source code for a function?
In most cases, typing the name of the function will print its source
code. However, code is sometimes hidden in a namespace, or compiled.
For a complete overview on how to access source code, see Uwe Ligges
(2006), “Help Desk: Accessing the sources”, R News, 6/4, 43–45
(http://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf).
When R installs a package, it evaluates all the ".R" source files and re-saves them into a binary format for faster loading. Therefore you typically cannot easily find the source file.
As has been suggested elsewhere, you can simply type the function name and see the source code, or download the source package and find the source there.
library(plyr)
ddply # prints the source for ddply
# See the content of the R directory for plyr,
# but it's only binary files:
dir(file.path(find.package("plyr"), "R"))
# [1] "plyr" "plyr.rdb" "plyr.rdx"
# Get the source for the package:
download.packages("plyr", "~", type="source")
# ...then unpack and inspect the R directory...
.libPaths() should tell you all of your current library locations. It's possible to have more than one installation of a package if there are two libraries but only the one that is in the first library will be used. Unless you offer the code and the exact error message, it's not likely that anyone will be able to offer better advice.
I think you are asking to see what I call the source code for a function in a package. If so, the way I do it is as follows, which has worked successfully for me on the three times I have tried. I keep these instructions handy in a few places and just copied and pasted them here:
To see the source code for a function in Program R download the package containing the function. Specifically, download the file that ends in "tar.gz". This is a compressed file. Expand the compressed file using, for example, "WinZip". Now you need to open the uncompressed file that ends in ".tar". Download the free software "7-Zip". Click on the file "7zFM.exe" and navigate to the directory containing the ".tar" file. You can extract the contents of that ".tar" file into a new folder. The contents consist of R files showing the source code for the functions in the R package.
EDIT:
Today (July 8, 2012) I was able to open the 'tar.gz' file using the latest version of 'WinZIP' and could copy the contents (the source code) from there without having to use '7-Zip'.
EDIT:
Today (January 19, 2013) I viewed the source code for functions in base R by downloading the file
'R-2.15.2.tar.gz'
To download that file go to the http://cran.at.r-project.org/ webpage and click on that file in this line:
"The latest release (2012-10-26, Trick or Treat): R-2.15.2.tar.gz, read what's new in the latest version."
Unzip the file. WinZip will work, or it did for me. Then search your computer for readtable.r or another base R function.
agstudy noted here https://stackoverflow.com/questions/14417214/source-file-for-r-function that source code for read.csv is located in the file readtable.r, so do not expect every base R function to have its own file.