R command line differs from the Unix shell
When you start working in the R environment, it looks quite similar to a Unix shell. In fact, some R command-line actions behave as you'd expect if you come from a Unix environment, but others don't.
Want to cycle through your last few commands? The up arrow works in R just as it does in Unix -- keep hitting it to see prior commands.
The list function,
ls(), will give you a list, but not of files as in Unix. Rather, it will provide a list of objects in your current R session.
Want to see your current working directory?
pwd just throws an error; what you want is
rm(my_variable) will delete a variable from your current session.
R does include a Unix-like
grep() function. For more on using
grep in R, see this brief writeup on Regular Expressions with The R Language at regular-expressions.info.
Terminating your R expressions
R doesn't need semicolons to end a line of code (although it's possible to put multiple commands on a single line separated by semicolons, you don't see that very often). Instead, R uses line breaks (new line characters) to determine when an expression has ended.
What if you want one expression to go across multiple lines? The R interpreter tries to guess if you mean for it to continue to the next line: If you obviously haven't finished a command on one line, it will assume you want to continue instead of throwing an error. Open some parentheses without closing them, use an open quote without a closing one, or end a line with an operator like + or - and R will wait to execute your command until it comes across the expected closing character and the command otherwise looks finished.
Syntax cheating: Run SQL queries in R
If you have SQL experience and R syntax starts giving you a headache -- especially when you're trying to figure out how to get a subset of data with proper R syntax -- you might start longing for the ability to run a quick
SQL SELECT command query your data set.
The add-on package
sqldf lets you run SQL queries on an R data frame (there are separate packages allowing you to connect R with a local database). Install and load
sqldf, and then you can issue commands such as:
sqldf("select * from mtcars where mpg > 20 order by mpg desc")
This will find all rows in the
mtcars sample data frame that have an MPG greater than 20, ordered from highest to lowest MPG.