2016-12-17 9 views
5
  • 問題文の
  • ソリューションの検索や
  • 質問

... rmarkdownのコード例を参照してください。埋め込みのcsv

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 ? 

答えて

11

ませんジャバスクリプトをインストールしておく必要があります。ウィジェットはありません。余分な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)`) 

かなり簡単:

  • だけの "もの" としてファイルを扱い、ライン
  • はそれをすべて作るようにそれを読んで改行のブロブが
  • 64は、適切なメディアタイプとデータURIを作る
  • エンコードをベースにテキストを分離
  • は、AMとして埋め込みますあなたは(値下げルールでHTMLの配置が適用されます)ブラウザ(と、それゆえ、ユーザーに)提案したファイル名を与えたい場合は

    <a download="mtcars.csv" href="`r sprintf('data:text/csv;base64,%s', encoded)`">Straight HTML Download Link</a> 
    

    :arkdownリンク

はまた、のような何かを行うことができます。

注:

readBin("./file.csv", "raw", file.info("./file.csv")$size) %>% 
    openssl::base64_encode() -> encoded 

readLines()バージョンが均等と同様に動作します。

+0

残念ながら、このソリューションはIE 11(Windows 8)では動作しません。ソリューションによって生成されたリンクは何もトリガーしません。どのように修正することができますか? – user2030503

+2

確かに、実際のブラウザを使用してください;-)これは、IE 11が脳死している既知の問題です:http://caniuse.com/#feat=datauri;これはIE 11用のこの種のデータURIを埋め込むことができないことを意味します。Edgeも脳死です。 – hrbrmstr

+0

このソリューションはChromeには最適ですが、Safariではダウンロードが開始されず、ブラウザにcsvが表示されます。回避策はありますか? –

2

どのようにこのようなものについて:

--- 
title: "Reproducable Example" 
author: "dimitris_ps " 
date: "17 December 2016" 
output: html_document 
--- 

<style> 
    #DataTables_Table_0 { 
    visibility: hidden; 
    } 

    #DataTables_Table_0_paginate { 
    visibility: hidden; 
    } 

</style> 

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

dt <- datatable(mtcars, rownames=T, 
      # filter = 'top', 
       callback=JS('$("a.buttons-collection").css("background","#008CBA"); 
      $("a.buttons-collection").css("font-size","15px"); 
      $("a.buttons-collection").css("border-radius", "8px"); 
      $("a.buttons-collection").css("margin-right","0px"); 
      return table;'), 
     extensions = 'Buttons', 
     options = list(searching=F, 
         paging = T, 
         bInfo = F, 
         columnDefs = list(list(className = 'dt-left', targets = 0), 
             list(className = 'dt-center', targets = 1:11)), 
         pageLength = 1, 
         initComplete = JS("function(settings, json) {", 
             "$(this.api().table().header()).css({'background-color': '#99ccff', 'color': '#003333'});", 
             "}"), 
         dom = 'Bfrtip', 
         buttons = list(
             list(extend = 'collection', 
              buttons = c('excel', 'csv'), 
              text = 'DOWNLOAD DATA') 
         ) 
     ) 
) 

``` 
<br> 

```{r mtcars, echo=FALSE} 
dt 
``` 

あなたはDTライブラリが

+0

はあなたの努力に感謝します。 2つの問題:1.小さなもの:2つのJS関数はDT ::プレフィックスを必要とします。 2.大きなものです:私はテーブルを表示したくありません。ボタンだけが残るようにリファクタリングすることはできますか? – user2030503

+0

ポイント1で、 'library(DT)'を読み込みます。これは最初のものです。それを指摘してくれてありがとう。あなたのpoint2には 'css' hideテーブルの回避策があるかもしれません。私はこれに後で戻ってきます –

+0

私は私の答えを更新しました。最良のアプローチではありませんが、あなたが探しているものに近いです。基本的に、私は 'css'で' DataTable'を隠しています –

1

私は便利な機能embed_data()を用意しhrbrmstrユーザーのanswerに基づいて、行動でそれを参照してください。

--- 
title: "Untitled" 
author: "user2030503" 
date: "17 12 2016" 
output: html_document 
--- 

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

```{r echo=FALSE} 

embed_data= function(x= mtcars, filename= "file.csv", label= "Get data"){ 

    # Create encoded Base64 datastream 
    encode_data= function(x){ 
    write.csv2(x, "./file.csv") 
    enc= sprintf('data:text/csv;base64,%s', openssl::base64_encode(paste0(readLines("./file.csv"), collapse="\n"))) 
    unlink("./file.csv") 
    return(enc) 
    } 

    # String result ready to be placed in rmarkdown 
    paste0("<a download='", filename, "' href=", encode_data(x), ">", label, "</a>") 

} 
``` 

`r embed_data(mtcars, filename="mtcars.csv")` 
関連する問題