How to use RStudio terminals in your day-to-day data science projects?

rstudio
terminal

#1

Hi guys!

thanks to RStudio for the latest release - lot’s of really cool features came along! I really wanna use RStudio to it’s limits and I read this article about using R Studio terminal: https://support.rstudio.com/hc/en-us/articles/115010737148-Using-the-RStudio-Terminal, but I’m not sure If I really understand how I could potentially benefit from this in my day-to-day work. I think I don’t really get the full picture since I don’t come from a programming background, but maybe some of you here could help me understand it better? Thanks!


#2

If you don’t currently use the command line, it’s unlikely that the terminal will help you very much. However, there are a few things that may be helpful for people that I can think of:

  • At a basic level, it allows someone who is “in the zone” of programming in RStudio to continue to work in there without having to switch to other windows. Could be especially useful for keyboard shortcut wizards.
  • As was pointed out by @jonathan, it allows you to potentially do some basic programming in other languages while using the command line interpreter for that language.
  • I’m not sure it will be a common use, but someone could use an editor like emacs within RStudio. I can’t think of why that would be useful for R programming, but for someone that has a good command line editor/IDE for their second-favorite language could use it alongside R.
  • If you don’t want to use a git GUI, you can perform necessary actions on your repository that are unsupported by RStudio natively from the RStudio window.
  • If you’re familiar with the command line, it can be a lot faster to do things like searching for a file and checking its size than the equivalent steps that it would take in RStudio
  • For those using RStudio Server, the terminal potentially provides the easiest route to a command line/shell for the server, particularly if ssh access is prevented by a firewall. This means that people can readily do things like update any OS packages (such as R), move files around, add a new database user, or manually check on external processes (such as a database) with top and kill them if needed.

Of course, there is another pretty critical use of the terminal that should vastly improve everyone’s data science abilities:


#3

Thanks for your answers @nick. I think in this case my question goes even wider to understand the benefits for that feature and is actually the following: “how can using the command line help me work better with R/ in R studio?”


#4

Since the answers could be oh so many things, might I recommend taking a gander at @sean’s awesome bookdown book The Unix Workbench?

If you’re a Mac person, homebrew is a great gateway drug to hanging out in Terminal. Basically, it’s an easy way to install and manage all sorts of things…


#5

@konradino When it comes to command line + data analysis, then this is also a great ressource
http://datascienceatthecommandline.com/ and should answer many of your questions.


#6

There are a couple situations I have loved it for so far:

  • Adding a new folder that I want to save an output in without having to switch to iTerm (or finder) or look up how to make them in R (since I somehow always forget).
  • Checking git status
  • @nick touched on this but using vim(or emacs) from it for quickly adding something to .gitignore

I second @mara’s recommendation of the Unix Workbench. It was super helpful to make me feel like I better understand what I am doing from the command line instead of that I’m just doing whatever a stack overflow answer said.


#7

One of the ways I use the terminal is to run make files that live in the same directory of my RStudio project. It’s so nice to be able to pop over to the terminal that launches in the same working directory to run something like make blahblah.


#8

@konradino, if you’re wondering why @mine’s suggestion is so gosh-darn helpful, check out the Automating data analysis pipelines section of STAT545!

The Test drive make activity is a fun place to start :+1:!


#9

I primarily work on remote servers running RStudio server. Previous versions of RStudio had the shell tool, but it was pretty limited in functionality so I ended up logging into RStudio server via the web interface and using ssh to log into the server from terminal. I no longer need to do that!


#10

This has made my life a lot easier, just through minimizing the number of things I need open and making Rstudio more of a ‘complete’ IDE.

90% of my use of the terminal is file management/organization and git. 10% is scripting other things.


#11

Thanks @mara - really helpfull!


#12

I’m planning to write a tutorial on this topic, but one of things I’ve started doing is making a command line interfaces for common actions for my R projects.

So for my dissertation, I have dissertate

image

Each command justs calls an R function. For example, I have clean_site()

#' delete current bookdown files
clean_site <- function(...) {
  rmarkdown::clean_site()
  if (file.exists("./dissertation.Rmd")) {
    message(" dissertation.Rmd")
    invisible(file.remove("./dissertation.Rmd"))
  }
}

Then, in the terminal, ./dissertate clean_site will run that function for me.

Here’s an example of one of the CLI scripts I’ve written. The basic idea is to clean up the command line arguments then invoke the function named in the command line arguments.


#13

Command line: the gateway drug for Make.


#14

A recent addition to my .bash_profile and .zshrc to make me less terrified of accidentally over-rming:

function trash {
    mv -v $@ ~/.Trash
}

so now

$ # make some junk
$ touch junk-{1..6}.txt
$ ls
#> junk-1.txt junk-2.txt junk-3.txt junk-4.txt junk-5.txt junk-6.txt
$ trash junk-1.txt junk-2.txt
#> junk-1.txt -> /Users/alistaire/.Trash/junk-1.txt
#> junk-2.txt -> /Users/alistaire/.Trash/junk-2.txt
$ trash junk-{3..4}.txt
#> junk-3.txt -> /Users/alistaire/.Trash/junk-3.txt
#> junk-4.txt -> /Users/alistaire/.Trash/junk-4.txt
$ trash *
#> junk-5.txt -> /Users/alistaire/.Trash/junk-5.txt
#> junk-6.txt -> /Users/alistaire/.Trash/junk-6.txt
$ # directories too
$ touch good-file.txt
$ mkdir junk-dir-{1..2}
$ touch junk-dir-1/junk-{6..7}.txt
$ tree
#> .
#> ├── good-file.txt
#> ├── junk-dir-1
#> │   ├── junk-6.txt
#> │   └── junk-7.txt
#> └── junk-dir-2
#> 
#> 2 directories, 3 files
$ trash junk*
#> junk-dir-1 -> /Users/alistaire/.Trash/junk-dir-1
#> junk-dir-2 -> /Users/alistaire/.Trash/junk-dir-2
$ ls
#> good-file.txt

Leave out the -v to make it silent like rm.