2016-12-17 12 views
5
  • उदाहरण
  • समस्या बयान
  • समाधान खोज और
  • प्रश्न

... 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 ? 

उत्तर

11

नहीं जावास्क्रिप्ट की आवश्यकता होगी; कोई विजेट नहीं; कोई अतिरिक्त सीएसएस नहीं;

```{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)`) 

काफी सरल:: 4 एलओसी (cld 1 एलओसी अगर आप अपठनीय कोड की तरह हो सकता है)

  • के रूप में सिर्फ "सामान" और लाइनों
  • कर यह सब के रूप में यह में पढ़ा फ़ाइल का इलाज न्यू लाइन में से एक ब्लॉब
  • यह एनकोड आधार में पाठ अलग 64
  • उचित मीडिया प्रकार के साथ एक डेटा यूआरआई बनाने
  • बजे के रूप में यह एम्बेड arkdown लिंक

तुम भी कुछ ऐसा कर सकते हैं: अगर आप ब्राउज़रों (और, इसलिए, उपयोगकर्ताओं को) देने के लिए एक सुझाव दिया फ़ाइलनाम (markdown नियमों में HTML नियुक्ति लागू) चाहते

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

नोट:

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

भी काम करता है समान रूप से और साथ ही readLines() संस्करण।

+0

दुर्भाग्यवश यह समाधान आईई 11 (विंडोज 8) में काम नहीं करता है। समाधान द्वारा उत्पन्न लिंक कुछ भी ट्रिगर नहीं करता है। इसका समाधान कैसे किया जा सकता है ? – user2030503

+2

ऐ, एक असली ब्राउज़र का उपयोग करें ;-) ऐसा लगता है कि यह आईई 11 के साथ एक ज्ञात समस्या है मस्तिष्क मृत: http://caniuse.com/#feat=datauri; इसका मतलब है कि आप आईईआई 11 के लिए इस प्रकार के डेटा यूआरआई को एम्बेड नहीं कर सकते हैं। ऐसा लगता है कि एज भी मस्तिष्क मृत है। – hrbrmstr

+0

यह समाधान क्रोम के लिए अच्छा है लेकिन सफारी में, एक डाउनलोड शुरू नहीं किया गया है और हम ब्राउज़र पर प्रदर्शित सीएसवी देखते हैं। क्या आसपास कोई काम है? –

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

आपके प्रयास की सराहना। दो मुद्दे: 1. एक छोटा सा है: दो जेएस कार्यों को डीटी :: उपसर्ग की आवश्यकता होती है। 2. एक बड़ा है: मैं एक टेबल प्रदर्शित नहीं करना चाहता हूं। क्या आप रिफैक्टर कर सकते हैं ताकि केवल बटन बने रहें? – user2030503

+0

आप 1 बिंदु पर, 'लाइब्रेरी (डीटी)' लोड करें, यह वही है जो मैंने शुरू में किया था। इस पर ध्यान दिलाने के लिए धन्यवाद। आपके पॉइंट 2 पर 'सीएसएस' छुपा तालिका के साथ कामकाज हो सकता है। मैं बाद में –

+0

पर वापस आऊंगा मैंने अपना जवाब अपडेट कर दिया है। सबसे अच्छा तरीका नहीं है, लेकिन आप जो खोज रहे हैं उसके करीब है। असल में, मैं 'css' –

1

उपयोगकर्ता की answer hrbrmstr मैं एक सुविधा समारोह embed_data() तैयार के आधार पर यह कार्रवाई में देखें:

--- 
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")`