A tool to import ESRI Shapefiles into Tableau using R
##Installing Programs and Packages
- Download and install R (http://cran.r-project.org/bin/windows/base/)
- Open RStudio
- Click the packages button
- Click on Install Packages
- Type “rgdal, maptools, sp, rgeos” into the Packages line
- Click Install
##Running Code
-
In RStudio, select File > New File > R Script
-
Copy/Paste contents of ImportFunctions.R into new file
-
Select File > New File > R Script
-
Copy contents of TabShapeRTemplate.R into new file
-
Add the full path of the directory where Shapefile is stored after directory =
-
If using Windows, ensure that all “\” characters are replaced with “/” characters or you will receive a “ ‘\U’ used without hex digits” error message
-
Add name of ESRI shapefile after name = a. Do not include any extensions in the name.
-
Add name of the file where the data should write to after endName =
-
Delete the # in front of the appropriate TabShapeR command based on the type of data being imported (lines, points, or polygons)
-
Highlight all code and select Run (this may run for a while)
###Lines
- Connect to .csv file using Connect to Data > Text File
- Import All.
- Select Line in the plot type drop down on the Marks card.
- Deselect Analysis > Aggregate Measures.
- Drag LineID to the Details shelf and PlotOrder to the Path shelf of the Marks card.
- Double click on Latitude, and then double click on longitude.
###Polygons
- Connect to .csv file using Connect to Data > Text File
- Import All.
- Select Polygon in the plot type drop down on the Marks card.
- Deselect Analysis > Aggregate Measures.
- Drag PolygonID to the Details shelf and PlotOrder to the Path shelf of the Marks card.
- Double click on Latitude, and then double click on longitude.
###Points
- Connect to .csv file using Connect to Data > Text File
- Import All.
- Drag PointID to the Details shelf of the Marks card.
- Double click on Latitude, and then double click on longitude.
Given a bunch of Latitude/Longitude data and a polygon ShapeFile, add what Polygon each point belongs to the underlying data. This will perform better than using R-Integration, but will also be less cool.
##Steps
- Copy the code from Add Points to Polygon to R Studio
- Edit lines 2-5 to put the directory where the files are, the shapefile name, and the name of the .csv file with the lat/lon data
- If you have not installed the packages, uncomment lines 7-11 to install the necessary packages
- Run the file
- New CSV with matching polygons will appear in the specified directory