Showing posts with label syntax. Show all posts
Showing posts with label syntax. Show all posts

Wednesday, April 30, 2014

Three Ways to Format R Code for Blogger

Unformatted Code




If you are like me originally then you might not think it is worth it to spend the extra energy to format your code.  After all people can just copy what you have and paste it into their preferred editor which will do its own formatting, no sweat.

Well, this might be true for some code in some languages, but in R it really is not gong to fly.  The problem is the ubiquitous <- which is very easily confused with html tags which take the form <tag>. Thus, you are going to be looking for a way to make your code not be buggy when you post it.  One way might be to find and replace all < with &lt; and > with &gt; using a text editor but that is already a fair amount of trouble especially if you actually want to have some HTML tags in your post.  Besides, if you are going to all of that trouble, you might as well as well actually format your posts with syntax highlighting since it will be that much more enjoyable for readers.

Here are three simple ways:

1. Export Code through Notepad++
Exporting through Notepad++ is easy and embeds a css style sheet within the HTML document which makes it easy to edit styles after export.  There is also a fair amount of editing possible before export as well.
  1. Open myscript.R file
  2. Configure appearance of formatting with: Settings>Style>R to your taste
  3. Export HTML: Plugins>NPPExport>Export to HTML
  4. Open saved myscript.html as a text file.
  5. Copy and paste HTML into new post HTML in blogger. 

2. Format with Pretty-R
Pretty-R is useful because it is very simple to use. It also provides links from highlighted function to associated R documentation.  However, the style is fixed and difficult to modify before or after export.
  1. Go to http://www.inside-r.org/pretty-r/tool
  2. Paste your R script
  3. Copy HTML code in the HTML box and paste into new post HTML in blogger.

3. Format with knittr
knittr is a pretty amazing package which turns R markdown files into HTML files.  There is a lot of customization possible with it and it is under active development.  I have only recently started looking into it though it seems to me to be the way to go forward. In addition, RStudio supports knittr markdown formatting.  However, it does take a few extra steps to set up but ultimately gives you more control over your final output.
  1. Open RStudio
  2. Select File>New>R Markdown
  3. Paste your code between ```{r} and ``` and structure appropriately.
  4. Specify formatting of your posts as you see fit. There are a lot of great options.  Images will be embedded directly into the HTML document.  This is a pretty amazing feature since it means your file will not require any external dependencies.
  5. Click Knit HTML. This should create an HTML document identical of the same name as the Rmd file.
  6. Edit the generated HTML file.
  7. You may need to remove the ccs classes h1 through h6 as they can interfere with your blogger theme's css classes of the same name.  You may also need to remove the markdown titled identified as <h1> (around line 150-200) if you are going to input your post title in the default blogger title position (which is recommended).
  8. Copy you edited HTML code into blogger

(Note: I know step 7 is a bit hacky so I have asked the question on how to change the default css styles with knitr so that they are not interfering with blogger on stackoverflow.  Please take a look at the question as I hope someone will be able to answer it soon and greatly simply this process.)
Flattr this

Tuesday, August 27, 2013

Rstylizer - Shiny, Stata HTML Syntax Highlighter

I have released a shiny app through the Rstudio Spark server that allows for users to copy and paste Stata code into the app and it returns html formatted code.  The syntax structure is very easy to modify and I am looking forward to updating it when I have time, permitting extensions of the app to other languages as well as allowing users to customize the particular aspects of of the formatting such as making some commands bold or changing the color or how comments are identified.  In addition, I also hope to include an input field for users to add to language definitions, allowing for the creation of custom definitions.


[[The app can be found at: http://spark.rstudio.com/fsmart/RStylizer/]]

The git can be found at: https://github.com/EconometricsBySimulation/Rstylizer

In general the html generated is not very efficient since tags are repeated when css embedded styles is probably the preferred option.  However, since it makes very little difference in terms of load time, I figure this should work fine.  In addition, Blogger's gui seems to handle the tags better than embedded style sheets.

(I created an outdated git: https://github.com/EconometricsBySimulation/RFormatter as well but could not figure out how to change names so sorry for the messiness.)

Tuesday, January 29, 2013

Stata Syntax Command's Syntax

do file

* When programming Stata "programs" (loosely known as functions in R) it is often times very useful to use the "syntax" command to parse arguments.

* This command takes the arguments input into the command and uses them to control the function of the command.

* For example:

cap program drop example1
program define example1, rclass

  syntax [anything]
  * The [] tell stata that this argument is optional
  * The anything tells Stata that this argument can be of any form excluding a comma (,)

  di "`anything'"

  * The local function 1 returns the first argument in the command
  di "`1'"

  * While 2 returns the second
  di "`2'"

end

example1 hello world
example1 *
example1 2123 123213 hello world

* We can see the display changes as a result of how the arguments are structured

* Programmers can force the user to input only inputs of a particular structure.

* For instance:


cap program drop example2
program define example2, rclass

  syntax [varlist]
  * The [] tell stata that this argument is optional
  * The anything tells Stata that this argument can be of any form excluding a comma (,)

  di "`varlist'"

  * The local function 1 returns the first argument in the command
  di "`1'"

  * While 2 returns the second
  di "`2'"

  * Variables targetted in this manner can be modified:
  foreach v of varlist `varlist' {
    rename `v' r_`v'_rename
  }

end

clear

gen hello = "content of variable hello"
gen world = "content of variable world"

example2 hello world
* Which works because we created the variables hello and world

example2 hello world
* This does not work because the above command already renamed the variables hello and world

* There is a lot of trickiness involved in effectively programming the syntax command and unfortunately it can be very frustrating because the only feedback stata gives is "invalid syntax" when something goes wrong.

* Also, unfortunately stata does not have many actual coding examples in which syntax is displayed.

* However, I think the following example will go a long way to answering many questions.

* This is the start of a command that will simulate data and test different estimators on that data.

* It will also set default parameters if the user does not input parameters into the simulation directly.

* Before executing the command, the command will display to the user the parameter set to be estimated

cap program drop example3
program define example3, rclass

*
  syntax [anything] [, NGrp(numlist >0 integer) NInd(numlist >0 integer) ///
                    Rgrp(numlist integer) Udist(string) NEXogenous NENdogenous]
  * The first few letters being capital allows the user to abreviate the option to just those two or three letters.

  * Set defaults if the user has not defined any.
  if "`ngrp'"==""   local ngrp=50
  if "`nind'"==""   local nind=20
  if "`rgrp'"==""   local rgrp=0
  if "`udist'"==""  local udist="rnormal()*5"

  * This sets the local parameter exogenous to be equal to 1 by default unless the user specifies ontherwise
  local exogenous=1
  if "`nexogenous'"!="" local exogenous=0

  local endogenous=1
  if "`nendogenous'"!="" local endogenous=0

  di as text _newline "Simulation Paramaters"
  di "Number of groups: `ngrp'"
  di "Average number of individuals in each group: `nind'"
  di "Group size random (0 no, 1 yes): `rgrp'"
  di "Error distribution: `udist'"

  di _newline _c "Models estimated: "
  if `exogenous' == 1 di _c "EXOGENOUS"
  if `endogenous' == 1 di " ENDOGENOUS"
  if  `exogenous' == 0 & `endogenous' == 0 di "No model estimated!"

  ********************

  * The actual simulation

  ********************

  * End of the command
end

  example3 , ngrp(454)
  example3 , ng(454)
  * See how the abreviation is possible

  example3 , nex  udist(runifrom()*3)
  example3 , nex  udist(runifrom()*3) r(1)

  example3 , nex nen

* It is very useful to check out the "help syntax" file.