Knit with parameters in R Markdown by selecting shapefile (.shp) as file input

I posted this same question last week on Stack Exchange but did not receive any answers.

I am trying to render an R Markdown script to a PDF using Knit with parameters. I want other people to be able to render the report using a UI generated by the YAML header. I would like to use a shiny control (file) as as a parameter input instead of the generic text one (i.e. the UI opens up a window in which the user can select the file from a File Explorer).

Minimal reproducible example:

I first create a copy of the sf package's nc.shp so that I can easily find it when testing the UI:

library(sf)
sf_nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
sf::st_write(sf_nc, 'C:/Temp/nc_temp.shp')

Here is the R Markdown (.rdm) file

title: "Params_Test"
output: pdf_document
params:
shp_program:
input: file
label: 'NC Shapefile'
value: 'C:/Temp/nc_temp.shp'
multiple: FALSE
buttonLabel: 'browse shapefiles'

knitr::opts_chunk$set(echo = TRUE)

library(sf)
library(ggplot2)

sf_nc_temp <- sf::st_read(params$shp_program)

plot <- ggplot2::ggplot(sf_nc_temp) +
  geom_sf(aes(color = NAME)) +
  geom_sf_text(aes(label = NAME)) 
plot

The tool runs fine when I just Knit using the default (Knit drop down icon > Knit with parameters > Knit). However I get the following error message when I try to select the shapefile from the UI: Line 20 Error: Cannot open 'C:\Users\username\AppData\Local\Temp\1\Rtmp8gVT2L\file2784148636a\0.shp"; The source could be corrupt or not supported. See st_drivers() for a list of supported formats.

The comments from the Stack Overflow question suggest that the issue is that a shapefile really has multiple files (with the same name but different extension) associated with it (like a .dbf) and that it is looking for those as well. I tried allowing for multiple files to be selected and included all associated files, but this did not work either.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.