Skip to content

Commit

Permalink
after variables
Browse files Browse the repository at this point in the history
  • Loading branch information
polla-fattah committed Jan 20, 2025
1 parent cd99352 commit bad478a
Show file tree
Hide file tree
Showing 6 changed files with 483 additions and 482 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ gem 'jekyll-feed'
gem 'jekyll-readme-index'
gem 'jemoji'
gem 'webrick'

gem 'jekyll-mermaid'
# gem "rails"
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ GEM
webrick (~> 1.7)
jekyll-feed (0.17.0)
jekyll (>= 3.7, < 5.0)
jekyll-mermaid (1.0.0)
jekyll-readme-index (0.3.0)
jekyll (>= 3.0, < 5.0)
jekyll-sass-converter (3.0.0)
Expand Down Expand Up @@ -117,6 +118,7 @@ PLATFORMS
DEPENDENCIES
jekyll
jekyll-feed
jekyll-mermaid
jekyll-readme-index
jemoji
webrick
Expand Down
181 changes: 1 addition & 180 deletions _chapters/010-r-programming/005-chapter00.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: R Programming Language
title: Tempo
slug: chapter00
order: 030
mermaid: true
Expand All @@ -8,182 +8,3 @@ abstract: >
<br>—Edsger W. Dijkstra
---


## **The Kitchen as a Programming Playground**

Programming is the process of creating a set of instructions that a computer can follow to perform specific tasks. Think of it as teaching a computer how to do something step by step. Computers can’t think for themselves; they need precise instructions from you to function correctly. Let’s break this down into simpler ideas.

Imagine you’re giving instructions to a friend on how to make a sandwich. You’d need to be specific: “Take two slices of bread, spread peanut butter on one slice, and jelly on the other, then put them together.” If you miss a step or are unclear, your friend might not make the sandwich correctly. Programming is like that—you write a detailed recipe for the computer to follow.

Imagine stepping into your kitchen. You see bowls, pans, knives, and a variety of ingredients waiting to be transformed into a meal. Similarly, in programming, we work with tools (operators), ingredients (data), and instructions (code) to create something functional and beautiful.

Let’s break it down:

- **Ingredients**: These are like data in your research – the raw materials we process.
- **Tools**: Knives, spoons, and blenders are the operators that manipulate data.
- **Instructions**: Recipes are our algorithms, step-by-step guides to achieve a result.
- **Decisions**: Whether to add salt or sugar is like making logical choices with if-else conditions.
- **Repetition**: Repeating the same action many times to get the required result just like staring the food in the pan while boiling.

## **Variables: The Bowls and Pans**

In the kitchen, we use bowls and pans to hold ingredients temporarily. In programming, these are called **variables** – containers that store values for later use.

### Example in R:

```r
# Assigning ingredients to variables
water <- 2 # liters
rice <- 500 # grams
```

Here, `water` and `rice` are the bowls, and `2` and `500` are the contents.

## **Operators: The Tools of the Kitchen**

Cooking is not just about holding ingredients; we chop, mix, and stir them. In programming, these actions are performed using **operators**.

### Example in R:

Adding water and rice:
```r
cooked_rice <- water + rice # Combine ingredients
```

Slicing an apple:
```r
slices <- 4 / 1 # Divide into 4 parts
```

## **Logic: Decisions in Cooking**
Every recipe has decision points. What if the soup is too salty? What if we’re out of an ingredient? These decisions mirror **if-else statements** in programming.

### Example in R:

```r
# Decide whether to add chili or sugar
if (taste == "spicy") {
add_chili()
} else {
add_sugar()
}
```

## **Loops: Repeating the Tedious Tasks**
Repetitive tasks, like rolling dolmas or stirring soup, are common in cooking. In programming, we use **loops** to handle these tasks efficiently.

### Example in R:
Rolling dolmas:
```r
for (leaf in grape_leaves) {
wrap(leaf, filling)
}
```

## **Functions: Reusable Recipes**
Chefs often reuse recipes for specific tasks. In programming, these are called **functions** – reusable blocks of instructions.

### Example in R:
```r
# Define a function to make tea
make_tea <- function(water, tea_leaves) {
boil(water)
add(tea_leaves)
return("Tea is ready!")
}

# Use the function
tea <- make_tea(2, 5) # 2 liters of water, 5 grams of tea leaves
```

## **Debugging: Fixing Mistakes**
Sometimes, a dish doesn’t taste right. Perhaps we added too much salt or forgot to cook something. In programming, this is called a **bug**, and debugging is the process of identifying and fixing the issue.

### Example in R:
A flawed tea recipe:
```r
make_tea <- function(water, tea_leaves) {
add(tea_leaves) # Error: Water is not boiled first!
boil(water)
return("Tea is ready!")
}
```

## **Your First Dish: Cooking with Code**
Let’s put it all together and create a simple dish: a fruit salad.

### Example in R:
```r
# Ingredients
fruits <- c("apple", "banana", "orange")

# Function to prepare the salad
make_fruit_salad <- function(fruits) {
salad <- c()
for (fruit in fruits) {
slices <- paste(fruit, "sliced")
salad <- c(salad, slices)
}
return(salad)
}

# Prepare the fruit salad
fruit_salad <- make_fruit_salad(fruits)
print(fruit_salad)
```


## **Cooking and Coding: A Reflection**
Programming, much like cooking, is about breaking a problem into manageable steps, using tools effectively, and following a logical sequence. It’s not just about the result; the process itself is a creative and fulfilling journey.

Now that we’ve cooked up our first program, you’re ready to explore more complex recipes in the next chapter. Bon appétit – or should we say, happy coding!


## Basic Concepts of Programming

Computers use special languages to understand these instructions, called **programming languages**. R is one such language, designed to help you analyze data, make calculations, and create visualizations.

### Why Does Programming Need to Be So Precise?

Computers are not like humans—they don’t understand hints or guesses. Every instruction must follow strict rules, known as **syntax**. For example, in R, the instruction to display a message is written like this:

```r
print("Hello, world!")
```

If you forget the quotation marks or the parentheses, the computer won’t understand what you mean, and it will give you an error. Syntax is like grammar for programming languages; you need to follow the rules exactly to get your program to work.

### The Importance of Structure

Good programming is organized and structured. This means writing instructions in a clear, step-by-step way so the computer knows exactly what to do. Let’s go back to the sandwich example. If you tell your friend, “Spread peanut butter” but don’t mention the bread first, they won’t know where to spread it! Similarly, in programming, you need to think carefully about the order of your instructions.

Programs are usually written in smaller chunks or blocks, each doing one thing. This makes them easier to understand and fix if something goes wrong.

### The Role of Logic in Programming

Programming also involves **logic**, which means thinking through the steps to solve a problem. For example, if you wanted a computer to decide whether to wear a raincoat, you might write:

- If it is raining, wear a raincoat.
- If it is sunny, wear sunglasses.

This logic is written in programming using special structures like "if" statements. Here’s how it might look in R:

```r
if (is_raining) {
print("Wear a raincoat")
} else {
print("Wear sunglasses")
}
```

The computer checks the condition (whether it’s raining) and follows the appropriate instruction based on the answer.


### A Simple Way to Think About Programming

At its heart, programming is:

1. **Breaking a big task into smaller steps.**
2. **Writing each step as an instruction the computer can understand.**
3. **Checking your instructions to make sure they work.**
4. **Fixing mistakes when they happen.**
Loading

0 comments on commit bad478a

Please sign in to comment.