为什么 RMarkdown 的“渲染”行为取决于它是从 RStudio Server 还是从 PHP shell 调用的?
Why does RMarkdown `render` behavior depend on whether it's called from RStudio Server or from a PHP shell?
我有一个包含 'special characters' 的 RMarkdown 文档,例如 ë
。如果我使用 RStudio Server 的 "knit document" 按钮呈现文档,它呈现得很好。当我通过使用 RStudio Server 按钮来调用另一个调用 RMarkdown
的 render
函数的 R 脚本来渲染它时,它也可以正常渲染。
但是,由于某些我无法理解的原因(但希望不会持续很长时间),当 index.php
使用以下方式调用相同的 R 脚本时,我得到了不同的结果:
$results = shell_exec("R --file='/home/username/public_html/some/subdirectories/process.R' --no-save 2>&1");
当我这样做时,在生成的 .html 文件中,特殊符号(我猜是 unicode 符号)被替换为 <U+00EB>
。我试图查看这是否是我还不知道的 HTML 元素的某种变体,但我一直找不到任何相关信息。
(注意:任何 link 到我可以了解更多信息的地方(以及,当我们在这里时,为什么我的浏览器不显示它,例如,代表,亦不胜感激!)
可重现的例子
example.php
的内容:
<?php
shell_exec("R --file='/home/username/public_html/subdirectory/example.R' --no-save 2>&1");
?>
example.R
的内容(这是我服务器上需要的):
workingPath <- "/home/username/public_html/subdirectory";
### Set path to RStudio's pandoc version
Sys.setenv(PATH=paste(Sys.getenv("PATH"),
"/usr/lib/rstudio-server/bin/pandoc",
sep=":"));
### Set HOME and LANG
Sys.setenv(HOME = '/home/username');
Sys.setenv(LANG = 'en_US.UTF-8');
require(rmarkdown);
renderResults <-
render(file.path(workingPath, 'example.Rmd'),
output_file = file.path(workingPath, 'example.html'),
intermediates_dir = file.path(workingPath, 'tmp'),
encoding="UTF-8");
example.Rmd
的内容:
---
title: 'Reproducable example'
output: html_document
---
```{r}
cat("This is an ë symbol.");
```
这个例子的结果:
当我从 R Studio 中 运行 时,我得到:
cat("This is an ë symbol.");
## This is an ë symbol.
当我 运行 来自 PHP 时,我得到:
cat("This is an ë symbol.");
## This is an <U+00EB> symbol.
(请注意,有趣的是,echo
'ed ë 确实正常显示...)
我现在求助于在 index.php
文件中执行 str_replace
,但这并不理想。
我已经查看了 render
手册,但找不到有关此行为的任何信息。
我还查看了在 .Rmd 文件的 YAML header 中为 pandoc
指定选项,但唯一似乎接近的是 --ascii
option, and that doesn't do anything. The R Studio RMarkdown page 没有也请提供任何提示。
它可能与 RStudio 中设置的环境变量有关吗?我已经必须设置:
Sys.setenv(HOME = '/home/oupsyusr');
Sys.setenv(LANG = 'en_US.UTF-8');
在 R 脚本中,在从 PHP shell 调用的 R 脚本中首先调用 Pandoc;但如果这是问题所在,我如何确定 RStudio 将哪些设置设置为哪些值,或者更准确地说,哪些设置是重要的?我运行:
Sys.getenv()
在 R Studio 中,它显示了一个列表。我认识到 none 的条目与编码有关。
或者,knitr 是否导致此问题?当我存储和检查 .md 文件时,Unicode 元素已经显示出来了。但是,knitr help page with chunk options 并没有说明任何关于 unicode 或编码的一般信息。
有没有人知道这是在哪里记录的,或者有没有人以前碰巧遇到过这种情况?
我运行在 CentOS 6.8 上安装 RStudio 0.99.903 和 R 3.3.1。
通常,这种形式的问题(其中 unicode 字符被转换为 unicode 代码点表示,例如 <U+00EB>
在这种情况下)是由尝试 运行 R 在非UTF-8 语言环境。
通常,这可以通过检查 Sys.getlocale("LC_ALL")
的输出来验证。如果您看到报告的 C
语言环境,那么您可能需要使用类似以下内容的 UTF-8 语言环境:
Sys.setlocale("LC_ALL", "en_US.UTF-8")
根据您想要的语言替换特定的 UTF-8 语言环境风格。 (作为参考,通常可以从终端使用 locale -a
之类的内容查询可用语言环境集)。
我有一个包含 'special characters' 的 RMarkdown 文档,例如 ë
。如果我使用 RStudio Server 的 "knit document" 按钮呈现文档,它呈现得很好。当我通过使用 RStudio Server 按钮来调用另一个调用 RMarkdown
的 render
函数的 R 脚本来渲染它时,它也可以正常渲染。
但是,由于某些我无法理解的原因(但希望不会持续很长时间),当 index.php
使用以下方式调用相同的 R 脚本时,我得到了不同的结果:
$results = shell_exec("R --file='/home/username/public_html/some/subdirectories/process.R' --no-save 2>&1");
当我这样做时,在生成的 .html 文件中,特殊符号(我猜是 unicode 符号)被替换为 <U+00EB>
。我试图查看这是否是我还不知道的 HTML 元素的某种变体,但我一直找不到任何相关信息。
(注意:任何 link 到我可以了解更多信息的地方(以及,当我们在这里时,为什么我的浏览器不显示它,例如,代表,亦不胜感激!)
可重现的例子
example.php
的内容:
<?php
shell_exec("R --file='/home/username/public_html/subdirectory/example.R' --no-save 2>&1");
?>
example.R
的内容(这是我服务器上需要的):
workingPath <- "/home/username/public_html/subdirectory";
### Set path to RStudio's pandoc version
Sys.setenv(PATH=paste(Sys.getenv("PATH"),
"/usr/lib/rstudio-server/bin/pandoc",
sep=":"));
### Set HOME and LANG
Sys.setenv(HOME = '/home/username');
Sys.setenv(LANG = 'en_US.UTF-8');
require(rmarkdown);
renderResults <-
render(file.path(workingPath, 'example.Rmd'),
output_file = file.path(workingPath, 'example.html'),
intermediates_dir = file.path(workingPath, 'tmp'),
encoding="UTF-8");
example.Rmd
的内容:
---
title: 'Reproducable example'
output: html_document
---
```{r}
cat("This is an ë symbol.");
```
这个例子的结果:
当我从 R Studio 中 运行 时,我得到:
cat("This is an ë symbol.");
## This is an ë symbol.
当我 运行 来自 PHP 时,我得到:
cat("This is an ë symbol.");
## This is an <U+00EB> symbol.
(请注意,有趣的是,echo
'ed ë 确实正常显示...)
我现在求助于在 index.php
文件中执行 str_replace
,但这并不理想。
我已经查看了 render
手册,但找不到有关此行为的任何信息。
我还查看了在 .Rmd 文件的 YAML header 中为 pandoc
指定选项,但唯一似乎接近的是 --ascii
option, and that doesn't do anything. The R Studio RMarkdown page 没有也请提供任何提示。
它可能与 RStudio 中设置的环境变量有关吗?我已经必须设置:
Sys.setenv(HOME = '/home/oupsyusr');
Sys.setenv(LANG = 'en_US.UTF-8');
在 R 脚本中,在从 PHP shell 调用的 R 脚本中首先调用 Pandoc;但如果这是问题所在,我如何确定 RStudio 将哪些设置设置为哪些值,或者更准确地说,哪些设置是重要的?我运行:
Sys.getenv()
在 R Studio 中,它显示了一个列表。我认识到 none 的条目与编码有关。
或者,knitr 是否导致此问题?当我存储和检查 .md 文件时,Unicode 元素已经显示出来了。但是,knitr help page with chunk options 并没有说明任何关于 unicode 或编码的一般信息。
有没有人知道这是在哪里记录的,或者有没有人以前碰巧遇到过这种情况?
我运行在 CentOS 6.8 上安装 RStudio 0.99.903 和 R 3.3.1。
通常,这种形式的问题(其中 unicode 字符被转换为 unicode 代码点表示,例如 <U+00EB>
在这种情况下)是由尝试 运行 R 在非UTF-8 语言环境。
通常,这可以通过检查 Sys.getlocale("LC_ALL")
的输出来验证。如果您看到报告的 C
语言环境,那么您可能需要使用类似以下内容的 UTF-8 语言环境:
Sys.setlocale("LC_ALL", "en_US.UTF-8")
根据您想要的语言替换特定的 UTF-8 语言环境风格。 (作为参考,通常可以从终端使用 locale -a
之类的内容查询可用语言环境集)。