Running Code

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Running Code

How can I run a program in R?

I understand simple commands such as assigning a value to a variable like a=2.

So far, all I seem to be able to do is enter simple one line commands (similar to entering a formula into an Excel cell) and returning a value.

How can I run this for example (taken from here:

lcm <- function(x, y) {
    # choose the greater number
    if(x > y) {
        greater = x
    } else {
        greater = y

    while(TRUE) {
        if((greater %% x == 0) && (greater %% y == 0)) {
            lcm = greater
        greater = greater + 1

# take input from the user
num1 = as.integer(readline(prompt="Enter first number: "))
num2 = as.integer(readline(prompt="Enter second number: "))

print(paste("The L.C.M. of", num1,"and", num2,"is", lcm(num1, num2)))

I cut and paste the code into R Studio but get an error message.