4.1 Good practices


The two main way of interacting with R are the console or script files.

I recommend using the editor to write, edit, annotate and SAVE your script for later use. This way, you will have a record of what you did. You can easily re-run the whole or parts of the script if need be; for example if you added some data to your data-file, or if you want to change settings for some analysis or plots.

The console can be useful to check some details of the data before writing code for the next analysis or plot (e.g. checking up on spread of data, row names or column names, if the format is ok and so on) or test a function.

I recommend annotating your script. To annotate, write “#” (square) followed by your comment. Everything behind the # in the same line will be understood by R as text and appear green in the R-studio interface. This way, you will not only have a record of what you did, but also why.

The editor (and console) in R-Studio have an auto-completion function build in, which can help during the script writing.

Another recommendation is to separate the original data (raw data) from intermediate datasets that you create e.g. for use in a particular analysis. In marine biology, those might be dataframes with CTD data, fluorescence data, wind data or others, created from many separate raw-data files. To separate raw data, you can for example create a “data/” directory within your working directory containing raw data, and a “data_output/” directory for intermediate datasets. Some also like to keep their plots separated, e.g. in a “figure_output/” directory within you projects working directory.

  Fant du det du lette etter? Did you find this helpful?
[Average: 0]