title: How to use Rscript command line tool to build a book in bookdown
body: <p>Because of the cross-reference feature, I am using <code>bookdown</code> instead of <code>rmarkdown</code> to generate dynamic reports. I have more than one reports to generate, so I drafted an <code>R</code> script to call <code>bookdown:render_book</code> in a <code>for</code> loop to generate all the reports. Unfortunately, it errored. For simplicity, I build one minimal example:</p>
  
  <p>The <code>bookdown</code> file to render (saved as <code>index.Rmd</code>):</p>
  
  <pre><code>---
  title: "test"
  output: html_document
  ---
  
  ```{r test}
  ext_var
  ```
  </code></pre>
  
  <p>The <code>build.R</code>:</p>
  
  <pre><code>ext_var &lt;- "test.html"
  bookdown::render_book("index.Rmd", output_file = ext_var) # it will call `rmarkdown::render`.
  </code></pre>
  
  <p>When using following commands: <code>Rscript build.R</code>, it will generate the following error messages:</p>
  
  <pre><code>$ Rscript build.R
  Error in rmarkdown::render(main, output_format, ..., clean = clean, envir = envir,  :
    'ext_var'
  Calls: &lt;Anonymous&gt; -&gt; render_cur_session -&gt; &lt;Anonymous&gt;
  Please delete _main.Rmd after you finish debugging the error.
  </code></pre>
  
  <p>Then I changed my <code>build.R</code> to use <code>rmarkdown::render</code> instead:</p>
  
  <pre><code>ext_var &lt;- "test.html"
  rmarkdown::render("index.Rmd", output_file = ext_var)
  </code></pre>
  
  <p>There won't be any error. So I guess there might be some issues to fix.</p>
  
  <p><code>sessionInfo()</code>:</p>
  
  <pre><code>R version 3.5.0 (2018-04-23)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  Running under: Windows &gt;= 8 x64 (build 9200)
  
  Matrix products: default
  
  locale:
  [1] LC_COLLATE=Chinese (Simplified)_China.936  LC_CTYPE=Chinese (Simplified)_China.936   
  [3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C                              
  [5] LC_TIME=Chinese (Simplified)_China.936    
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  other attached packages:
  [1] RevoUtils_11.0.0     RevoUtilsMath_11.0.0
  
  loaded via a namespace (and not attached):
   [1] Rcpp_0.12.17    bookdown_0.7    digest_0.6.15   rprojroot_1.3-2 backports_1.1.2
   [6] magrittr_1.5    evaluate_0.10.1 stringi_1.1.7   rstudioapi_0.7  rmarkdown_1.10 
  [11] tools_3.5.0     stringr_1.3.1   xfun_0.1        yaml_2.1.19     compiler_3.5.0 
  [16] htmltools_0.3.6 knitr_1.20  
  </code></pre>
  
  <p>Also filed <a href="https://github.com/rstudio/bookdown/issues/592" rel="nofollow noreferrer">an issue #592</a> at <code>rstudio/bookdown</code> repository.</p>