2015-04-18 5 views
6

I html- und pdf-Notebooks von R -scripts bin Erzeugung mit 's Funktion render() und knitr' s Funktion spin(). Manchmal verwende ich geschachtelte Listen und mische sie mit Codeblöcken. Hier ist ein Beispiel mit und knitr Chunk-Optionen.Rmarkdown die render() + knitr Spin(): Wie Codeblöcke mischen und verschachtelte Elemente

#' (1) This is normal text. 
#' (a) This is normal text but indented. 
#+ echo = TRUE, eval = TRUE 
print("This is code") 
#' (b) This is supposed to be normal text with the same 
#'  indentation as (a). However, it will be formatted as code. 
#'  By this I mean that e.g. in a pdf-notebook it will be 
#'  correctly indented but the font will be the same font as 
#'  the code. 

jedoch alles, was den Code folgt nach Listenposition (a) als auch Code markiert werden (zum Beispiel (b)). Aber was ich erreichen möchte, ist (b) als normaler Text markiert zu haben und den gleichen Einschnitt wie (a) zu verwenden. Ist es möglich, dies zu tun?

Antwort

3

Es gibt eine interne Chunk-Option indent, die der Chunk-Ausgabe einen Einzug hinzufügen kann. In Ihrem Fall können Sie vier Leerzeichen angeben, z.

#+ echo = TRUE, eval = TRUE, indent = ' ' 
3

Sie haben zu verwenden, was The four-space rule in der Dokumentation genannt wird: http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#the-four-space-rule

arbeitet also der folgende Code

(1) This is normal text. 

    Continued. 

    (a) This is normal text but indented. 

     ```{r, echo = TRUE, eval = TRUE} 
     summary(cars) 
     ``` 

    (a) This is normal text with the same indentation as (a). 

Hinweis: Es gibt

  • 2 Spaces infront die (1)
  • 4 Spaces Infront jeweils (a)
  • 8 Spaces direkt vor dem Codeblock

in Resultierende: enter image description here

lief ich es mit rmarkdown::render("test.Rmd") und das ist meine Session info

R version 3.1.1 (2014-07-10) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 

locale: 
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 
[4] LC_NUMERIC=C     LC_TIME=German_Germany.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

loaded via a namespace (and not attached): 
[1] digest_0.6.8 evaluate_0.5.5 formatR_1.0  htmltools_0.2.6 knitr_1.9  rmarkdown_0.5.1 
[7] stringr_0.6.2 tools_3.1.1  XML_3.98-1.1 yaml_2.1.13  
+0

Danke. Ich kann es nicht zum Laufen bringen, wenn ich ein 'R'-Skript direkt mit' render ("test.R", output_format = "pdf_document") kompiliere ''. –

+0

Ihre Datei sollte '* .Rmd' genannt werden, anstatt' * .R', aber anders als: Seltsam. Für mich funktioniert es einwandfrei: Resultierend in http://www.filedropper.com/test_50 – Rentrop

+0

'render()' kann auch zu reinen, einfachen 'R'-Skripten verwendet werden, nicht nur' * .Rmd' Dateien zu Notizbüchern . Und damit meine ich ein Skript wie [this] (https://drive.google.com/open?id=0B_UAut69TSAic0ZvZC1qUlZDY28&authuser=0). Es basiert auf 'spin()', das kompilierte reine 'R'-Skripte in' * .Rmd'-Dateien umwandelt, und davon rede ich. Eine Erklärung finden Sie [hier] (https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R). @Yihui, kannst du vielleicht helfen? –

Verwandte Themen