March 3, 2020

Homework answers

Comment about homework format

Your file must be .R, not .Rhistory

Files with extension .Rhistory show all the commands you did, not only the “good” ones

It is fine to try and make mistakes when you explore a problem

The answer should contain only the good part

Show what is important, not what is not important

Example of .R file

#'title: "Homework 3"
#'subtitle: "Computing in Molecular Biology and Genetics II"
#'author: "İlker Şam"
#'number: 0405150449
#'date: "February 27, 2020"

# 1. Draw a pentagon
### DO NOT DELETE THIS ###
#+ q1
# write here
library(TurtleGraphics)
turtle_init(width = 480, height = 360, mode = "clip")
turtle_getpos()
turtle_up()
turtle_goto(200, 240)
turtle_setangle(90)
turtle_down()
turtle_hide()
turtle_col(col = "blue")
turtle_do({ 
  turtle_move(50)
  turtle_left(72)
  turtle_move(50)
  turtle_left(72)
  turtle_move(50)
  turtle_left(72)
  turtle_move(50)
  turtle_left(72)
  turtle_move(50)
  turtle_left(72)    
})
### DO NOT DELETE THIS ###

Good format

  • Used the provided file template
  • Wrote name and number in the correct place
  • Easy to understand

Not so good

library(TurtleGraphics)
install.packages("TurtleGraphics")
turtle_init(mode = "clip")
draw_polygon <- function(N, length, angle){
for (i in 1:N) {
  turtle_move(length)
  turtle_right(angle)
}
}

draw_polygon(10, 20, 36)
turtle_hide()

Some parts good, some not so

  • Good: better code, used for(){} loop
  • Bad: not used the template
  • Bad: not name, not number
  • Bad: install.packages("TurtleGraphics") should not be in the script

Bad answer format

library(TurtleGraphics)
turtle_init()
install.packages("TurtleGraphics")
library("TurtleGraphics")
turtle_init()
turtle_init(mode = "clip")
turtle_right(90)
turtle_move(60)
turtle_init
install.packages("TurtleGraphics")
install.packages("TurtleGraphics")
install.packages("TurtleGraphics")
library("TurtleGraphics")
turtle_init(mode = "clip")
turtle_right(90)
turtle_right(180)
turtle_move(20)
turtle_left(90)
turtle_move(20)
turtle_right(90)
turtle_right(180)
turtle_move(40)
turtle_move(10)
turtle_left(90)
turtle_move(20)
turtle_left(90)
turtle_move(300)
install.packages("TurtleGraphics")
library("TurtleGraphics")
install.packages("TurtleGraphics")
turtle_init()
install.packages("TurtleGraphics")
turtle_init(mode = "clip")
install.packages("TurtleGraphics")
library("TurtleGraphics")
turtle_init()
turtle_init(mode = "clip")
turtle_right(90) turtle_right(180)
> turtle_move(20)
> turtle_left(90)
> turtle_move(20)
> turtle_right(90)
> turtle_right(180)
> turtle_move(40)
> turtle_move(10)
> turtle_left(90)
> turtle_move(20)
> turtle_left(90)
turtle_left(90)
turtle_move(20)
turtle_left(90)
turtle_move(20)
turtle_left(90)
turtle_move(20)
turtle_move(30)
turtle_left(90)
turtle_move(20)
turtle_left(90)
turtle_move(30)
turtle_right(30)
turtle_move(20)
turtle_right(60)
turtle_left(30)
install.packages("TurtleGraphics")
install.packages("TurtleGraphics")
install.packages("TurtleGraphics")
library("TurtleGraphics")
turtle_init()
turtle_init(mode = "clip")
turtle_right(90) turtle_right(180)
> turtle_move(20)
> turtle_left(90)
> turtle_move(20)
> turtle_right(90)
> turtle_right(180)
> turtle_move(40)
> turtle_move(10)
> turtle_left(90)
> turtle_move(20)
> turtle_left(90)
turtle_left(90)
turtle_move(20)
turtle_left(90)
turtle_move(20)
turtle_left(90)
turtle_move(20)
turtle_left(90)
turtle_move(20)
turtle_right(90)
turtle_move(30)
turtle_right(90)
turtle_move(20)
turtle_right(90)
turtle_move(30)
turtle_setpos(50,50)
turtle_left(30)
turtle_right(30)
turtle_right(60)
turtle_move(20)
turtle_left(120)
turtle_move(20)
turtle_setpos(80,50)
turtle_right(30)
turtle_right(30)
turtle_right(60)
turtle_move(20)
turtle_left(60)
turtle_move(30)
turtle_setpos(60,40)
turtle_move(5)
turtle_right(90)
turtle_move(5)
turtle_right(90)
turtle_move(5)
turtle_right(90)
turtle_move(5)
turtle_setpos(75,40)
turtle_right(90)
turtle_move(5)
turtle_right(90)
turtle_move(5)
turtle_right(90)
turtle_move(5)
turtle_right(90)
turtle_move(5)
turtle_setpos(45,30)
turtle_right(180)
turtle_setpos(44,30)
turtle_move(10)
turtle_left(90)
turtle_move(8)
turtle_left(90)
turtle_move(10)
turtle_setpos(10,95)
turtle_setpos(10,90)
for (i in 1:5) {
turtle_move(10)
turtle_right(72)
}
turtle_setpos(25,90)
for (i in 1:5) {
turtle_move(10)
turtle_left(72)
}
turtle_setpos(15,15)
turtle_setpos(15,30)
turtle_left(180)
turtle_move(10)
turtle_left(90)
turtle_move(3)
turtle_right(90)
turtle_move(6)
turtle_right(90)
turtle_move(6)
turtle_right(90)
turtle_move(6)
turtle_right(90)
turtle_move(3)
turtle_setpos(15,30)
turtle_move(3)
turtle_setpos(15,30)
turtle_right(180)
turtle_move(3)
turtle_setpos(15,37)
turtle_right(45)
turtle_move(3)
turtle_setpos(15,37)
turtle_left(90)
turtle_left(180)
turtle_move(3)
turtle_hide()

We need scripts, not history

  • This last example was a .Rhistory
  • We do not know who sent it
  • We do not know what is important, and what is not
  • But we can see how the student tries different ideas

The code is right, the format is not

All these answers did the correct thing

The comment is not about the content, but about the format

It is important to follow the format now, since we will use it for the exam.

Let’s do one example

Draw a head

turtle_right(90)
turtle_forward(5)
turtle_left(90)
turtle_forward(10)
turtle_left(90)
turtle_forward(10)
turtle_left(90)
turtle_forward(10)
turtle_left(90)
turtle_forward(5)
turtle_right(90)

Arms

# neck
turtle_forward(7)
# arms
turtle_left(90)
turtle_forward(10)
turtle_right(180)
turtle_forward(20)
turtle_left(180)
turtle_forward(10)
turtle_right(90)

Draw the first leg

# body
turtle_forward(15)
turtle_right(40)
# first leg
turtle_forward(15)
turtle_left(90)
turtle_forward(5)
turtle_left(180)
turtle_forward(5)
turtle_right(90)
turtle_forward(15)

Draw the second leg

# direction of second leg
turtle_right(100)
# second leg
turtle_forward(15)
turtle_left(90)
turtle_forward(5)
turtle_hide()

Can we simplify it?

Replace repeated pattern by loop

turtle_right(90)
turtle_forward(5)
turtle_left(90)
turtle_forward(10)
turtle_left(90)
turtle_forward(10)
turtle_left(90)
turtle_forward(10)
turtle_left(90)
turtle_forward(5)
turtle_right(90)

 

turtle_right(90)
turtle_forward(5)
for(i in 1:3) {
    turtle_left(90)
    turtle_forward(10)
}
turtle_left(90)
turtle_forward(5)
turtle_right(90)

(use h key)

The size can be variable

turtle_right(90)
turtle_forward(5)
for(i in 1:3) {
    turtle_left(90)
    turtle_forward(10)
}
turtle_left(90)
turtle_forward(5)

Let’s say size=10 (for now)  

turtle_right(90)
turtle_forward(size/2)
for(i in 1:3) {
    turtle_left(90)
    turtle_forward(size)
}
turtle_left(90)
turtle_forward(size/2)

(use h key)

Simplify and generalize the arms

# neck
turtle_forward(7)
# arms
turtle_left(90)
turtle_forward(10)
turtle_right(180)
turtle_forward(20)
turtle_left(180)
turtle_forward(10)
turtle_left(90)

 

# neck
turtle_forward(size*7/10)
# arms
turtle_left(90)
turtle_forward(size)
turtle_right(180)
turtle_forward(size*2)
turtle_left(180)
turtle_forward(size)
turtle_left(90)

1st leg: Use variable size

# body
turtle_forward(15)
turtle_right(40)
# first leg
turtle_forward(15)
turtle_left(90)
turtle_forward(5)
turtle_left(180)
turtle_forward(5)
turtle_right(90)
turtle_forward(15)

 

# body
turtle_forward(size*15/10)
turtle_right(40)
# first leg
turtle_forward(size*3/2)
turtle_left(90)
turtle_forward(size/2)
turtle_left(180)
turtle_forward(size/2)
turtle_right(90)
turtle_forward(size*3/2)

Clear code on second leg

# direction of second leg
turtle_right(100)
# second leg
turtle_forward(15)
turtle_left(90)
turtle_forward(5)
# get back to initial angle
turtle_right(100)
# second leg
turtle_forward(size*3/2)
turtle_left(90)
turtle_forward(size/2)

Abstraction and decomposition

We can simplify even more by decomposing and looking at the patterns in the problem

This can be decomposed as:

  • Body
  • Head
  • 2 arms
  • 2 legs

Abstraction and decomposition

draw_person <- function(size) {
  draw_head(size)
  turtle_left(180)
  turtle_forward(size/2)
  turtle_left(90)
  draw_arm(size)
  turtle_left(180)
  draw_arm(size)
  turtle_left(90)
  turtle_forward(size*3/2)
  turtle_left(40)
  draw_leg(size)
  turtle_right(40)
  draw_leg(size)
}

 

This is the main function.

It is my part.

I can change it.

You should not change it.

Instead, you have to provide functions for head, arms and legs.

Body parts

draw_head<-function(size){
}

draw_arm<-function(size){
}

draw_leg<-function(size){
}

 

This is a Contract

Each part commits to do something

Each part makes a promise

We promise to leave the turtle in the same position as we received it

Let’s start with the arms

You should always start with the easy parts

The easy part may not be the first part

We receive the turtle pointing in the arm direction

We must leave the turtle in the same place and same angle

draw_arm <- function(size) {
    turtle_forward(size)
    turtle_backward(size)
}

From arms to legs

draw_arm <- function(size) {
    turtle_forward(size)
    
    
    
    
    turtle_backward(size)
}

 

draw_leg <- function(size) {
    turtle_forward(size*3/2)
    turtle_left(90)
    turtle_forward(size/2)
    turtle_backward(size/2)
    turtle_right(90)
    turtle_backward(size*3/2)
}

Undoing

Notice that to undo something you have to undo each part in reverse order

You put your socks first, then your shoes

To undo you first “un-put” your shoes, then your socks

In general

Undo(A,B,C) = Undo(C), Undo(B), Undo(A)

There is another way

Each function has a separate environment with its own variables

We can save pos and angle at the start, and reset them at the end

draw_leg <- function(size) {
    old_pos <- turtle_getpos()
    old_angle <- turtle_getangle()
    turtle_forward(size*3/2)
    turtle_left(90)
    turtle_forward(size/2)
    turtle_setangle(old_angle)
    turtle_setpos(old_pos[1], old_pos[2])
}

This is useful when the drawing is complex. Keep it in mind

Separation of concerns

We separated the big problem on independent parts

We can change each part without affecting the others, as long as we keep our promises.

For example, you can change the position of the hands, the shape of the head and hands, an others

The code is in stick-person-2.R.

Bugs

Bugs

(bəɡ) noun

  1. a small insect.
    • informal a harmful microorganism, as a bacterium or virus.
  2. an insect of a large order distinguished by having mouthparts that are modified for piercing and sucking.
  3. a miniature microphone, typically concealed in a room or telephone, used for surveillance.
  4. an error in a computer program or system.

Debugging with RStudio

Debugging is designed to help you find bugs

To do this, you need to:

  • Begin running the code
  • Stop the code at the point where you suspect there is a problem
  • Walk through the code, step-by-step

Stopping on a line

Editor breakpoints

The most common way to stop on a line of code is to set a breakpoint.

You can do this by clicking to the left of the line number, or by pressing Shift+F9.

Using the debugger

Once your code stops, you will enter “debug mode”

Environment window

Usually in R you’re interacting with the “global environment”

In debug mode, RStudio shows the currently function’s environment

  • The objects you see in the Environment pane are in the current function
  • Your commands will be evaluated in the context of the function

Environment window

Code window

The code window shows you the currently executing function. The line about to execute is highlighted in yellow

Console

Console

While debugging, you’ll notice two changes to the R console

The first is that the prompt is different:

Browse[1]> 

This prompt indicates that you’re inside the R environment browser.

Console

While debugging you can use all the normal commands, plus this:

  • Commands are evaluated in the current environment
    • If your function has a variable named x, typing x at the prompt will show you the value of that variable
  • Pressing Enter at the console will execute the current command and move on to the next one
  • Several special debugging commands are available

New toolbar on top of the console:

This toolbar provides buttons for debug control commands

  • There’s no difference between using the toolbar and entering the commands directly
  • learn the command shortcuts

Extra commands when debugging

Command Shortcut Description
n or Enter F10 Execute next statement
s Shift+F4 Step into function
f Shift+F6 Finish function/loop
c Shift+F5 Continue running
Q Shift+F8 Stop debugging

You can also type help at the Browse[N]> prompt

Traceback

The traceback shows you how execution reached the current point,

  • from the first function that was run (at the bottom)
  • to the function that is running now (at the top).