2015-01-09 47 views
0

我在R中有這種奇怪的場景,其中rmarkdown :: render()函數意外執行。考慮下面的簡單腳本/ test.Rmd在rmarkdown :: render(rmarkdown R package)的output_dir參數中使用getwd()函數會產生意想不到的結果

--- 
title: test 
--- 

```{r test} 
plot(1:10) 
``` 

然後我有下列R-代碼:

> getwd() 
[1] "/projects/test_project" 
library('rmarkdown') 
rmarkdown::render("scripts/test.Rmd", output_file = 'test.html', output_dir = paste(getwd(), '/', 'reports', sep = '')) 

這結束了在這裏產生輸出:

/projects/test_project/scripts/reports/test.html 

這是不期而至我因爲:

> paste(getwd(), '/', 'reports', sep = '')) 
/projects/test_project/reports 

我希望在這裏生成test.html /projects/test_project/reports/test.html。有趣的是,當我放棄getwd()並將路徑用作字符串時:

> render("scripts/test.Rmd", output_file = 'test.html', output_dir = "/projects/test_project/reports") 

這將在預期位置生成文件。有什麼想法發生在這裏?

> sessionInfo() 
R version 3.1.2 (2014-10-31) 
Platform: x86_64-apple-darwin14.1.0 (64-bit) 

locale: 
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8 

attached base packages: 
[1] splines grid  stats  graphics grDevices utils  datasets methods base 

other attached packages: 
[1] rmarkdown_0.4.2 scales_0.2.4  broom_0.3.5  data.table_1.9.4 gplots_2.15.0  RColorBrewer_1.1-2 reshape2_1.4.1  dplyr_0.3.0.2  ggplot2_1.0.0 
[10] stringr_0.6.2  plyr_1.8.1   survival_2.37-7 xtable_1.7-4  fields_7.1   maps_2.3-9   spam_1.0-1   knitr_1.8   argparse_1.0.1 
[19] proto_0.3-10  vimcom_1.0-0  setwidth_1.0-3  colorout_1.1-0 

loaded via a namespace (and not attached): 
[1] assertthat_0.1  bitops_1.0-6  caTools_1.17.1  chron_2.3-45  colorspace_1.2-4 DBI_0.3.1   digest_0.6.8  evaluate_0.5.5  findpython_1.0.1 
[10] formatR_1.0  gdata_2.13.3  getopt_1.20.0  gtable_0.1.2  gtools_3.4.1  htmltools_0.2.6 KernSmooth_2.23-13 lazyeval_0.1.10 magrittr_1.5 
[19] MASS_7.3-35  munsell_0.4.2  parallel_3.1.2  psych_1.4.8.11  Rcpp_0.11.3  rjson_0.2.15  tidyr_0.2.0  tools_3.1.2  yaml_2.1.13 

回答

1

我面臨着同樣的問題,覺得這可能是因爲在render()功能的小BUG。我提出一個問題,它的GitHub的網頁上: https://github.com/rstudio/rmarkdown/issues/416

作爲一種解決方法/黑客,我使用:

require(rmarkdown) 
fl = render("awesome.Rmd") 
file.copy(fl, getwd()) 

希望有所幫助。

相關問題