Repository navigation
R Intro II: Working with scripts
Bioinformatics Core Facility Gothenburg edited this page Sep 4, 2017
·
2 revisions
- Open a new file and save it in a suitable location. It is a good idea to save it using an
.Rending, so you know that it is R code in the file. Also, using spaces in file names may cause trouble, so try to use_instead. Do as much work as possible in this file, so that you can go back and see what you did.- You can use
#to write comments in your code, so that it is easier to read or to remember why you did what you did. Anything written on a line after#will be disregarded when running the code.
- You can use
- Type the same expression as before into your new file. You can run a line by putting your marker on the line (by clicking it), and then either click the “Run” button in the top left corner of the editor, or press Ctrl+R.
- Save the expression into a variable called
my_exprusing the->command and run the code again
- Save the expression into a variable called
- On a new line use the
print()function to print the value of your new variable (place the variable name inside the parentheses and the function will understand that this is what you want to print).- When working from the editor simply typing the variable name on a row will print it, but if you run the entire script from another location you need the
print()function. - What happens if you put citation marks around the name of the variable in the
print()function?
- When working from the editor simply typing the variable name on a row will print it, but if you run the entire script from another location you need the
- Run the entire script. You can do this by
-
marking all code (using the pointer or Ctrl+A) and press Ctrl+R, or
-
marking all code and press the “Run” button, or
-
pressing the “Source” button in the top right of the editor, which runs the entire script
-
Using the
source()function, using the file including its directory as argument, e.g:source("C:/My_R_Scripts/My_script.R")This is the case where the
print()function will be needed (without it nothing will be printed in the console).
-