use*_*503 10 csv r r-markdown
...请参阅rmarkdown示例代码.
通过修改rmarkdown片段来欣赏证明解决方案的答案.
---
title: "Reproducable Example"
author: "user2030503"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Example: mtcars
```{r}
write.csv2(mtcars, "./file.csv")
# Code to embed mtcars as csv
# Code to provide mechanism for button or link for later user interaction to open/save the csv.
```
## Problem
* I want to embed the csv file into the html generated by this rmarkdown script.
* Embedding here means, that the csv data are integral part of the hmtl (i.e. for offline use).
* I want a mechanism (button or link) in the html, which allows the user to open/save the data the csv.
## Search for a solution
There are techniques for embedding rdata files.
* http://rmarkdown.rstudio.com/articles_rdata.html
* https://github.com/richarddmorey/BayesFactorExtras/blob/master/BayesFactorExtras/R/downloadURI.R
## Question
* Dispite of above approaches, I did not find a solution yet how to solve the problem.
* How can it be achieved demonstrating it via this reproducable example ?
Run Code Online (Sandbox Code Playgroud)
hrb*_*str 18
没有javascript; 没有小工具; 没有额外的CSS; 4 LoC(如果你喜欢不可读的代码,则cld为1 LoC):
```{r}
write.csv2(mtcars, "./file.csv")
library(magrittr)
readLines("./file.csv") %>%
paste0(collapse="\n") %>%
openssl::base64_encode() -> encoded
```
[Download CSV](`r sprintf('data:text/csv;base64,%s', encoded)`)
Run Code Online (Sandbox Code Playgroud)
非常坦率的:
你也可以这样做:
<a download="mtcars.csv" href="`r sprintf('data:text/csv;base64,%s', encoded)`">Straight HTML Download Link</a>
Run Code Online (Sandbox Code Playgroud)
如果您想为浏览器(以及用户)提供建议的文件名(适用于降价规则中的HTML放置).
注意:
readBin("./file.csv", "raw", file.info("./file.csv")$size) %>%
openssl::base64_encode() -> encoded
Run Code Online (Sandbox Code Playgroud)
同样和readLines()版本一样.