Syntax and Control Flow
Practicum ~ Week 4
1 ). Introduction
By leveraging the analytical power of R, we are streamlining compensation management to ensure both precision and clarity. Through the integration of automated logic and data visualization, complex employee datasets are transformed into transparent bonus reports that provide stakeholders with immediate, actionable insights.
2 ). employee dataset
library(plotly)
library(dplyr)
nama <- c("Bagas","Joan","Alya","Dwi","Nabil")
usia <- c(25,30,27,35,40)
gaji <- c(5000,7000,6500,10000,12000)
posisi <- c("Staff","Supervisor","Staff","Manager","Director")
kinerja <- c("Good","Very Good","Average","Good","Very Good")
semua_bonus <- c()
for(i in 1:length(nama)){
if(kinerja[i] == "Very Good"){
bonus_temp <- gaji[i]*0.20
} else if(kinerja[i] == "Good"){
bonus_temp <- gaji[i]*0.10
} else{
bonus_temp <- gaji[i]*0.05
}
semua_bonus[i] <- bonus_temp
cat("Name:",nama[i],", Bonus:",bonus_temp,"\n")
}## Name: Bagas , Bonus: 500
## Name: Joan , Bonus: 1400
## Name: Alya , Bonus: 325
## Name: Dwi , Bonus: 1000
## Name: Nabil , Bonus: 2400
library(plotly)
library(dplyr)
employee_data <- data.frame(
name = c("Bagas","Joan","Alya","Dwi","Nabil"),
salary = c(5000, 7000, 6500, 10000, 12000),
bonus = c(500, 1400, 325, 1000, 2400)
) %>%
mutate(total_income = salary + bonus) %>%
arrange(total_income)
plot_ly(employee_data,
x = ~reorder(name, total_income),
y = ~salary,
type = "bar",
marker = list(
color = "#F8BBD0",
line = list(color = "white", width = 1)
)) %>%
add_trace(
y = ~bonus,
marker = list(
color = "#FCE4EC",
line = list(color = "white", width = 1)
)
) %>%
layout(
title = list(text = "<b>Employee Income Overview</b>", x = 0.5),
xaxis = list(title = "Employee Name"),
yaxis = list(title = "Total Income", gridcolor = "#F5D0DA"),
barmode = "stack",
hovermode = "x",
showlegend = FALSE,
width = 650,
height = 380,
paper_bgcolor = "#FFF7FA",
plot_bgcolor = "#FFF7FA",
font = list(color = "#5A5A5A")
) %>%
config(displayModeBar = FALSE)2.1 Key Insights from the Chart
The stacked bar chart illustrates the composition of employee income, which consists of base salary and bonus. Each bar represents the total income of an employee, where the darker pink indicates the base salary and the lighter pink represents the bonus component.
From the visualization, Nabil has the highest total income, followed by Dwi, while Bagas has the lowest total income among the employees. This comparison makes it easier to see how earnings differ across individuals.
The chart also shows that base salary contributes the largest portion of income for all employees, while bonuses serve as an additional component that increases total compensation.
Overall, the visualization helps compare employee earnings and clearly highlights the differences in total compensation among individuals.
3 ). For loop (salary > 6000)
## Name: Joan , Salary: 7000
## Name: Alya , Salary: 6500
## Name: Dwi , Salary: 10000
## Name: Nabil , Salary: 12000
library(plotly)
library(dplyr)
# Dataset
employee_salary <- data.frame(
name = c("Bagas","Joan","Alya","Dwi","Nabil"),
salary = c(5000,7000,6500,10000,12000)
) %>%
mutate(
status = ifelse(salary > 6000, "Selected", "Filtered Out")
)
# Visualization Pie Chart (Tanpa Legend)
plot_ly(employee_salary,
labels = ~name,
values = ~salary,
type = 'pie',
textinfo = 'label+percent',
textposition = 'outside', # Menempatkan teks di luar agar rapi tanpa legend
marker = list(
colors = ifelse(employee_salary$status == "Selected", "#F8BBD0", "#FCE4EC"),
line = list(color = "white", width = 2)
)) %>%
layout(
title = list(text = "<b>Salary Distribution by Employee</b>", x = 0.5),
paper_bgcolor = "#FFF7FA",
plot_bgcolor = "#FFF7FA",
showlegend = FALSE, # Legend dihilangkan sesuai permintaan
font = list(color = "#5A5A5A")
) %>%
config(displayModeBar = FALSE)3.1 Salary Filtering Analysis
The visualization illustrates how the for loop filters employee data based on a salary condition. In this case, the loop prints only employees whose salary is greater than 6,000.
From the dataset, Joan, Alya, Dwi, and Nabil meet the condition because their salaries are 7,000, 6,500, 10,000, and 12,000 respectively. Therefore, these employees are included in the output generated by the loop.
On the other hand, Bagas has a salary of
5,000, which does not satisfy the condition
salary > 6000. As a result, Bagas is excluded
from the printed output, and the loop continues to the next
iteration without displaying his data.
Overall, this visualization demonstrates how a conditional statement inside a loop can be used to filter data, allowing the program to display only the elements that meet specific criteria while ignoring others.
4 ). While loop until manager
i <- 1
while(i <= length(nama)){
cat("Name:",nama[i],", Position:",posisi[i],"\n")
if(posisi[i] == "Manager"){
cat("(Stop here)\n")
break
}
i <- i + 1
}## Name: Bagas , Position: Staff
## Name: Joan , Position: Supervisor
## Name: Alya , Position: Staff
## Name: Dwi , Position: Manager
## (Stop here)
library(plotly)
library(dplyr)
# 1. SETUP DATA
name <- c("Bagas", "Joan", "Alya", "Dwi", "Nabil")
position <- c("Staff", "Supervisor", "Staff", "Manager", "Director")
salary <- c(5000, 7000, 6500, 10000, 12000)
performance <- c("Good", "Very Good", "Average", "Good", "Very Good")
# 2. DATA PROCESSING (Bonus Logic & Loop Status)
df_sequence <- data.frame(name, position, salary, performance) %>%
mutate(
# Bonus calculation based on your provided logic
bonus = case_when(
performance == "Very Good" ~ salary * 0.20,
performance == "Good" ~ salary * 0.10,
TRUE ~ salary * 0.05
),
# Determining execution flow based on 'while' loop and 'break' at Manager
status = ifelse(row_number() <= 4, "Processed", "Excluded"),
hover_text = paste0("<b>Name:</b> ", name,
"<br><b>Bonus:</b> $", bonus,
"<br><b>Position:</b> ", position)
)
# 3. SEQUENTIAL VISUALIZATION
plot_ly(df_sequence,
x = ~name,
y = ~position,
type = "scatter",
mode = "markers+lines",
text = ~hover_text,
hoverinfo = "text",
marker = list(
size = 18,
color = ~ifelse(status == "Processed", "#F8BBD0", "#FCE4EC"),
line = list(color = "white", width = 2)
),
line = list(color = "#F8BBD0", width = 1.5, dash = "dot")) %>%
layout(
title = list(text = "<b>Algorithm Execution Sequence & Bonus Calculation</b>", x = 0.5),
xaxis = list(title = "Employee (Processing Order)", categoryorder = "trace"),
yaxis = list(title = "Organizational Position"),
paper_bgcolor = "#FFF7FA",
plot_bgcolor = "#FFF7FA",
font = list(color = "#5A5A5A"),
showlegend = FALSE,
annotations = list(
x = "Dwi",
y = "Manager",
text = "<b>Loop Terminated</b><br>(Manager Found)",
showarrow = TRUE,
arrowhead = 2,
arrowcolor = "#D81B60",
ax = 0,
ay = -50,
font = list(color = "#D81B60", size = 12)
)
) %>%
config(displayModeBar = FALSE)4.1 Loop Execution Summary
The visualization illustrates the sequence of loop execution across employees based on their positions. Each point represents an employee processed sequentially in the loop, starting from Bagas and continuing through Joan, Alya, and Dwi.
In this process, the loop continues running while evaluating each employee’s position. The employees Bagas (Staff), Joan (Supervisor), and Alya (Staff) are processed normally as the loop moves through the dataset. When the loop reaches Dwi, who holds the Manager position, the condition that controls the loop execution is triggered.
As a result, the loop terminates at Dwi, which is highlighted in the visualization by the annotation indicating the termination point. Because the loop stops at this stage, the next employee in the sequence, Nabil (Director), is excluded from the loop processing.
Overall, this visualization demonstrates how a loop processes data sequentially and stops when a specific condition is met, preventing further elements in the dataset from being evaluated. This helps illustrate the concept of loop control and early termination in algorithm execution.
5 ). Break if salary > 10000
for(i in 1:length(nama)){
if(gaji[i] > 10000){
cat("(Stopped because",nama[i],"has a salary above 10,000)\n")
break
}
cat("Name:",nama[i],", Salary:",gaji[i],"\n")
}## Name: Bagas , Salary: 5000
## Name: Joan , Salary: 7000
## Name: Alya , Salary: 6500
## Name: Dwi , Salary: 10000
## (Stopped because Nabil has a salary above 10,000)
library(plotly)
library(dplyr)
# Prepare employee data
employee_salary <- data.frame(
name = c("Bagas", "Joan", "Alya", "Dwi", "Nabil"),
salary = c(5000, 7000, 6500, 10000, 12000)
) %>%
mutate(
status = ifelse(salary > 10000, "Terminated", "Processed")
)
# Plotly visualization (soft pink theme)
plot_ly(employee_salary,
x = ~name,
y = ~salary,
type = "bar",
marker = list(
color = ifelse(employee_salary$status == "Processed", "#F8BBD0", "#F48FB1"),
line = list(color = "white", width = 1)
)) %>%
layout(
title = list(text = "<b>Loop Termination Logic (Threshold > 10,000)</b>", x = 0.5),
xaxis = list(title = "Employee Name"),
yaxis = list(title = "Salary Amount", gridcolor = "#F5D0DA"),
paper_bgcolor = "#FFF7FA",
plot_bgcolor = "#FFF7FA",
font = list(color = "#5A5A5A"),
showlegend = FALSE,
annotations = list(
x = "Nabil",
y = 12000,
text = "Execution Stopped",
showarrow = TRUE,
arrowhead = 2,
ax = 0,
ay = -40
)
) %>%
config(displayModeBar = FALSE)5.1 Loop Termination Analysis
The code demonstrates how a for loop processes employee data sequentially while checking a specific condition related to salary. In each iteration, the program prints the employee’s name and salary until it encounters a value that meets the stopping condition.
From the visualization, the loop successfully processes the data for Bagas, Joan, Alya, and Dwi, whose salaries are 5,000, 7,000, 6,500, and 10,000 respectively. Since all of these salaries are less than or equal to 10,000, the loop continues executing normally and prints their information.
However, when the loop reaches Nabil, whose salary
is 12,000, the condition
salary > 10,000 becomes true. As a result, the
break statement is triggered, which immediately stops
the loop execution. The message indicating that the process stopped
because the salary exceeded the threshold is printed, and no further
iterations occur.
Overall, the visualization highlights how a loop termination condition controls the flow of execution. It clearly shows that the algorithm continues processing data while the condition is not met and stops immediately once the threshold is exceeded.
6 ). Continue (Skip average)
for(i in 1:length(nama)){
if(kinerja[i] == "Average"){
cat("(",nama[i],"is skipped because performance is Average )\n")
next
}
cat("Name:",nama[i],", Performance:",kinerja[i],"\n")
}## Name: Bagas , Performance: Good
## Name: Joan , Performance: Very Good
## ( Alya is skipped because performance is Average )
## Name: Dwi , Performance: Good
## Name: Nabil , Performance: Very Good
library(plotly)
library(dplyr)
# Prepare employee performance data
employee_performance <- data.frame(
name = c("Bagas", "Joan", "Alya", "Dwi", "Nabil"),
performance = c("Average", "Very Good", "Good", "Average", "Very Good")
) %>%
mutate(
status = ifelse(performance == "Average", "Skipped", "Processed")
)
# Visualization with soft pink theme
plot_ly(employee_performance,
x = ~name,
y = ~performance,
type = "scatter",
mode = "markers",
marker = list(
size = 18,
color = ifelse(employee_performance$status == "Processed", "#F8BBD0", "#FCE4EC"),
line = list(color = "white", width = 1.5)
)) %>%
layout(
title = list(text = "<b>Execution Flow: Skipping 'Average' Performance</b>", x = 0.5),
xaxis = list(title = "Employee Name"),
yaxis = list(title = "Performance Level"),
paper_bgcolor = "#FFF7FA",
plot_bgcolor = "#FFF7FA",
font = list(color = "#5A5A5A"),
showlegend = FALSE
) %>%
config(displayModeBar = FALSE)6.1 Execution Flow Analysis
The visualization illustrates how a loop processes employee data while skipping specific iterations based on a condition. In this case, the condition checks the employee’s performance level and skips those categorized as “Average”.
From the chart, Bagas and Dwi have a performance level labeled “Average”, which causes their iterations to be skipped during the loop execution. As a result, these entries are marked as Skipped and are not fully processed by the algorithm.
Meanwhile, employees with higher performance levels such as Joan, Alya, and Nabil are categorized as Processed because their performance values (“Very Good” and “Good”) do not meet the skipping condition. Therefore, the loop continues to process and evaluate their data normally.
Overall, this visualization demonstrates how a loop can
bypass certain elements without stopping the entire
process. Unlike loop termination using break, the
skipping mechanism allows the algorithm to continue running while
selectively ignoring specific data that meet the defined condition.
7 Referensi
Wickham, H., & Grolemund, G. (2017). R for Data Science. O’Reilly Media.
R Core Team. (2023). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing.
Xie, Y., Allaire, J., & Grolemund, G. (2018). R Markdown: The Definitive Guide. Chapman and Hall/CRC.
Plotly Technologies Inc. (2023). Plotly for R Documentation.
Wickham, H. (2023). dplyr: A Grammar of Data Manipulation. R Package Documentation.