Dev Space

Customize & extend the power of Alteryx. SDKs, APIs, custom tools, and more!
The Expert Exam is now live online! Read about the specifics and what it took to bring it to life in the blog by our very own Elizabeth Bonnell!
SOLVED

R tigris package Inbound Pipe Error

Highlighted
8 - Asteroid

I am attempting to use the R package tigris within an R tool, in order to retrieve geographies using the Census API. The attached R code (saved as txt to allow upload) works as expected in RStudio. Note that tigris is not a package included in the Predictive Analytics RInstaller, but the package is correctly installed and available to Alteryx. Running the R tool produces the following error: The Designer x64 reported: InboundNamedPipe GetOverlappedResults: The pipe has been ended.

 

Note that on occasion, the error message also includes "You have found a bug. Replicate, then let us know. We shall fix it soon." I opened a case, but was told that since the package is not part of the Predictive Analytics RInstaller that it is very unlikely the Dev team would address, so the case was closed.

 

Any advice on the potential issue would be helpful. @SydneyF -- I know you're one of the resident Alteryx-R gurus; perhaps you have an idea why this error is produced and potentially how it could be remedied?

Highlighted
9 - Comet

Hi @abrasch,

 

From what I can see, tr_WI_Winnebago is a simple feature (sf, https://r-spatial.github.io/sf/articles/sf1.html) and Alteryx Designer expects a data.frame.

 

> class(tr_WI_Winnebago)
[1] "sf" "data.frame"

 

Following should work in Alteryx Designer. Bear in mind that it is going to drop geometry. Depending on your goal with the data, this might not be the right solution for you.

 

## Code Example
# Load the `tigris` package
library(tigris)

# Use the `tracts`` function to download all tracts in a specific state and ounty
tr_WI_Winnebago <- tigris::tracts(
state = "WI", # Specify state
county = "Winnebago", # Specify county
year = 2019, # Specify year
cb = FALSE) # Specify cartographic boundary (True) or TIGER Line/Shapefile (False)

library(sf)
st_geometry(tr_WI_Winnebago) <- NULL


# In Alteryx, urite out to output anchors
write.Alteryx(tr_WI_Winnebago, 1)

 

 

Best,

 

PaulN

Highlighted
8 - Asteroid

Hi @PaulNo,

 

You're correct, tigris returns sf objects. Thanks for the clarification about Alteryx expecting a data frame. Your use of st_geometry() to set the geometry to null may come in handy with future analysis use cases, but unfortunately, the point of my current objective is precisely to acquire the geometries and use them in Alteryx.

 

Do you know of any way around this issue? Perhaps the sf object can be turned into something that Alteryx can understand, like a shapefile?

 

Much appreciated,

Alex

Highlighted
9 - Comet

Hi @abrasch,

 

What about the following?

 

It uses package geojsonsf to convert geometry to geojson. This will allow you to convert geometry column to a spatial object.

 

 

## Code Example
# Load the `tigris` package
library(tigris)

# Use the `tracts`` function to download all tracts in a specific state and ounty
tr_WI_Winnebago <- tigris::tracts(
	state = "WI", # Specify state
	county = "Winnebago", # Specify county
	year = 2019, # Specify year
	cb = FALSE) # Specify cartographic boundary (True) or TIGER Line/Shapefile (False)


# Convert geometry field to geojson
library(geojsonsf)
geometry <- sfc_geojson(tr_WI_Winnebago$geometry)
# Convert geometry list to a data.frame
geometry_df <- data.frame(geometry=matrix(unlist(geometry), nrow=length(geometry), byrow=T))

# Remove original geometry column
library(sf)
st_geometry(tr_WI_Winnebago) <- NULL

# Output original data frame without geometry column and converted column
write.Alteryx(cbind(tr_WI_Winnebago,geometry_df),1)

 

 

Then, I would use a Select tool to convert geometry field to a Spatial Object:

 

 
 

I have attached the workflow as an example.

 

R is not my first programming language so feel free to adapt code and share the result!

 

Best,

 

PaulN

Highlighted
8 - Asteroid

Hi @PaulNo,

 

This is fantastic! You could have fooled me with R not being your first programming language. I tested in Alteryx & RStudio, and you're solution functions as expected. I did make two updates.

1. When creating the data frame, I included the GEOID.

 

geometry_df <- data.frame(GEOID = tr_WI_Winnebago$GEOID, geometry = matrix(unlist(geometry), nrow = length(geometry), byrow = T))

 

2. That allowed me to join the original data (sans sf geometry) to the new geojson geometry data frame, rather than cbind--out of a concern for the possibility of the datasets getting out of order with one another (i.e., join rather than union by position).

 

tr_WI_Winnebago_geo <- tr_WI_Winnebago_removesf %>% inner_join(geometry_df, by = c("GEOID" = "GEOID"))

 

I attached an R Markdown (HTML in zip file) and Alteryx workflow detailing the question, slightly updated solution/code sample.

Thanks again for your help on this!

9 - Comet

Nice job @abrasch!!

 

I am personally a big fan of the HTML file!

 

There should be way to produce a SpatialObject directly from the R tool but I thought workflow made more sense with a select tool.

 

Many thanks for sharing your update!

 

PaulN