如何正确使用knit2html?

时间:2016-02-07 11:55:29

标签: r knitr r-markdown

我正在尝试使用该脚本生成HTML:

title: "SQL"

date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: 
 html_document: 
   keep_md: yes
---

  ```{r setup, echo = FALSE}
library(knitr)
path <- "drive"
extension <- "sql"
```

This document contains the code from all files with extension ``r     extension`` in ``r paste0(getwd(), "/", path)``.

 ```{r, results = "asis", echo = FALSE}
fileNames <- list.files(path, pattern = sprintf(".*%s$", extension))
fileInfos <- file.info(paste0(path, fileNames))

for (fileName in fileNames) {
  filePath <- paste0(path, fileName)
  cat(sprintf("## File `%s` \n\n### Meta data \n\n", fileName))
  cat(sprintf(
   "| size (KB) | mode | modified |\n|---|---|---|\n %s | %s | %s\n\n", 
   round(fileInfos[filePath, "size"]/1024, 2), 
   fileInfos[filePath, "mode"],
    fileInfos[filePath, "mtime"]))
  cat(sprintf("### Content\n\n```\n%s\n```\n\n", paste(readLines(filePath),    collapse = "\n"))) 

使用相同的设置&amp; Windows机器上的R脚本和相同目录运行良好。我想编码有问题。

有时执行的开头是:

During startup - Warning messages:
1: Setting LC_CTYPE failed, using "C" 
2: Setting LC_COLLATE failed, using "C" 
3: Setting LC_TIME failed, using "C" 
4: Setting LC_MESSAGES failed, using "C" 
5: Setting LC_MONETARY failed, using "C" 
6: Setting LC_PAPER failed, using "C" 
7: Setting LC_MEASUREMENT failed, using "C" 

并且执行暂停

然后我必须:

 Sys.setenv(LANG="de_DE.UTF-8")

并重新执行。出现以下输出:

processing file: all_SQLs.Rmd
  |................                                                 |  25%
   inline R code fragments

  |................................                                 |  50%
label: setup (with options) 
List of 2
 $ echo  : logi FALSE
 $ indent: chr "  "

  |.................................................                |  75%
   inline R code fragments

   |.................................................................| 100%
 label: unnamed-chunk-1 (with options) 
List of 2
 $ results: chr "asis"
  $ echo   : logi FALSE


output file:SQL.knit.md

    /usr/lib/rstudio-server/bin/pandoc/pandoc +RTS -K512m -RTS      SQL.utf8.md     --to html --from    markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-  implicit_figures --output all_SQLs.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /usr/local/lib/R/site-library  /rmarkdown/rmd/h/default.html --variable 'theme:bootstrap' --include-in-header   /tmp/RtmpBkTBSH/rmarkdown-str50021b4e910.html --mathjax --variable 'mathjax-   url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-  AMS-MML_HTMLorMML' --no-highlight --variable highlightjs=/usr/local/lib/R/site-  library/rmarkdown/rmd/h/highlight 
  pandoc: Cannot decode byte '\xfc':      Data.Text.Internal.Encoding.Fusion.streamUtf8: Invalid UTF-8 stream
   Error: pandoc document conversion failed with error 1

我认为编码存在问题。但是我通过将R中的区域设置和Ubuntu中的区域设置设置为一个标准来尝试所有方法:

locale R

 [1] LC_CTYPE=de_DE.UTF-8       LC_NUMERIC=C                  LC_TIME=de_DE.UTF-8       
 [4] LC_COLLATE=de_DE.UTF-8     LC_MONETARY=de_DE.UTF-8       LC_MESSAGES=de_DE.UTF-8   
 [7] LC_PAPER=de_DE.UTF-8       LC_NAME=C                     LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=de_DE.UTF-8    LC_IDENTIFICATION=C      


Locale Ubuntu

LANG=de_DE.UTF-8
LANGUAGE=
LC_CTYPE="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_PAPER="de_DE.UTF-8"
LC_NAME="de_DE.UTF-8"
LC_ADDRESS="de_DE.UTF-8"
LC_TELEPHONE="de_DE.UTF-8"
LC_MEASUREMENT="de_DE.UTF-8"
LC_IDENTIFICATION="de_DE.UTF-8"
LC_ALL=

在Rstudio中使用knit2html()代替Knit HTML按钮时,我会得到以下内容:

warning:
1: In readlines (con):
   incomplete last line in '/SQL.Rmd'
2: In RegExpr (".. '? [Hh] [1-6] *> (*) </ [Hh] [1-6] *.?>", Html, perl = TRUE):
   Input string 1 is incorrect UTF-8
3: In Grepl (i, html, perl = TRUE):
   Input string 1 is incorrect UTF-8
4: In Grepl (i, html, perl = TRUE):
   Input string 1 is incorrect UTF-8

0 个答案:

没有答案