This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# คำนวณคะแนนรวมของแต่ละทีม
team_points <- df %>%
group_by(constructor_id) %>%
summarise(total_points = sum(points, na.rm = TRUE)) %>%
arrange(desc(total_points))
# สร้างกราฟแท่งคะแนนรวมทีม
p1 <- ggplot(team_points, aes(x = reorder(constructor_id, total_points), y = total_points, fill = constructor_id)) +
geom_col(show.legend = FALSE) +
coord_flip() +
labs(title = "คะแนนรวมของแต่ละทีมตลอด 17 สนาม",
x = "ทีม (Constructor)",
y = "คะแนนรวม (Points)") +
theme_minimal() +
geom_text(aes(label = total_points), hjust = -0.1, size = 3.5)
ggplotly(p1)
You can also embed plots, for example:
# ดึงรายชื่อทีมทั้งหมดเรียงตามตัวอักษร
teams_list <- sort(unique(df$constructor_id))
fluidRow(
# ฝั่งซ้าย: แผงควบคุมตัวเลือก (ขนาด 4/12)
column(4,
wellPanel(
h4("🔍 ค้นหาผู้เล่น / ทีม"),
hr(),
# 1. ตัวเลือกทีม
selectInput("team_filter", "1. เลือกทีม:",
choices = c("ทั้งหมด", teams_list),
selected = "ทั้งหมด"),
br(),
# 2. ตัวเลือกผู้เล่น (ปรับตามทีมที่เลือกแบบ Dynamic)
renderUI({
if (input$team_filter == "ทั้งหมด") {
drivers_list <- sort(unique(df$driver_id))
} else {
drivers_list <- df %>%
filter(constructor_id == input$team_filter) %>%
pull(driver_id) %>%
unique() %>%
sort()
}
selectInput("driver_filter", "2. เลือกผู้เล่น:",
choices = c("ทั้งหมด", drivers_list),
selected = "ทั้งหมด")
})
)
),
# ฝั่งขวา: การ์ดสรุป + ตารางข้อมูลแบบ Interactive DT (ขนาด 8/12)
column(8,
# การ์ดสรุปสถิติ 3 ค่าหลัก
renderUI({
filtered_df <- df
if (input$team_filter != "ทั้งหมด") {
filtered_df <- filtered_df %>% filter(constructor_id == input$team_filter)
}
if (!is.null(input$driver_filter) && input$driver_filter != "ทั้งหมด") {
filtered_df <- filtered_df %>% filter(driver_id == input$driver_filter)
}
total_pts <- sum(filtered_df$points, na.rm = TRUE)
best_pos <- suppressWarnings(min(filtered_df$position, na.rm = TRUE))
best_pos_txt <- if(is.infinite(best_pos)) "ไม่มีข้อมูล" else paste("อันดับที่", best_pos)
best_time <- suppressWarnings(min(filtered_df$time_sec, na.rm = TRUE))
best_time_txt <- if(is.infinite(best_time)) "ไม่มีข้อมูล" else paste(best_time, "วินาที")
fluidRow(
column(4,
div(style = "background-color: #eef6ff; border-radius: 8px; padding: 12px; text-align: center; border-left: 5px solid #007bff; margin-bottom: 15px;",
h5("คะแนนรวม 17 สนาม"),
h3(style = "color: #007bff; font-weight: bold; margin: 0;", total_pts)
)
),
column(4,
div(style = "background-color: #eef9f1; border-radius: 8px; padding: 12px; text-align: center; border-left: 5px solid #28a745; margin-bottom: 15px;",
h5("Position ที่ดีที่สุด"),
h3(style = "color: #28a745; font-weight: bold; margin: 0;", best_pos_txt)
)
),
column(4,
div(style = "background-color: #fdf0f0; border-radius: 8px; padding: 12px; text-align: center; border-left: 5px solid #dc3545; margin-bottom: 15px;",
h5("เวลาเร็วที่สุดใน 17 สนาม"),
h3(style = "color: #dc3545; font-weight: bold; margin: 0;", best_time_txt)
)
)
)
}),
# ตารางสถิติผู้เล่นแบบ DT
renderDT({
filtered_df <- df
if (input$team_filter != "ทั้งหมด") {
filtered_df <- filtered_df %>% filter(constructor_id == input$team_filter)
}
if (!is.null(input$driver_filter) && input$driver_filter != "ทั้งหมด") {
filtered_df <- filtered_df %>% filter(driver_id == input$driver_filter)
}
player_summary <- filtered_df %>%
group_by(constructor_id, driver_id) %>%
summarise(
`คะแนนรวม 17 สนาม` = sum(points, na.rm = TRUE),
`Position ที่ดีที่สุด` = suppressWarnings(min(position, na.rm = TRUE)),
`ความเร็วที่ดีที่สุด (วินาที)` = round(suppressWarnings(min(time_sec, na.rm = TRUE)), 3),
.groups = "drop"
) %>%
rename(
`ทีม` = constructor_id,
`ผู้เล่น` = driver_id
) %>%
arrange(desc(`คะแนนรวม 17 สนาม`))
datatable(
player_summary,
options = list(
pageLength = 8,
lengthMenu = c(5, 8, 10, 15, 20),
language = list(
search = "Search:",
lengthMenu = "Show _MENU_ entries"
)
),
rownames = FALSE,
class = 'cell-border stripe hover'
)
})
)
)
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
renderPlotly({
plot_data <- df
# กรองข้อมูลจากปุ่มเลือกทีมและผู้เล่นในส่วนที่ 2
if (input$team_filter != "ทั้งหมด") {
plot_data <- plot_data %>% filter(constructor_id == input$team_filter)
}
if (!is.null(input$driver_filter) && input$driver_filter != "ทั้งหมด") {
plot_data <- plot_data %>% filter(driver_id == input$driver_filter)
}
# สร้าง Scatter Plot แบบจุดล้วน
p3 <- ggplot(plot_data, aes(
x = round,
y = time_sec,
color = driver_id,
text = paste("สนามที่:", round,
"<br>ทีม:", constructor_id,
"<br>นักแข่ง:", driver_id,
"<br>เวลาต่อรอบ:", time_sec, "วินาที")
)) +
geom_point(size = 3.5, alpha = 0.85) +
scale_x_continuous(breaks = 1:17) +
labs(title = paste("เวลาต่อรอบในแต่ละสนาม (Round 1 - 17) -",
if(input$team_filter == "ทั้งหมด") "ทุกทีม" else input$team_filter),
x = "สนามการแข่งขัน (Round 1 - 17)",
y = "เวลาต่อรอบที่ดีที่สุด (วินาที)",
color = "ผู้เล่น") +
theme_minimal()
ggplotly(p3, tooltip = "text")
})