How to use RStudio to write a RMarkdown file with

2020-06-21 06:12发布

问题:

My problem is explained in the title. I've tried compiling a sample .Rmd I found here: http://www.ssc.wisc.edu/~hemken/Stataworkshops/Stata%20and%20R%20Markdown/ in addition to looking up various resources online to no avail. While my resource, Doug, was able to compile an RMarkdown, I am getting an error for the MWE below.

The MWE is:

---
title: "Stata and R Markdown (Windows)"
author: "Doug Hemken"
date: "July 2015"
output: 
html_document:
toc: yes
---

```{r, echo=FALSE, message=FALSE}
require(knitr)
statapath <- "/Applications/Stata/Stata.app"
opts_chunk$set(engine="stata", engine.path=statapath, comment="")
```

### Descriptive Statistics
A simple example.
```{r}
sysuse auto
summarize
```

The output/error from the RStudio console is:

processing file: stata.Rmd
  |................                                                 |  25%
   ordinary text without R code

  |................................                                 |  50%
label: unnamed-chunk-1 (with options) 
List of 2
$ echo   : logi FALSE
    $ message: logi FALSE

Loading required package: knitr
  |.................................................                |  75%
  ordinary text without R code

  |.................................................................| 100%
label: unnamed-chunk-2

running: /Applications/Stata/Stata.app  -q -b stata47b9d14e1c.do
Quitting from lines 20-22 (stata.Rmd) 
Error in engine(options) : 
  sh: /Applications/Stata/Stata.app: is a directory
Calls: <Anonymous> ... process_group.block -> call_block -> block_exec ->     in_dir -> engine
In addition: Warning message:
running command ''/Applications/Stata/Stata.app'  -q -b stata47b9d14e1c.do 2>&1' had status 126 
Execution halted

回答1:

The page you link to points to this page. There, it is written: "For example, if Stata is installed in /Applications/Stata/, then the path to the Stata executable is /Applications/Stata/Stata.app/Contents/MacOS/", and they give appropriate links for all different flavours of Stata. Also, you need to refer to the executable, not the folder where it is located. If I change your MWE as below, everything works for me (note that I use Stata-SE; you might need to change this for your system).

---
title: "Stata and R Markdown (Windows)"
author: "Doug Hemken"
date: "July 2015"
output: 
html_document:
toc: yes
---

```{r, echo=FALSE, message=FALSE}
require(knitr)
statapath <- "/Applications/Stata/StataSE.app/Contents/MacOS/stata-se"
opts_chunk$set(engine="stata", engine.path=statapath, comment="")
```

### Descriptive Statistics
A simple example.
```{r}
sysuse auto
summarize
```