## R Script Sample File
## Source: http://www.rexamples.com

## Basics
a <- 42
A <- a * 2  # R is case sensitive
print(a)
cat(A, "\n") # "84" is concatenated with "\n"
if(A>a) # true, 84 > 42
<beginfold id='1'>{</beginfold id='1'>
  cat(A, ">", a, "\n")
<endfold id='1'>}</endfold id='1'>

## Functions
Square <- function(x) <beginfold id='1'>{</beginfold id='1'>
  return(x^2)
<endfold id='1'>}</endfold id='1'>

print(Square(4))
print(Square(x=4)) # same thing

DoSomething(color="red",number=55)

## Countdown
countdown <- function(from)
<beginfold id='1'>{</beginfold id='1'>
  print(from)
  while(from!=0)
  <beginfold id='1'>{</beginfold id='1'>
    Sys.sleep(1)
    from <- from - 1
    print(from)
  <endfold id='1'>}</endfold id='1'>
<endfold id='1'>}</endfold id='1'>

countdown(5)

## Reading user input
readinteger <- function()
<beginfold id='1'>{</beginfold id='1'>
  n <- readline(prompt="Enter an integer: ")
  n <- as.integer(n)
  if (is.na(n))<beginfold id='1'>{</beginfold id='1'>
    n <- readinteger()
  <endfold id='1'>}</endfold id='1'>
  return(n)
<endfold id='1'>}</endfold id='1'>
print(readinteger())

readinteger <- function()
<beginfold id='1'>{</beginfold id='1'>
  n <- readline(prompt="Enter an integer: ")
  if(!grepl("^[0-9]+$",n))
  <beginfold id='1'>{</beginfold id='1'>
    return(readinteger())
  <endfold id='1'>}</endfold id='1'>

  return(as.integer(n))
<endfold id='1'>}</endfold id='1'>
print(readinteger())

## Guess a random number game
readinteger <- function()
<beginfold id='1'>{</beginfold id='1'>
  n <- readline(prompt="Enter an integer: ")
  if(!grepl("^[0-9]+$",n))
  <beginfold id='1'>{</beginfold id='1'>
    return(readinteger())
  <endfold id='1'>}</endfold id='1'>
  return(as.integer(n))
<endfold id='1'>}</endfold id='1'>

# real program start here

num <- round(runif(1) * 100, digits = 0)
guess <- -1

cat("Guess a number between 0 and 100.\n")

while(guess != num)
<beginfold id='1'>{</beginfold id='1'>
  guess <- readinteger()
  if (guess == num)
  <beginfold id='1'>{</beginfold id='1'>
    cat("Congratulations,", num, "is right.\n")
  <endfold id='1'>}</endfold id='1'>
  else if (guess < num)
  <beginfold id='1'>{</beginfold id='1'>
    cat("It's bigger!\n")
  <endfold id='1'>}</endfold id='1'>
  else if(guess > num)
  <beginfold id='1'>{</beginfold id='1'>
    cat("It's smaller!\n")
  <endfold id='1'>}</endfold id='1'>
<endfold id='1'>}</endfold id='1'>

## Lists
sum(0:9)
append(LETTERS[1:13],letters[14:26])
c(1,6,4,9)*2
something <- c(1,4,letters[2])  # indices start at one, you get (1,4,"b")
length(something)

## margrittr's pipe
diamonds %>%
  filter(price > 1000) %>%
  group_by(cut) %>%
  tally() %>% 
  rename(tally = n) %>% 
  arrange(-tally) %>% 
  mutate(pct = tally / sum(tally)) -> df

## R native pipes (R > 4.1)
Sys.setenv("_R_USE_PIPEBIND_"= TRUE)
mtcars |> x => lm(mpg ~ cyl, data = x)

## ggplot2 
plot = ggplot(diamonds, aes(x = price, y = carat)) +
  geom_point(alpha = 0.3, colour = 'steelblue') +
  labs(
    title = "ggplot diamonds",
    x = "Price, $",
    y = "Carat"
  ) +
  facet_wrap(~ cut) +
  theme_minimal()

plot + coord_flip()
