SF Package - Spatial Data Simplified
The sf
package (Simple Features package) provides a standardized model for accessing geographic features, primarily working with two-dimensional geometries. It helps developers natively represent all 17 simple feature types across multiple dimensions (XY, XYZ, XYM, XYZM) in R. The package integrates with several essential tools: GEOS for performing geometrical operations on projected coordinates, GDAL, which serves as a translator library for both raster and vector geospatial data formats, and PRØJ, a generic coordinate transformation software that specializes in converting geospatial coordinates from one coordinate reference system to another.
In short, it’s a powerful R tool for working with spatial data, implementing Simple Features standards that ensure consistency and interoperability across geographic information systems.
GDAL is Missing - Understanding the Error
A common issue when installing the sf
package is encountering the error:
# MacOS
ERROR: configuration failed for package 'sf' [...] GDAL not found
# Linux
Error: package or namespace load failed for ‘sf’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/usr/local/lib/R/site-library/units/libs/units.so':
libudunits2.so.0: cannot open shared object file: No such file or directory
unable to load shared object '/usr/local/lib/R/site-library/sf/libs/sf.so':
libgdal.so.34: cannot open shared object file: No such file or directory
This occurs because the library interfaces with GDAL, which is written in C++, and thus needs to be compiled when the package is installed. While this process is straightforward on Windows with Rtools, MacOS and Linux require additional system dependencies to be installed.
Fix - Installing on MacOS
For MacOS users, Homebrew provides a convenient formula to install both gdal
and proj
simultaneously:
# Install package configuration tool for development libraries
brew install pkg-config
# Install GDAL and PROJ
brew install gdal
After installing GDAL, you can install the sf package from source in R. The current version of proj on Homebrew requires additional configuration:
install.packages("sf", type = "source",
configure.args = "--with-proj-lib=$(brew --prefix)/lib/")
Fix - Installing on Linux
For Ubuntu and Debian-based distributions, you’ll need to install several system libraries before installing the sf package:
# Install basic dependencies
sudo apt-get update
sudo apt-get install -y \
libudunits2-dev \
libgdal-dev \
libgeos-dev \
libproj-dev
Deploying Shiny-R Applications with SF
Project Template
For a working example of a Shiny-R application with SF and Docker, check out our template on GitHub.
To deploy a Shiny-R application with SF, we need to configure an environment with all necessary dependencies. Docker provides an excellent solution for this by creating a reproducible environment within a containerized Linux instance.
Project Structure
Your project should be organized as follows:
.
├── Dockerfile
├── app.R
├── install.R
└── startApp.R
Dockerfile Configuration
Here’s a complete Dockerfile that sets up your application:
# Start with Shiny & R pre-installed
FROM rocker/shiny
# Set working directory
WORKDIR /app
# Install SF dependencies for Linux
RUN apt-get -y update && apt-get install -y \
libudunits2-dev \
libgdal-dev \
libgeos-dev \
libproj-dev \
&& rm -rf /var/lib/apt/lists/*
# Install R packages
COPY install.R /app/
RUN Rscript install.R
# Copy application files
COPY . /app
# Launch the application
ENTRYPOINT ["Rscript", "startApp.R"]
Application Startup Configuration
While your Shiny application logic lives in app.R
, we need the startApp.R
to properly configure and start it in the Docker container.
Create a startApp.R file with the following content:
library(shiny)
options(shiny.host = '0.0.0.0')
options(shiny.port = 80)
runApp('app.R')
This configuration:
- Loads the Shiny library
- Sets the host to 0.0.0.0 to allow external connections to the container
- Sets the port to 80 for HTTP traffic
- Runs your Shiny application defined in app.R
Deployment Steps
Follow these steps to deploy your application:
# Install Ploomber Cloud CLI
pip install ploomber-cloud
# Set up authentication
ploomber-cloud key <YOUR_PLOOMBER_KEY>
# Initialize your project
ploomber-cloud init
# Deploy with live monitoring
ploomber-cloud deploy --watch
Once deployed, your application will be accessible at your-app-id.ploomber.app
.
Need enhanced security? We provide options for password protection and enterprise authentication systems. Learn more about our security features in our documentation.