Lab: Intro to RStudio and Images
Lab Workspace
The link to join the RStudio Cloud workspace should have been mailed out.
Post Lab Survey
Muddiest Points
I’m wondering about the distinction between <- and = to assign a value to a variable. It seemed like maybe that was outside the scope of what we had time to cover today, and maybe it will become clear in the future as we work in R more, but I’m curious what the difference is and why you wouldn’t want to use = to assign a variable a value.
Here’s a post by Colin Fay about why we use <-
instead of =
: https://colinfay.me/r-assignment/
The TLDR: The original versions of R
couldn’t even use =
for assignment (it was used to check equivalence of objects). Nowadays, we mostly use it because of readablility.
For example, we can do this:
new_value = sqrt(x=4)
which takes a little more mental effort to read than:
new_value <- sqrt(x=4)
Honestly, the thing I have been struggling with most is importing/exporting/loading data/file paths etc.
Yes, it’s difficult to wrap your head around file paths. If you follow the convention for the projects, (having a data
folder), it makes it much easier to manage. If you can spare the disk space, it’s better to copy the images you need into a data
folder, so you can zip up the project folder and share it with people.
Jenny Bryan (who has thought about this a lot more than I have), has a great discussion about why organizing your project is good. Highly recommended: https://rstats.wtf/project-oriented-workflow.html
I also missed what the histogram notation meant (
xlim=c(1,0)
, etc)
Ah. So xlim
sets the limits of the x axis. Here, we’re setting our axis limits to 0 and 1.
When you are uploading your own images into your project, do they always have to be .tif files or are there other formats that are compatible?
If we look at the documentation for readImage()
:
library(EBImage)
?readImage
There’s info about the image types that EBImage
accepts: jpeg
.png
and .tiff
. So those are the 3 main image types.
I understand that print(image_1) displays several parameters about image_1. Does “frames.total” mean the total number of channels included in the image? What does “frames.render” mean?
library(EBImage)
?`Image-class`
This is from here: https://www.bioconductor.org/packages/release/bioc/vignettes/EBImage/inst/doc/EBImage-introduction.html
The “frames.total” and “frames.render” fields shown by the object summary correspond to the total number of frames contained in the image, and to the number of rendered frames. These numbers can be accessed using the function
numberOfFrames
by specifying thetype
argument.
So, frames.total
is the total number of frames in the image, while frames.render
is the total number of rendered image. This is different because
Is there a difference in the way R runs a code based on the spacing? for example starting on the next line
hist( x = image_1,
xlim = c(0,1)
)
vs
hist(x = image_1, xlim = c(0,1))
Is the spacing just to read it better later?
The short answer is yes, it’s really for readability. I try to follow the R Style Code guide, so the code is easier to understand. http://adv-r.had.co.nz/Style.html
There is a shortcut in R that tries to format your code to make it more legible. I can’t remember what it is, but I’ll update this when I find it.
If you’re interested in how R decides whether the next line a continuation of the previous one, this is the best one I have found so far (apologies, I hate linking to “Dummies”): https://www.dummies.com/programming/r/how-to-structure-your-code-in-r/