Hi!
This week I finally made it through cleaning my DNA. To simplify a bit, the process involves using teeny tiny magnetic beads and a big magnet to "fish out" the DNA (since it's charged) from the solution full of unwanted molecules and transfer it to clean tubes. It’s super cool but also pretty stressful, because you have to work quickly. If you don’t, the DNA dries out, and we definitely don’t want that. It’s like leaving a bowl of noodles out overnight — by the next day, they’re all sad, stuck together, and they definitely don’t look good 🍜.
I really try not to think about my DNA all the time, but I can’t help it. It became my DNA, even though it isn’t my DNA — it’s bacterial — but I still feel responsible for it. Well, I am responsible for it. I need to keep it more or less intact or months of hard work will go to waste! And I’m really curious to see what’s inside my samples. Will there be lots of things I haven’t expected? Or will it be just a boring mix? It’s my source of pain, my source of joy.
This reminds me of when we grew sweet pea plants in primary school. When the sprout was about a dozen centimetres tall, we were supposed to take it home and care for it over the next few months. I tried so hard to bring mine safe and sound to my parents’ garden. I almost made it to the garden shed. Almost. I was so focused on holding the precious plant that I didn’t notice a big stone hidden in the grass. I tripped, and in an instant, that was the end of my little sweet pea plant 🥲.
I think about that every time I have to move my samples from the fridge to my bench in the lab. One unguarded moment, and I could be looking at weeks of hard work melting on the floor.
I'll use my recent workload as an excuse to keep this piece short and sweet. So today, you have just one thing to learn. But it's an important one!
For a long time, I didn’t understand how running a single script could trigger an entire cascade of tasks in the background. Then I discovered the 🌟source()🌟 function and it made my life much neater. From that day on, I could divide my lengthy scripts into smaller ones focused on specific tasks (like plotting and saving figures, for example). It’s a bit like being able to create multiple tabs in an Excel spreadsheet — imagine having to do everything in one tab!
As always you can start by grabbing the dataset here.
In RStudio, open a new script and type:
### Loading the data ###
library(readr)
blue_tits <- read_csv("../data/blue_tits.csv") # Remember to use the correct file pathway
### Instructions for creating the plot ###
plot(blue_tits$Laying_day, blue_tits$Hatching_day,
xlab = "Laying Day (where 1 is the 1st day of April)",
ylab = "Hatching Day (where 1 is the 1st day of April)",
col = "blue", # Change the color of points to blue
pch = 8, # Data points as stars
cex = 1, # Set point size
xlim = c(0,35), # Set up x-axis limits
ylim = c(15,60), # Set up y-axis limits
bty = "l") # Use an L-shaped box as a frame
### Messages ###
print("I just created a plot for you.")
message <- "Do you like it?"
Since we didn’t put much effort into creating an appealing plot last week, I thought this time we could experiment a little. I wouldn’t normally use blue stars as data points, but hey, now’s the time to try it out!
Save this script as "plotting.R" in your code folder, and then close it.
Next, open a completely new R script, name it something like "main_script.R" and type:
source("plotting.R") # Again, use the correct file path. I keep all of my scripts in the same "code" folder
print(message)
If you're unsure about picking the correct file paths, code folders and directories, you can check out my previous piece.
Now, execute these two commands, one after the other.
Can you see the plot?
Can you see the full message?
Did you notice how the commands and variables saved in one script can be executed and displayed by running a completely different one?
Now, looking back at last week’s piece, can you modify the "plotting.R" script so it also saves the plot to make sure it isn’t lost after closing RStudio? I dare you to give it a try!
See you next week,
Aga
PS: Here is the survey in which you can tell me what R topic you find particularly confusing and why you want to learn it so that we can shape this space together!