简体   繁体   English

我可以强制knitr将“\\ n”解释为传递给R函数的字符串中的实际换行符吗?

[英]Can I force knitr to interpret “\n” as an actual line feed in a string passed to an R function?

I am trying to use knitr to knit the following markdown to HTML from an .Rmd file using RStudio: 我正在尝试使用knitr使用RStudio从.Rmd文件中将以下markdown编织为HTML:

NOTE: This code will generate an error since you don't have access to the server I am querying, but the problematic HTML will still render and my question just concerns the formatting of the code in the HTML file - not the output from R. 注意:此代码将生成错误,因为您无法访问我正在查询的服务器,但有问题的HTML仍将呈现,我的问题只涉及HTML文件中代码的格式 - 而不是R的输出。

SQL Code 

```{sql}
select 
  count(*) child_count
  ,year(eps_begin) year_plc
from vw_episodes
where eps_begin between '2009-01-01' and '2010-12-31'
  and fl_dur_7 = 0
group by 
  year(eps_begin)
```

R Code

```{r }
sqlQuery(cn, 
         "select 
                count(*) child_count
                ,year(eps_begin) year_plc
              from vw_episodes
              where eps_begin between '2009-01-01' and '2010-12-31'
                and fl_dur_7 = 0
              group by 
                year(eps_begin)")

```

My issue concerns the formatting of the R code when I knit to HTML. 我的问题涉及到我编织HTML时R代码的格式。 I would like the SQL code that is passed to the sqlQuery() function to be formatted similarly to the code that is produced in the SQL chunk. 我希望传递给sqlQuery()函数的SQL代码的格式与SQL块中生成的代码类似。 However, based on the HTML produced, knitr does not appear to be interpreting the \\n in the string as an actual line feed - it's just interpreting it as text. 但是,根据生成的HTML,knitr似乎没有将字符串中的\\n解释为实际换行符 - 它只是将其解释为文本。

The resulting HTML (for the R chunk) looks like this: 生成的HTML(对于R块)看起来像这样:

<pre><code class="r">sqlQuery(cn, &quot;select \n                count(*) child_count\n                ,year(eps_begin) year_plc\n              from vw_episodes\n              where eps_begin between &#39;2009-01-01&#39; and &#39;2010-12-31&#39;\n                and fl_dur_7 = 0\n              group by \n                year(eps_begin)&quot;)
</code></pre>

When what I really want is something like this: 当我真正想要的是这样的:

<pre><code class="r">sqlQuery(cn, 
    &quot;select
        count(*) child_count
        ,year(eps_begin) year_plc
    from vw_episodes
    where eps_begin between &#39;2009-01-01&#39; and &#39;2010-12-31&#39;     
        and fl_dur_7 = 0
    group by 
        year(eps_begin)&quot;)
</code></pre>

Any thoughts on how I can knit to HTML and still preserve the line feeds in strings that are passed to R functions? 有关如何编织HTML并仍保留传递给R函数的字符串中的换行符的任何想法?

Update on 2017/09/21: the chunk option tidy defaults to FALSE in the current version of knitr , so it is no longer necessary to set it (there is no harm doing so, though). 2017/09/21 更新 :在当前版本的knitr中 ,chunk选项tidy默认为FALSE ,因此不再需要设置它(尽管这样做没有任何伤害)。


That was due to the default reformatting of the code using the formatR package; 这是因为使用formatR默认重新格式化代码; you can turn it off by tidy=FALSE : 你可以通过tidy=FALSE关闭它:

```{r tidy=FALSE}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM