Terminology:The following is a list of terms used in this book:
- R Base | R Base
- R Studio | R Studio
- Syntax R | Syntax
- Interface | antarmuka
- Icon | ikon
- Window | jendela
- R-prompt | R-prompt
- R-script | R-script
- R-console | R-konsol
- R-code | R-kode
- Arguments | perintah tambahan
Part 2: Starting-RGetting excited yet? Now let’s try to run R. Open your applications list or window. The following figure shows a list of my apps on Mac OS. Look for the two R icons on the left. The left icon would be for R Studio, while the right one would run R Base.
2.1 Interface of R BaseTry to click the right R icon. Then this window will pop up.
R Base is the engine of R. It has a very simple interface in form of
console window. In the window, you would see the following symbol, which is called
You would type all of your R commands behind that prompt. Now try to type some codes below. Ends with
enterfor each command.
There you go. It is your first R code ever. You should remember this date. Mine was in April 2011.
- Then type this code. See what happens.
- Then this one. See what happens. The
mtcarsis a data frame that comes in the R installation by default as an example. There are several other installed data frames for exercise purposes.
Again, you must remember this date. It is your first R plot ever.
After entering the
plot()command you would see another window pops up containing your plot.
2.2 Interface of R StudioNow go back to your app window. It’s time to click the R Studio icon. You would see this window opens up.
As I mentioned in Part 1, R Studio is a integrated development environment (IDE) for R. There are four windows in the interface. The position is changeable according to your taste. On my system, the windows in clock-wise order are:
- R-script Window: you type your code here.
- R-console Window: here, you can also type you code and see the result immediately.
- R-environment Window : you can see your data frames, variables, objects, and other components you are using in your work. All of these components are loaded on your memory (RAM).
- R-files, plots, packages, help, viewer:
- tab “files”: you can see list of your files in the working directory.
- tab “plots”: it contains a history of your plots.
- tab “packages”: you can see the installed packages in your system.
- tab “help”: you can see the help file you need inhere.
- tab “viewer”: this tab is used to open a website. But you will rarely (or never) use this tab.
- Copy the above commands that you just typed in to the console window. Don’t forget to hit
enterfor each command. What happens? You would see the result immediately after you hit
- Now paste the same commands in to the script window. What happens? You would see that you can copy-paste the commands back and forth without having to run them.
- You can click the
Runbutton on the top toolbar.
- The other way is by pressing the following combination keys together:
- in MacOS: hit
- in Linux dan Windows: hit
- Try it. Then see the results in the console window.
2.2 R syntaxLike other programming language, R also has a
syntaxor how to write the code. As you type the above codes, you have learned the syntax indirectly. So here are some notes:
- Type the commands after the prompt symbol
- What do you type? You have typed a simple calculation and asked R to calculate the result. Or, you can type
functions. So what are they?
Is that statement enough?
"Could you buy coffee for me".
It’s enough, if you drink any kind of coffee. There would be many variations of result that you are going to get. You can get a cup of coffee, or you can get a sachet of coffee.
So what is the solution?
You have to give specific supplementary instructions. In R this is called
arguments. So your command could be something like this:
Your command is specific this time, that can be listed as follow:
Could you buy me a cup of black coffee at the near by coffee stall, medium size, with there sachets of sugar.
- Your command =
- Your arguments :
- object =
- location =
near by stall
- quantity =
- sugar =
yes, 3 sachets
> buy(beverage, type = c(coffee, black, medium), loc = "stall", sugar = "TRUE")
If you don’t know how to write a command, just type
> ?. In this case, you would type.
Then you would see the help window shows the following documentation of the (imaginary) command:
By looking at the documentation, you can learn how to write the code and ask R to do your analysis. In the next part we will learn more about basic R commands. This time we will play around with the real R commands.
buy Description A generic function to buy coffee. For more details about the graphical parameter arguments, see the following explanation. Usage buy(class, type=c(..., ..., ...), loc="...", sugar="...") Arguments class: classification of your purchase. beverage cleaning type: the specification of your product coffee for coffee products cleaning for cleaning products black for black coffee flatwhite for flatwhite coffee tall for tall size medium for medium size small for small size fluid for fluid cleaning products bar for bar cleaning products loc: the location/lokasi barang yang anda perlukan stall mall sugar: TRUE with sugar FALSE without sugar