Hemken / Statamarkdown

Functions to write Stata documentation with knitr
Other
59 stars 11 forks source link

collectcode=TRUE not working to vinculate two code chunks #29

Closed aito123 closed 1 year ago

aito123 commented 2 years ago

Hello, just discover this package to integrate Stata into my R workflows.

I confirm every stata chunk works fine independently, but doesn't remember the previous stata chunk.

Syntax:

Setup


pacman::p_load_gh("hemken/Statamarkdown")
knitr::opts_chunk$set(
  engine.path = list(stata = "C:/Program Files/Stata-MP-16.0/StataMP-64"),
  error=TRUE, 
  cleanlog=TRUE, 
  comment=NA
  )#where is stata

Stata Markdown

sysuse auto
summarize

(1978 Automobile Data) Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- make | 0 price | 74 6165.257 2949.496 3291 15906 mpg | 74 21.2973 5.785503 12 41 rep78 | 69 3.405797 .9899323 1 5 headroom | 74 2.993243 .8459948 1.5 5 -------------+--------------------------------------------------------- trunk | 74 13.75676 4.277404 5 23 weight | 74 3019.459 777.1936 1760 4840 length | 74 187.9324 22.26634 142 233 turn | 74 39.64865 4.399354 31 51 displacement | 74 197.2973 91.83722 79 425 -------------+--------------------------------------------------------- gear_ratio | 74 3.014865 .4562871 2.19 3.89 foreign | 74 .2972973 .4601885 0 1

Frequency Tables


tab1 foreign 

no variables defined r(111);

end of do-file r(111);



When i see the root directory of my project, i identify when i run the first stata chunk some files are created and then deleted. Why is this happening? i tried executing my Stata as administrator and doesn't work either. I have Stata 16.

Let me know if there is solution to my problem.

Thanks,
Santiago

Ps. please read it in raw code because the formatting is not helping.
remlapmot commented 2 years ago

It looks like you simply need the chunk option collectcode=TRUE set on the Stata chunk in which you read in the data, i.e.,

---
title: "Example"
output: html_document
---

```{r, setup}
# install.packages("remotes")
remotes::install_github("Hemken/Statamarkdown")
library(Statamarkdown)
sysuse auto
summarize
tab1 foreign
aito123 commented 2 years ago

Thanks for the answer @remlapmot ,

I run the code and now i'm having a different problem:

---
title: "Example"
output: html_document
---

```{r, setup}
# install.packages("remotes")
remotes::install_github("Hemken/Statamarkdown")
library(Statamarkdown)

Using github PAT from envvar GITHUB_TOKEN Skipping install of 'Statamarkdown' from a github remote, the SHA1 (d9cbb1a1) has not changed since last install. Use force = TRUE to force installation No Stata executable found. No Stata executable found. No Stata executable found. No Stata executable found. The 'stata' engine is ready to use.

sysuse auto
summarize

Error in running comma

tab1 foreign

r(111);

end of do-file r(111);



Could be a problem with the location of my Stata.exe program?

Let me know thanks for the help

S
remlapmot commented 2 years ago

That looks like that's the problem. I'd start by adding your engine.path option back in, then adding the others as required.

---
title: "Example"
output: html_document
---

```{r, setup}
knitr::opts_chunk$set(
  engine.path = list(stata = "C:/Program Files/Stata-MP-16.0/StataMP-64")
)
# install.packages("remotes")
remotes::install_github("Hemken/Statamarkdown")
library(Statamarkdown)
sysuse auto
summarize
tab1 foreign
aito123 commented 2 years ago

Hello @remlapmot thanks for the response. Running the following code gave me a Error: node stack overflow so i restart R session and i'm again in the error of end of do-file not recognizing collectcode. I even force=TRUE the installation from github of the package to have the latest version.

---
title: "Example"
output: html_document
---

```{r, setup}
knitr::opts_chunk$set(
  engine.path = list(stata = "C:/Program Files/Stata-MP-16.0/StataMP-64")
)
# install.packages("remotes")
remotes::install_github("Hemken/Statamarkdown", force = TRUE)
library(Statamarkdown)
sysuse auto
summarize

Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- make | 0 price | 74 6165.257 2949.496 3291 15906 mpg | 74 21.2973 5.785503 12 41 rep78 | 69 3.405797 .9899323 1 5 headroom | 74 2.993243 .8459948 1.5 5 -------------+--------------------------------------------------------- trunk | 74 13.75676 4.277404 5 23 weight | 74 3019.459 777.1936 1760 4840 length | 74 187.9324 22.26634 142 233 turn | 74 39.64865 4.399354 31 51 displacement | 74 197.2973 91.83722 79 425 -------------+--------------------------------------------------------- gear_ratio | 74 3.014865 .4562871 2.19 3.89 foreign | 74 .2972973 .4601885 0 1

tab1 foreign

r(111);

end of do-file r(111);


Here i put my sessionInfo for more information. Let me know if you can spot the problem, if not no worries. Thanks.

R version 4.2.0 (2022-04-22 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale: [1] LC_COLLATE=Spanish_Peru.utf8 LC_CTYPE=Spanish_Peru.utf8 LC_MONETARY=Spanish_Peru.utf8 LC_NUMERIC=C
[5] LC_TIME=Spanish_Peru.utf8

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

other attached packages: [1] Statamarkdown_0.7.1

loaded via a namespace (and not attached): [1] ps_1.7.1 prettyunits_1.1.1 crayon_1.5.1 withr_2.5.0 rprojroot_2.0.3 R6_2.5.1 cli_3.3.0
[8] curl_4.3.2 rstudioapi_0.13 remotes_2.4.2 callr_3.7.0 tools_4.2.0 xfun_0.31 compiler_4.2.0
[15] processx_3.6.1 pkgbuild_1.3.1 knitr_1.39

nalsalam commented 2 years ago

I also am having the problem that profile.do is being created but not executed.

I also see that Stata is starting in an odd place -- another project I worked on days before.

library(Statamarkdown)
stataexe <- "C:/Program Files (x86)/Stata14/Stata-64.exe"
knitr::opts_chunk$set(
  engine.path=list(stata=stataexe),
  error = TRUE,
  cleanlog = FALSE
  )
cd
cd "U:\Projects\sandbox\R_and_Stata"
sysuse auto
gen gpm = 1 / mpg
summarize price gpm
cd
dir *.do
cd "U:\Projects\sandbox\R_and_Stata"
dir *.do
type profile.do
d,s
reg price gpm

Output from code chunk second-stata

.D:\Nabeel\Projects\minimum_wage_model_single_option_with_output_for_budget_anal

ysts

. dir *.do 0.1k 7/14/22 9:54 statabec40a2ac6.do

. cd "U:\Projects\sandbox\R_and_Stata" U:\Projects\sandbox\R_and_Stata

. dir *.do 0.0k 7/14/22 7:44 auto.do
0.1k 7/14/22 9:54 profile.do

. type profile.do cd cd "U:\Projects\sandbox\R_and_Stata" sysuse auto gen gpm = 1 / mpg summarize price gpm

. d,s

Contains data obs: 0
vars: 0
size: 0
Sorted by:

. reg price gpm no variables defined r(111);

end of do-file

remlapmot commented 2 years ago

These look like different errors to me.

@aito123 could you try adding the .exe onto the end of the string for your Stata executable (or edit what I have put so it's correct for where it is on your computer). And setting that after the library() call, i.e.,

---
title: "Example"
output: html_document
---

```{r, setup}
# install.packages("remotes")
remotes::install_github("Hemken/Statamarkdown")
library(Statamarkdown)
knitr::opts_chunk$set(
  engine.path = list(stata = "C:/Program Files/Stata-MP-16.0/StataMP-64.exe")
)
sysuse auto
summarize
tab1 foreign


The other solution is to install your Stata at the filepath the installer uses, which for Stata 16 is `C:/Program Files/Stata16/StataMP-64.exe`, because then the package should find it without you having to set `engine.path`.
remlapmot commented 2 years ago

@nalsalam I am wondering whether this is to do with the different drives you are using. Is it 2 (C: and U:) or 3 (C:, D:, and U:)?

Could you try with everything on the same drive? Or at least the .Rmd file and dataset being on same drive?

Remember you can show Rmd syntax on GitHub using 4 backticks (as then the 3 backtick chunks don't get mangled) e.g.

````rmd
---
title: "Example"
output: html_document
---
...rest of Rmd file...
nalsalam commented 2 years ago

@remlapmot I solved the problem using RStudio: Tools, Global Options, Rmarkdown, Evaluate chunks in directory: Document. Alternatively, if the .Rmd is in the root project directory one could have RStudio evaluate chunks in the Project directory. It seems that RStudio option controls the current directory for Stata where it looks for profile.do and where Stata_markdown saves profile.do.

I am using Rstudio 2021.09.1 Build 372

remlapmot commented 2 years ago

Thanks for reporting that @nalsalam. I guess that you might have changed this sometime in the past as Document should be the default.

Looks like you can also set this explicitly using the root.dir knitr option in the setup chunk as described here.

Hemken commented 2 years ago

Thanks Nabeel and Tom.

Just FYI I am out of the office traveling for the month of July.

From: Tom Palmer @.> Sent: Monday, July 18, 2022 9:16 AM To: Hemken/Statamarkdown @.> Cc: Subscribed @.***> Subject: Re: [Hemken/Statamarkdown] collectcode=TRUE not working to vinculate two code chunks (Issue #29)

Thanks for reporting that @nalsalamhttps://github.com/nalsalam. I guess that you might have changed this sometime in the past as Document should be the default.

Looks like you can also set this explicitly using the root.dir knitr option in the setup chunk as described herehttps://bookdown.org/yihui/rmarkdown-cookbook/working-directory.html#working-directory.

— Reply to this email directly, view it on GitHubhttps://github.com/Hemken/Statamarkdown/issues/29#issuecomment-1187628062, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACYBME7JKF2DVO3WPREW3ELVUVYK7ANCNFSM53IRKPQQ. You are receiving this because you are subscribed to this thread.Message ID: @.***>

aito123 commented 1 year ago

Hello there! Trying to keep up with this thread I just wanted to say that I've had some improvement in the matter.

It's actually a problem when running individual chunks but not a problem when Rendering the whole document. For example:

---
title: "Statamarkdown"
author: "Santiago Sotelo"
format: html
---

## Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

```{r, setup}
#| message: false
# install.packages("remotes")
# remotes::install_github("Hemken/Statamarkdown")
library(Statamarkdown)

knitr::opts_chunk$set(error = TRUE,
                      cleanlog = TRUE,
                      comment = NA)
sysuse auto
summarize
tab1 foreign
graph box mpg, over(foreign)
graph export "boxplot.svg", replace

Boxplot

![image](https://user-images.githubusercontent.com/53672503/201431472-f64d98b2-720b-4b2f-9b9b-287b3bc51781.png)

This works!

So there is no problem when rendering the document to HTML. It seems the problem occur when you try to run each individual chunk in RStudio even with `collectcode=TRUE` activated. That's when the following error appears:

no variables defined r(111);

end of do-file r(111);



I mean is not that critical because in the end it works, but I suggest there is room for improvement in this aspect. 

Best wishes,
Santiago

Ps. my Global options are configure this way: _Evaluate chunks in directory: Document._
Hemken commented 1 year ago

Statamarkdown does not work in notebook mode, running one code chunk at a time.

If you prefer to work in notebook mode, you should look into using Jupyter Notebooks, which has official Stata support. See https://www.stata.com/new-in-stata/jupyter-notebooks/

-- Doug Hemken SSCC statistical consultant 4226i Social Science Bldg.

@.**@.> 608-262-4327