如何使用RStudio用Stata命令编写RMarkdown文件?

时间:2015-09-15 03:55:27

标签: rstudio stata r-markdown

标题中解释了我的问题。我已经尝试编译一个示例。我在这里找到了。http://www.ssc.wisc.edu/~hemken/Stataworkshops/Stata%20and%20R%20Markdown/ 除了在网上查找各种资源无济于事。虽然我的资源Doug能够编译RMarkdown,但我收到的MWE错误如下。

MWE是:

---
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
```

RStudio控制台的输出/错误是:

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 个答案:

答案 0 :(得分:3)

您链接的页面指向this page。在那里写道:“例如,如果Stata安装在/ Applications / Stata /中,那么Stata可执行文件的路径是/Applications/Stata/Stata.app/Contents/MacOS/”,它们提供了适当的链接各种不同口味的Stata。此外,您需要引用可执行文件,而不是它所在的文件夹。如果我更改你的MWE如下,一切都适合我(注意我使用Stata-SE;你可能需要为你的系统更改它。)

---
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
```