繁体   English   中英

通过选择 shapefile (.shp) 作为文件输入,在 R Markdown 中使用参数编织

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

我正在尝试使用带参数的 Knit 将 R Markdown 脚本渲染为 PDF。 我希望其他人能够使用 YAML 标头生成的 UI 呈现报告。 我想使用闪亮的控件文件)作为参数输入而不是通用文本(即 UI 打开一个窗口,用户可以在其中从文件资源管理器中选择文件)。

最小可重现示例:

我首先创建了 sf 包的 nc.shp 的副本,以便在测试 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')

这是 R Markdown (.rdm) 文件

---
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'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r, eval = TRUE, include = 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

```

当我只使用默认编织(编织下拉图标 > 带参数编织 > 编织)时,该工具运行良好。 这使用字符串到 shapefile 路径作为文本。 在此处输入图片说明

但是,当我尝试从 UI 选择 shapefile 时收到以下错误消息:第 20 行错误:无法打开 'C:\\Users\\username\\AppData\\Local\\Temp\\1\\Rtmp8gVT2L\\file2784148636a\\0.shp";源可能已损坏或不受支持。有关支持的格式列表,请参阅st_drivers()

在此处输入图片说明

我尝试根据以下内容替换块: 如何从作为 RMarkdown 文档中的参数传递的文件访问数据?

library(sf)
library(ggplot2)

cat(params$shp_program)
c <- sf::st_read(params$shp_program)
c

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

正如@lbusett 在他们的评论中提到的,您只选择了 shapefile 的一部分。 *.shp 文件只是 shapefile 的一个组件,它由多个文件( .shp.shx.dbf等)组成。

解决此问题的一种方法是调整参数,使multiple = TRUE ,这将允许您选择与特定 shapefile 关联的所有文件(即place.shpplace.shxplace.df等)

---
title: "Params_Test"
output: pdf_document

params:
  shp_program: 
    input: file
    label: 'NC Shapefile'
    value: 'C:/Temp/nc_temp.shp'
    multiple: TRUE
    buttonLabel: 'browse shapefiles'
---

稍后在您的代码中,您需要确定每个文件的相应文件路径并将它们复制到您的工作目录。 这将确保它们共享相同的名称和位置。

设置工作目录,然后使用str_which()为每个相应的文件类型标识params$shp_program的适当索引,如下所示:

```
{r, eval = TRUE, include = TRUE}

library(sf)
library(ggplot2)

setwd("C:/temp")

shp_index<- str_which(params$shp_program, ".shp")
shx_index <- str_which(params$shapefile, ".shx")
dbf_index <- str_which(params$shapefile, ".dbf")
prj_index <- str_which(params$shapefile, ".prj")    

file.copy(params$shapefile[shp_index], "temp_shape.shp")
file.copy(params$shapefile[shx_index], "temp_shape.shx")
file.copy(params$shapefile[dbf_index], "temp_shape.dbf")
file.copy(params$shapefile[prj_index], "temp_shape.prj")

sf_nc_temp <- sf::st_read("temp_shape.shp")

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

当使用参数通过 Shiny 加载文件时,R 会将选定的文件复制到一个临时目录并重命名它们。 因此,如果您选择了"place.shp""place.shx""place.dbf"它们将被复制到本地临时目录中的单独子文件夹中作为"0.shp""1.shx""2.dbf" 原始文件路径在此过程中丢失,因此它可以防止其他人看到您选择的文件。 如果您的工作流程需要同行评审,这可能是一个交易破坏者。

此外,您可能会遇到文件大小限制,需要额外编码才能增加到默认值 5mb 以上。 具体来说,您需要在顶部删除以下代码以将文件大小限制增加到 30 MB:

options(shiny.maxRequestSize = 30*1024^2)

由于这些问题的结果,我觉得easierto使用file.choose()函数,而不是参数。 这样做将允许您在保留原始文件路径的同时只选择.shp文件,以便 R 知道 shapefile 的其余组件文件所在的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM