简体   繁体   English

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

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

I am trying to render an R Markdown script to a PDF using Knit with parameters.我正在尝试使用带参数的 Knit 将 R Markdown 脚本渲染为 PDF。 I want other people to be able to render the report using a UI generated by the YAML header.我希望其他人能够使用 YAML 标头生成的 UI 呈现报告。 I would like to use a shiny control ( file ) as as a parameter input instead of the generic text one (ie the UI opens up a window in which the user can select the file from a File Explorer).我想使用闪亮的控件文件)作为参数输入而不是通用文本(即 UI 打开一个窗口,用户可以在其中从文件资源管理器中选择文件)。

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:我首先创建了 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')

Here is the R Markdown (.rdm) file这是 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

```

The tool runs fine when I just Knit using the default (Knit drop down icon > Knit with parameters > Knit).当我只使用默认编织(编织下拉图标 > 带参数编织 > 编织)时,该工具运行良好。 This uses the string to the shapefile path as text.这使用字符串到 shapefile 路径作为文本。 在此处输入图片说明

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

在此处输入图片说明

I tried replacing the chunk based on: How do I access the data from a file passed as parameters in a RMarkdown document?我尝试根据以下内容替换块: 如何从作为 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

As @lbusett had mentioned in their comment, you're selecting only one part of the shapefile.正如@lbusett 在他们的评论中提到的,您只选择了 shapefile 的一部分。 The *.shp file is only a component of the shapefile, which is comprised of several files ( .shp , .shx , .dbf , etc.). *.shp 文件只是 shapefile 的一个组件,它由多个文件( .shp.shx.dbf等)组成。

One way to work around this is to adjust your parameters so that multiple = TRUE , which will allow you to select all of the files associated with a particular shapefile (ie place.shp , place.shx , place.df , etc.)解决此问题的一种方法是调整参数,使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'
---

Later in your code, you will need to identify the respective file paths of each file and copy them to your working directory.稍后在您的代码中,您需要确定每个文件的相应文件路径并将它们复制到您的工作目录。 This will ensure that they all share the same name and location.这将确保它们共享相同的名称和位置。

Set the working directory and then use str_which() to identify the appropriate index of params$shp_program for each respective filetype, as follows:设置工作目录,然后使用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
```

When using parameters to load files through Shiny, R copies the selected files over to a temporary directory and renames them.当使用参数通过 Shiny 加载文件时,R 会将选定的文件复制到一个临时目录并重命名它们。 Thus, if you selected "place.shp" , "place.shx" , and "place.dbf" they would be copied to separate subfolders in your local temp directory as "0.shp" , "1.shx" , and "2.dbf" .因此,如果您选择了"place.shp""place.shx""place.dbf"它们将被复制到本地临时目录中的单独子文件夹中作为"0.shp""1.shx""2.dbf" The original file path is lost in this process, so it prevents people after you from seeing which files you selected.原始文件路径在此过程中丢失,因此它可以防止其他人看到您选择的文件。 If your workflow requires peer review, this can be a deal breaker.如果您的工作流程需要同行评审,这可能是一个交易破坏者。

In addition, you may encounter file size limitations that require additional coding to increase beyond the 5mb default.此外,您可能会遇到文件大小限制,需要额外编码才能增加到默认值 5mb 以上。 Specifically, you'll need to drop the following code at the top to increase the file size limit to 30 MB:具体来说,您需要在顶部删除以下代码以将文件大小限制增加到 30 MB:

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

As a result of these issues, I find it easierto use the file.choose() function instead of parameters.由于这些问题的结果,我觉得easierto使用file.choose()函数,而不是参数。 Doing so will allow you to select just the .shp file while preserving the original filepath, so that R will know where the rest of the shapefile's component files are located.这样做将允许您在保留原始文件路径的同时只选择.shp文件,以便 R 知道 shapefile 的其余组件文件所在的位置。

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

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