How to use interactive coding
What is the interactive coding widget
With the help of DataCamp, we designed our website to include an interactive learning platform that supports both R and Python coding.
In the editor below, you should type R code to solve the exercises. When you hit the ‘Submit’ button, every line of code is interpreted and executed by R, and you receive a message indicating whether or not your code was correct. The outputs of your R codes are shown in the right-hand side R console.
In R, you can use the # sign to add comments, allowing you and others to understand what the R code is about. Just like Twitter! Comments are not run as R code, so they will not influence your result. For example, ‘# Calculate 3 + 4’ in the editor is a comment.”
You can also execute R commands directly in the right-hand side console. This is a good way to experiment with R code, as your submission is not checked for correctness.
How it works: An R exercise
# no pec
# Calculate 3 + 4
3 + 4
# Calculate 6 + 12
# Calculate 3 + 4
3 + 4
# Calculate 6 + 12
6 + 12
test_output_contains("18", incorrect_msg = "Make sure to add `6 + 12`
on
a new line. Do not start the line with a `#`, otherwise your R code is not executed!")
success_msg("Awesome! See how the console shows the result of the R code you
submitted? Now that you're familiar with the interface, let's get down to R
business!")
Just add a line of R code that calculates the sum of 6 and 12, just like the example in the sample code!
Instructions:
In the editor above there is already some sample code. Can you see which lines are actual R code and which are comments?
Add a line of code that calculates the sum of 6 and 12, and hit the ‘Submit’ button.
An exercise
Economic Order Quantity Model: \(Q= \sqrt{2DK/h}\)
- D=5000: annual demand quantity
- K=$4: fixed cost per order
- h=$0.5: holding cost per unit
- Q=?
Economic Order Quantity Model
# no pec
# D=5000: annual demand quantity
D <- 5000
# K=$4: fixed cost per order
K <-
# h=$0.5: holding cost per unit
h <-
# Q=?
Q <-
# D=5000: annual demand quantity
D <- 5000
# K=$4: fixed cost per order
K <- 4
# h=$0.5: holding cost per unit
h <- 0.5
# Q=?
Q <- sqrt(2*D*K/h)
Q
test_object("D",
undefined_msg = "Please make sure to define a variable `D`.",
incorrect_msg = "Make sure that you assign the correct value to `D`.")
test_object("K",
undefined_msg = "Please make sure to define a variable `K`.",
incorrect_msg = "Make sure that you assign the correct value to `K`.")
test_object("h",
undefined_msg = "Please make sure to define a variable `h`.",
incorrect_msg = "Make sure that you assign the correct value to `h`.")
test_object("Q",
undefined_msg = "Please make sure to define a variable `Q`.",
incorrect_msg = "Make sure that you assign the correct value to `Q`.")
test_output_contains("Q",
incorrect_msg = "Have you explicitly told R to print out the `Q`
variable to the console?")
success_msg("Great! Continue to the next exercise!")
Add lines of R codes to save these given numbers, then follow the formula to calculate the order quantity Q!
An python exercise with a plot output
An python exercise with a plot
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)
plt.show()