{"id":24319406,"url":"https://github.com/btskinner/stataknitr","last_synced_at":"2025-06-22T04:07:13.741Z","repository":{"id":84089715,"uuid":"37686724","full_name":"btskinner/stataknitr","owner":"btskinner","description":"Method for knitting Stata","archived":false,"fork":false,"pushed_at":"2016-05-11T21:28:35.000Z","size":205,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-22T04:07:07.707Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/btskinner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-06-18T21:42:16.000Z","updated_at":"2024-11-09T06:24:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"9ebfbeed-ce9d-4a2e-ac39-4c101403a250","html_url":"https://github.com/btskinner/stataknitr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/btskinner/stataknitr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/btskinner%2Fstataknitr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/btskinner%2Fstataknitr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/btskinner%2Fstataknitr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/btskinner%2Fstataknitr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/btskinner","download_url":"https://codeload.github.com/btskinner/stataknitr/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/btskinner%2Fstataknitr/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261233299,"owners_count":23128200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-17T15:33:02.582Z","updated_at":"2025-06-22T04:07:08.729Z","avatar_url":"https://github.com/btskinner.png","language":"HTML","readme":"Unlike many of the other programming languages that knitr supports, Stata doesn't work as well when trying to compile literate programming documents. Other discussions about this issue and potential solutions can be found [here](https://hopstat.wordpress.com/2014/01/11/stata-markdown-2/), [here](https://github.com/amarder/stata-tutorial), and [here](http://www.ssc.wisc.edu/~hemken/Stataworkshops/Stata%20and%20R%20Markdown/Statalinux.html). \n\nThe biggest problem of using Stata with knitr is that each chunk is treated as its own do file and run in independent Stata sessions through the command line. This means that changes aren't persistent across chunks and strange behaviors occur, assuming the document compiles at all. One can attempt to set up a `profile.do` file to help with persistence, but I had trouble with it.\n\n\nStataKnitr comes from my attempt to knit a Stata do file into an HTML file. My solution is to run the Stata do file using a bash chunk in knitr and then parse the resulting log file with an R helper function I wrote called `logparse()`. Images are aligned with another R function, `alignfigure()`. The resulting workflow isn't as smooth as one might desire, but it works well for making a nice presentation of Stata code and output when that is required.\n\n## Requirements\n\nThis method requires:\n\n*  a do that can be run from top to bottom and is well commented with unique comments\n*  a log file of the session\n*  the ability to run Stata from the [command line (batch mode)](http://www.stata.com/support/faqs/unix/batch-mode/)\n*  [`rmarkdown`](http://cran.r-project.org/web/packages/rmarkdown/) package in R\n*  `stataknitrhelper.r` be accessible by the rmarkdown file (the default is to have it in the same directory)\n\n\u003e ##### NOTE\n\u003e I have an OS X system, so I suspect it will work on other \\*NIX systems. I'm not sure about Windows.\n\n## Set up\n\n### Construct your stata do file \n\nIt must run from top to bottom without error and save a log of the output. Also, every section that you want displayed should have a unique comment. `demo.do` is an example:\n\n```\n// log stata session with *.log file\nlog using \"demo.log\", replace\n\n// load data\nwebuse school, clear\n\n// create nominal income variable; summarize\ngen inc = exp(loginc)\nsummarize inc\n\n// list observations inc\nlist obs inc\n\n// close log and exit\nlog close                               \nexit\n```\n\n### Construct your knitr rmd file  \n  \n#### Call do file  \n\nAfter setting any knitr chunk options that you want, call your do file with a knitr chunk that uses `engine = 'bash'` in the options:\n\n```r\n## run accompanying .do file to get log file for parsing   \nstata -b -q do demo.do\n```\n\n\u003e ##### NOTE\n\u003e Your command line Stata call may be different from `stata`. If you have StataSE or StataMP and you haven't created a symlink using `stata`, you may need to call the command with `stata-se` or `stata-mp`.\n\n#### Store log file\n\nStore the log file in an object in the next knitr chunk:\n\n```r\n## save log file in object\nlf \u003c- 'demo.log'\n```\n\n#### Parse log\n\nTo get the chunk you want, use `logparse()` wraped with `writeLines()` to grab the relevant part of the log file and print to output. The R function will take line numbers, but it's easiest to use with unique comments. If the comments aren't unique, the function should still run, but with undesired output:\n\n```r\nstart \u003c- 'load data'\nend \u003c- 'create nominal income variable; summarize'\nwriteLines(logparse(lf, start = start, end = end))\n```\n\n\u003e ##### NOTE\n\u003e The default option for `logparse()` is to drop the first and last lines when printing the pulled section. This is so the first and last comment aren't printed to the console when the comments are placed above, rather than inline with, the relevant code. If you use inline comments or want to show the first and last lines, set the `logparse` option `include = TRUE`.\n\n## Set up with graphics\n\nWhen called from the command line, Stata can only produce graphics in `*.ps` and `*.eps` format. I couldn't get rmarkdown to work with these files. To work around this, I include code at the beginning of the knitr document (in the BASH chunk just below the `stata ... ` call) that uses [ImageMagick](http://www.imagemagick.org/script/index.php) to convert the EPS files to PNG:\n\n```bash\n## convert plots used in this file to png\nplotlist=(*.eps)\nfor i in ${plotlist[@]};\ndo\nbase=${i%.eps}\nconvert -density $dpi -flatten $base.eps $base.png;\ndone\n```\n\nThis code finds all EPS files in the directory and converts them to PNG files with the same name. For it to work, the YAML frontmatter needs to include the `params:dpi` option and the following line in the first knitr setup chunk:\n\n```r\n## set yaml params in environment\nSys.setenv(dpi = params$dpi)\n```\n\n\nTo get files of different size, change the current density value of `150` to whatever you want in the `params` option of `rmarkdown::render()` (see below).\n\n### Do file setup for plots\n\nMake sure your do file exports images using the `graph export` command:\n\n```\n// create scatter of years by loginc; export to file\nscatter years loginc, name(sc_yearsXloginc)\ngraph export \"sc_yearsXloginc.eps\", name(sc_yearsXloginc) replace\n```\n\n### Call plot in document\n\nCall the plot in the rmarkdown document either with the standard `![]` command or, if you want to align it, using `alignfigure()` wrapped in `writeLines()` and using the `results = 'asis'` chunk option. Here is an example using the `demo_plots.rmd`:\n\n```r\nwriteLines(alignfigure('sc_yearsXloginc.png', 'center'))\n```\n\n## Run\n\nTo run, open an R session in the same directory as your RMD file and run `rmarkdown::render()`. Here's an example using `demo.rmd`:\n\n```r\nrmarkdown::render('demo.rmd')\n```\n\nIf you want to change the size of the image, you need to pass the change in the `render` options:\n\n```r\nrmarkdown::render('demo.rmd', params = list(dpi = 300))\n```\n\nSee the files in `demo_html` for examples HTML output.\n\n## Styling options\n\nBecause Stata, unlike R, repeats the command in the output, I didn't want to echo the command as well. This means, by default, all the output code chunks were the same color as the backgroud. To add contrast, I change the CSS with the `custom.css` file. You can change as you like or remove the call from the YAML to use the default settings.\n\n\n\n\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbtskinner%2Fstataknitr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbtskinner%2Fstataknitr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbtskinner%2Fstataknitr/lists"}