2016-06-16 12 views
6

की टेक्स्ट सामग्री में लाइन नंबर जोड़ें मैं रमार्कडाउन में एक रिपोर्ट लिख रहा हूं जिसे HTML में प्रस्तुत किया जाएगा क्योंकि इसमें Rshiny भाग शामिल हैं। क्या कोई तरीका है कि मैं फ़ाइल में लाइन नंबर जोड़ सकता हूं?एक रेंडर किए गए rmarkdown एचटीएमएल दस्तावेज़

महत्वपूर्ण बात मैं पाठ और पूछा here के रूप में कोड हिस्सा के लिए नहीं करने के लिए लाइन नंबर की जरूरत है।

मुझे आश्चर्य है कि कुछ सीएसएस को आरएमडी फ़ाइल में जोड़ना संभव है लेकिन यह नहीं पता कि यह कैसे करना है।

--- 
title: "Title" 
author: "Yourname" 
date: "June 16, 2016" 
output: html_document 
runtime: shiny 
--- 


This R Markdown document is made interactive using Shiny. 
Unlike the more traditional workflow of creating static reports, you can now create documents that allow your 
readers to change the assumptions underlying your analysis and see the results immediately. 

## Inputs and Outputs 

You can embed Shiny inputs and outputs in your document. Outputs are automatically updated whenever inputs change. 
This demonstrates how a standard R plot can be made interactive by wrapping it in the Shiny `renderPlot` function. 
The `selectInput` and `sliderInput` functions create the input widgets used to drive the plot. 

आपको बहुत बहुत

उत्तर

6

दिलचस्प सवाल धन्यवाद,

पौलुस और के बाद से मैं RMarkdown दस्तावेजों के अंदर जे एस और jQuery के साथ चारों ओर खेलना पसंद है मैं इसे एक शॉट दे दी है।

यह समाधान बुलेटप्रूफ नहीं है। यह केवल फ़ायरफ़ॉक्स के साथ परीक्षण किया गया है। चूंकि jQuery की क्रॉस-ब्राउज़र संगतता एक गड़बड़ है क्योंकि यह शायद फ़ायरफ़ॉक्स के साथ ही काम करेगी।

प्रत्येक पंक्ति को अब सामान्य अनुच्छेदों, अनियंत्रित और आदेशित सूचियों, स्रोत कोड और आउटपुट हिस्सों सहित लेबल किया गया है।

यहाँ पूरा काम कर दस्तावेज है:

--- 
title: "Title" 
author: "Yourname" 
date: "June 16, 2016" 
output: html_document 
runtime: shiny 
--- 

<style> 
    /* Style the linenumber div */ 

    .linenumbers { 
    border: 1px solid #ccc; 
    border-radius: 4px; 
    background-color: #EBEBEB; 
    text-align: center; 
    padding: 0px 3px; 
    font-family: monospace; 
    float: left; 
    position: absolute; 
    transform:translate(-125%); 
    font-size: inherit !important; 
    } 

    .main-container { 
    margin-left: 8% !important; 
    } 

    /* fixes the problem with inline code 
    that makes the line spacing bigger: */ 
    p > code { 
    line-height: 90% !important; 
    } 
</style> 


<script> 
    var $elements = $('p:not(:has(>img)), pre, ul, ol').slice(start=1); 

    $(document).ready(function(){ 
    $elements.wrap('<numbering/>'); 
    $('numbering').prepend('<div class=\"linenumbers\"/>'); 

    updateLines = function(elmts) { 
     var counter = 1; // counts total number of lines 
     $(elmts).each(function() {  

     if($(this).is('pre')) { 
      var $elmt = $(this).children('code'); 
      var styles = $(this).css([ "padding-top", "padding-bottom", "line-height"]); 
      $(this).siblings('.linenumbers').css(styles); 
     } else { 
      var $elmt = $(this); 
     } 

     var h = $elmt.outerHeight(); // get the height 
     var nl = Math.round(h /parseFloat($elmt.css('line-height'))); 
     var text = ''; 
     for(var i=counter; i < counter + nl; ++i) { 
      text += i + '</br>'; 
     } 
     counter += nl; 
     $(this).siblings('.linenumbers').html(text); 
     }); 
    }; 
    updateLines($elements); 

    }); 

    $(window).resize(function() { 
    updateLines($elements); 
    }); 
</script> 

This R Markdown document has the ability to interactively number 
the lines of text content. It might not be perfect but it sure 
looks nice. If you find a bug or have a tip for how to further improve 
the code, please let me know. I am no professional JavaScript 
programmer so this was made by an amateur ;) 


What if I leave some space here? 


## Inputs and Outputs 

Here starts another paragraph. Look how the line numbers jump over to 
this one! This is amazing! For demonstrating purposes I have to write 
some more lines. So I bore you a bit more with my nonsense. NONSENSE! 
Ok let us try out some inline code and see whether the line numbers 
still align. `library(ggplot2)` And a second `time()`. Looks ok I 
guess. 
Here starts another paragraph. Look how the line numbers jump over to 
this one! This is amazing! For demonstrating purposes I have to write 
some more lines.  
So I bore you a bit more with my nonsense. NONSENSE! Ok let us try out 
some inline code and see whether the line numbers still align. 
`library(ggplot2)` And a second `time()`. Looks ok I guess. 

```{r} 
x <- 1:5 
B <- 'Martin' 
head(mtcars) 
``` 

```{r} 
plot(1) 
``` 

तुम बस लाइन

var $elements = $('p:not(:has(>img)), pre, ul, ol').slice(start=1); 

तो से इसी टैग हटाने के द्वारा कुछ भागों बाहर कर सकते हैं यदि आप उत्पादन मात्रा लेबल करने के लिए नहीं करना चाहते हैं और सूचियों, इसे

var $elements = $('p:not(:has(>img)), pre.r').slice(start=1); 

(आउटपुट हिस्सों के विपरीत, स्रोत कोड भाग कक्षा .r ले जाएं।)

जैसा कि मैंने जटिल दस्तावेजों के लिए कहा था, आप कुछ कीड़े पर ठोकर खा सकते हैं। मुझे पता है अगर आप करते हैं;)

enter image description here

+0

यह अद्भुत है! हम कोड भाग को शामिल करने के लिए संख्या कैसे प्राप्त कर सकते हैं? मैंने उनके आसपास डालने की कोशिश की लेकिन इसका कोई प्रभाव नहीं पड़ा। – Ben

+0

मार्टिन, बहुत बहुत धन्यवाद! चमकदार तत्वों के साथ rmarkdown में एक सम्मेलन कागज लिखने की दिशा में एक बड़ा कदम। मैंने दो चीजें देखीं। यह फ़ायरफ़ॉक्स 47.0 में काम करता है लेकिन मेरे कंप्यूटर पर क्रोम 51.0 में नहीं। इसके अलावा, अगर टैग के भीतर एक खाली खाली रेखा है तो यह नंबरिंग रोकता है। कोई विचार? – Paul

+1

पॉल और @ बेन: धन्यवाद और आपका स्वागत है। मैंने समस्याओं में देखा। पहली बात यह है कि मैं कह सकता हूं कि ये jQuery तंत्र क्रॉस-ब्राउज़र संगत नहीं हैं, इसलिए आपको फ़ायरफ़ॉक्स पर चिपकना होगा। कल मैं कुछ नए कोड समर्थन स्रोत कोड भाग के साथ जवाब अपडेट कर दूंगा। –

संबंधित मुद्दे