访问R markdown参数文件的原始文件名

drm*_*iod 7 parameters markdown r

我想为用户提供一种定义输入文件的便捷方法。为此,我使用 markdown 中的参数功能。如果我“使用参数编织”,我会被要求提供输入文件。

有机会检索文件名吗?因为我在降价期间创建了一些不同的文件,并且我将使用输入文件的文件名作为前缀。到目前为止,文件被上传到临时目录中,原始文件名丢失了。

如何通过下拉菜单将文件名和位置获取到我的 Markdown 文档中?我不希望用户手动写入路径和文件名。

---
title: "Untitled"
date: "11/16/2021"
output: html_document
params:
  date_file:
    label: "date file"
    value: 'dates.tsv'
    input: file
---

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

## R Markdown

Filename: `r params$date_file`
Run Code Online (Sandbox Code Playgroud)

Mik*_*gan 5

您可以通过嵌入 Shiny 应用程序让用户在呈现的文档中选择文件。需要注意的是,涉及用户选择依赖性的所有表达式都必须包装在. 如果你想教授 R,这显然不是最佳选择,但如果它有帮助或激发更好的答案,这里有一个例子:reactive()

## Create a TSV file for testing
cat("x\ty\n1\t2\n3\t4\n", file = "test.tsv")
Run Code Online (Sandbox Code Playgroud)
---
title: "Untitled"
output: html_document
runtime: shiny
---

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

Users can select a file by interacting with an embedded Shiny application.

```{r ui, echo=FALSE}
inputPanel(
  fileInput("file", label = "Select a TSV file:", accept = ".tsv")
)
```

User input is stored in a named list-like object `input`, 
but this object can only be accessed inside of a _reactive context_.

```{r test1, error=TRUE}
input$file$name
## Name of file selected by user ... _not_ an absolute or relative path
fn <- reactive(input$file$name)
fn
## Absolute path of temporary copy of file
dp <- reactive(input$file$datapath)
dp
```

Don't be misled by how `fn` and `dp` are printed. They are not 
strings, but _reactive expressions_. As as a result, they, too, 
must be handled inside of a reactive context.

```{r test2}
class(fn)
reactive(class(fn()))
```

Some more examples:

```{r test3, error=TRUE}
## Define reactive data
dd <- reactive(read.table(file = dp(), sep = "\t", header = TRUE))
## Do stuff with it
reactive(names(dd()))
reactive(nrow(dd()))
## Write object to file in _working directory_
reactive(saveRDS(dd(), file = sub("tsv$", "rds", fn())))
```
Run Code Online (Sandbox Code Playgroud)


Mar*_*cus 4

作为使用 Shiny 运行时的替代方案,您还可以将Shiny Gadgets与自定义 Knit 按钮行为结合使用(据我了解,这很大程度上是您使用“Knit withParameters”时发生的情况)

您需要两件事:一个运行小工具的函数,一个在编织时运行的函数。

该小工具本质上是一个 Shiny 应用程序,但您可以使用miniUI. 作为一个迷你应用程序,您可以做更多的事情,但这里是一个基本的实现。

library(shiny)
library(miniUI)

get_file_param <- function(){
  
  ui <- miniPage(
    miniContentPanel(
      
      fileInput("dateFile", "Date File")
      
      # ...Other UI elements to collect other parameters... 
      
    ),
    miniTitleBar(
      NULL,
      right = miniButtonBlock(
        miniTitleBarCancelButton(),
        miniTitleBarButton("done", "Done", primary = TRUE))
    )
    
  )
  
  server <- function(input, output, session){
    
    # Handle the Done button being pressed
    observeEvent(input$done, {
      # Return the full file info data.frame. See ?shiny::fileInput
      stopApp(input$dateFile)
    })
    
  }
  
  runGadget(
    app = ui, 
    server = server, 
    viewer = paneViewer() #dialogViewer("Knit with Parameters - Custom UI")
  )
  
}
Run Code Online (Sandbox Code Playgroud)

knit 函数将调用小工具,然后在调用中使用其输出rmarkdown::render

knit_with_file <- function(input, ...){
  
  fileInfo <- get_file_param()
  
  rmarkdown::render(
    input,
    params = list(
      date_file = list(name = fileInfo$name, datapath = fileInfo$datapath)
    ),
    envir = globalenv()
  )
  
}
Run Code Online (Sandbox Code Playgroud)

要自定义编织按钮,您可以向knit文档 YAML 标头中的字段提供一个函数。R Markdown 食谱建议您将其保存在一个包中,但我将上述两个函数放在文件(“knitFx.R”)中,rmarkdown 文档将来源该文件。

---
title: "Untitled"
date: "11/16/2021"
output: html_document
params:
  date_file: 'dates.tsv'
knit: (function(input, ...) {

    source("knitFx.R")
    
    knit_with_file(input, ...)

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

## R Markdown

**Original Name of Uploaded File**: *`r params$date_file$name`*

**Local Temp File Path**: *`r params$date_file$datapath`*
Run Code Online (Sandbox Code Playgroud)

当用户点击“Knit”时,将显示一个用户界面来选择文件。基于上述实现,namedatapath将可在 rmarkdown 文档中使用。

这是呈现的 HTML 文档: 在此输入图像描述