【问题标题】:Custom CSS with knitr and markdown in RR中带有knitr和markdown的自定义CSS
【发布时间】:2012-10-26 23:38:25
【问题描述】:

我找到了这篇很棒的教程,介绍了如何在 Rstudio 中修改使用 markdown 和 knitr 创建的 HTML 报告的 css 格式。帖子可以在here找到。

我希望以此概念为基础,并通过使用相同的 css 来模仿页面 here 的布局。我试图简单地复制/粘贴/组合我在查看页面源时找到的两个 css 文件。

您可以提供的任何帮助将不胜感激!这是我第一次尝试做任何 CSS。

【问题讨论】:

    标签: r markdown knitr


    【解决方案1】:

    这是RStudio提供的方法:http://www.rstudio.com/ide/docs/authoring/markdown_custom_rendering

    options(rstudio.markdownToHTML = 
      function(inputFile, outputFile) {      
        require(markdown)
        markdownToHTML(inputFile, outputFile, stylesheet='custom.css')   
      }
    ) 
    

    我一直无法让它正常工作,所以我做的有点不同:

    我通过创建标准输出文件来做到这一点,然后将标题和 css 代码放在 R 的顶部:

    tmp <- readLines("your.html") 
    tmp <- tmp[-c(1:50)] # or however many lines it is before the css ends
    write(tmp,"your.html")
    

    然后我使用 pandoc 将我自己的 css 添加到一个独立的文件中

    system("pandoc -s -S your.html -c your.css -o output.html")
    

    【讨论】:

    • 谢谢!仍然不完美,但这有很大帮助。
    • 是的,第一种方法我从来没有运气,所以我只是后期处理。如果你得到它的工作 - 让我知道!
    • 根据 RStudio 页面,您可以直接进入 Pandoc,通过将 rstudio.markdownToHTML 函数更改为通过 Pandoc 而不是使用内置的 markdown 到 html 转换,这是一个一步的过程功能。 Link(向底部)
    【解决方案2】:

    在 RStudio 之外(也可以在其中工作 - 我不确定,因为我不经常使用它),您可以使用选项“markdown.HTML.stylesheet”来设置自定义样式表。然后它会将 .css 文件中的所有内容导入到新创建的 html 文件中。

    这是一个例子:

    ## Set file names
    htmlName <- "test.html"
    rmdName <- gsub("html","Rmd", htmlName) 
    stylesheetName <- 'style.css'
    
    ## Generate rmd file from R
    sink(file = rmdName, type='output') 
        cat('\n<textarea maxlength="3000" cols="70">') 
        cat("Hello World!") 
        cat('</textarea>\n') 
    sink()
    
    ## Generate style sheet from R
    sink(file = stylesheetName, type='output') 
        cat("textarea {color: #a10000; }\n")
    sink()
    
    ## Set knitr options and knit html
    require(knitr) 
    options(markdown.HTML.stylesheet = stylesheetName)
    knit2html(rmdName, output = htmlName) 
    

    【讨论】:

      猜你喜欢
      • 2012-09-03
      • 2014-11-10
      • 2012-08-06
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多