如何访问作为RMarkdown文档中的参数传递的文件中的数据? [英] How do I access the data from a file passed as parameters in a RMarkdown document?

查看:149
本文介绍了如何访问作为RMarkdown文档中的参数传递的文件中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照RMarkdown的参数化报告页上的最后一个示例,我尝试使用界面,以在YAML标头内使用以下代码选择我的输入文件:

Following the last example on RMarkdown's Parametrized Reports page, I am trying to use the Shiny interface to select my input file with the following code inside the YAML header:

params:  
  data:  
    input: file  
    label: 'Input dataset:'  
    value: myData.csv  

出现Shiny界面,我可以浏览一个文件,但是当我尝试通过read.csv(file=params$data, header=TRUE)R代码中进一步访问该文件时,我收到以下消息:

The Shiny interface shows up and I get to browse for a file, but when I try to access it further down in the R code via read.csv(file=params$data, header=TRUE), I get the following message:

file(file,"rt")中的错误:无法打开连接

Error in file(file, "rt") : cannot open the connection

如何获取我的文件?

注意:我见过线程,其中用户在呈现RMarkdown文档时在函数中传递文件路径,但这不是我要尝试的方法.我只希望能够从Shiny界面中选择它.

Note: I have seen a thread where users pass the file path in a function at the time of rendering the RMarkdown document, but this is not what I am trying to do. I would just like to be able to select it from the Shiny interface.

编辑
经过更多的播放后,我认为问题是当我尝试通过shiny接口选择的文件时创建并以params $ data形式传递的临时文件不再存在.
实际上,file.exists(params$data)返回FALSE.

EDIT
After playing a bit more, I think the issue is that the temporary file created when reading the file I select via the shiny interface and passed as params$data doesn't exist anymore when I try to access it.
Indeed, file.exists(params$data) returns FALSE.

所以我想我的问题变成了:如何删除此临时文件?

So I guess my question now becomes: How do I get to read this temporary file before it is erased ?

推荐答案

看起来不再可以复制了.

It doesn't look like this can be reproduced anymore.

仅出于解释目的:

  1. 创建一个新的RMarkdown文档
  2. 在顶部添加以下yaml块:

  1. Create a new RMarkdown document
  2. Add the following yaml block to the top:

params:  
  data:  
    input: file  
    label: 'Input dataset:'  
    value: myData.csv  

  • 将以下R块添加到文档中:

  • Add the following R chunk to the document:

    ```{r data}
    cat(params$data)
    c <- read.csv(params$data)
    print(c)
    ```
    

  • 在编织"下拉菜单中选择带参数编织"选项

  • Select under the "Knit" dropdown the "Knit with Parameters" option

    这篇关于如何访问作为RMarkdown文档中的参数传递的文件中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆