Hello World

Introduction

This page follows the tradition of introducing a language by writing a hello world program. Here you'll learn how to write and compile a simple FAViR paper.

Saving, Sweaving, TeXing

First, copy and paste the following code into a new text file and save it as hello-world.Rnw. We'll explain what each line does later.

<<SourceR,results=hide,echo=FALSE>>=
library(favir)
InitPaper()
@ 

<<LatexPrelude,echo=FALSE,results=tex>>=
IncludePrelude("Hello World Title", "Random Actuary")
@ 

Hello, world!

\end{document}

This is our complete hello world program. There are two steps to compiling this paper into a PDF:

  1. Sweaving: This means running R on the Rnw file and executing any embedded R code. The output of this step is a latex (.tex) file. Sweave is a function of R that allows code and text to be interleaved in the same file.
  2. LaTeXing: This takes the output of the previous step and produces the PDF. LaTeX is a free document markup language. LaTeX is somewhat like HTML but is designed for non-interactive high-quality papers.

This process can be done from within R or on the command line. If you're familiar with a command-line interface you may wish to use option 2; otherwise stick to option 1.

Option 1: within R

Start R in the directory that contains your hello-world.Rnw file. Then at the R prompt run the following commands:

library(favir)
FavirSweave("hello-world")

Option 2: command-line

Change to the directory that contains your hello-world.Rnw file. Then run these two commands from the command-line:

R CMD Sweave hello-world.Rnw
pdflatex hello-world.tex

You may need to adjust your PATH to ensure that R and pdflatex can be found.

Back to the process

Either way, the first command should sweave your hello-world.Rnw file into a latex file called hello-world.tex. The final command should produce the finished PDF file called hello-world.pdf. Simply view it in your favorite PDF viewer, such as Foxit or Evince.

The PDF should look like this:

hello-world.pdf

If anything goes wrong, see Troubleshooting on the installation page.

Explanation by section

<<SourceR,results=hide,echo=FALSE>>=
library(favir)
InitPaper()
@

The section runs R code, but the results=hide,echo=FALSE directives mean that there is no tex output. Internally, this tells the R process to load the favir module and get ready to process a FAViR paper.

<<LatexPrelude,echo=FALSE,results=tex>>=
IncludePrelude("Hello World Title", "Random Actuary")
@

The section above does generate LaTeX output (due to the results=tex directive). The IncludePrelude(…) function outputs all the LaTeX necessary to make the title, specify the header and footer, draw the FAViR logo, etc. If you look at the hello-world.tex file that Sweave makes in a text editor, you can see everything that IncludePrelude did.

Hello, world!

\end{document}

Finally this code is not within a «»= … @ block, so it is interpreted directly as LaTeX code. The \end{document} is a standard command that LaTeX requires at the end of all documents.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License