RStudio knitr themes

2019-04-05 03:04发布

问题:

I've just started playing with some of the new knitr features in RStudio.

I've tried selecting several of the different themes in the R Markdown settings dialogue but these don't seem to have any noticeable impact on the style of my resulting document. Should it, or am I missing something? Everything just seems to come out in the default style no matter what theme I select.

---
output:
  html_document:
    number_sections: yes
    theme: spacelab
    toc: yes
  pdf_document: default
  word_document: default
---

Installation details:

  • R version 3.1.1
  • RStudio Version 0.98.977
  • knitr 1.6
  • rmarkdown 0.2.50
  • htmltools 0.2.4
  • Windows 7

回答1:

I had the same problem. Learning the following led me to the solution.

Two different things show up if you google "knitr theme".

  1. highlight parameter = syntax highlighting (1, 2, 3—familiar keywords like kate, tango, solarized-dark)
  2. theme parameter = bootswatch CSS (these are the less familiar keywords like spacelab, superhero, united, yeti)

Here are the instructions of how to add the correct knitr flags at the top of your .Rmd file.


Once you've added something like

---
title: "Impressive Client Report"
output:
  html_document:
    theme: spacelab
    highlight: neon
---

to the top, then open R in the directory where your .Rmd file lives, and run

require(knitr)
knit(input='impressive report.Rmd', output='impressive_report.Rhtml')

(I switched to _ because of another gotcha: I was switching between command-line R and RStudio knitting, not realising that RStudio was creating a different .html file to the one R was creating.)


Or in the case of RStudio, just Ctrl+Shift+K to knit your .Rmd file from the editing window—after changing both theme and highlight to valid values.



回答2:

I had this exact same problem and I was able to solve it by placing the theme argument before any other arguments. I am unsure if the order matters, but in my case it did. For example, this correctly changes my html theme:

---
title: "A Title"
author: "An Author"
date: "last update: `r format(Sys.Date(), format = '%d %B %Y')`" 
output: 
  html_document:
    theme: flatly
    highlight: haddock
    toc: true
    toc_float:
      collapsed: false
      smooth_scroll: true
---

While providing the theme argument towards the end did not work:

---
title: "A Title"
author: "An Author"
date: "last update: `r format(Sys.Date(), format = '%d %B %Y')`" 
output: 
  html_document:
    toc: true
    toc_float:
      collapsed: false
      smooth_scroll: true
  theme: flatly
  highlight: haddock
---

This also was true for my syntax highlighting argument.



回答3:

Be sure that you activate the following option in your RStudio: Tools --> Global Options... --> Sweave --> Weave Rnw files using: knitr

At least it worked with me while compiling pdf from tex format.



标签: r knitr rstudio