TalatThaiR เป็น R package ที่จะพาคุณไปเดินสำรวจราคาของสินค้าเกษตรต่าง ๆ ซึี่งคุณสามารถทราบราคาสินค้าเกษตรและดัชนีเกษตรของไทย โดยแหล่งที่มาของข้อมูลนั้นมาจาก จาก API ของสำนักงานเศรษฐกิจการเกษตร (NABC)
ภาพรวม
talatThaiR ช่วยให้ผู้สนใจสามารถดึงข้อมูลจาก agriapi.nabc.go.th เข้าสู่ R ซึ่ง ใคร จะใช้ RStudio Positron หรือ จาก IDE ที่สามารถ ใช้ R ได้โดยตรง โดยไม่ต้องจัดการ HTTP request หรือ pagination (page) ด้วยตนเอง ข้อมูลทั้งหมด return กลับมาเป็น data.frame พร้อมใช้งาน
ข้อมูลที่รองรับ:
- ราคาสินค้าเกษตร รายวัน / รายสัปดาห์ / รายเดือน
- ดัชนีผลผลิตการเกษตร รายเดือน / รายไตรมาส / รายปี
- ดัชนีราคาสินค้าเกษตร รายเดือน / รายไตรมาส / รายปี
การติดตั้ง
# ติดตั้งจาก GitHub
# install.packages("remotes")
remotes::install_github("kanthjs/talatThaiR")Functions
| Function | ข้อมูลที่ได้ |
|---|---|
get_daily_prices() |
ราคาสินค้าเกษตรรายวัน |
get_weekly_prices() |
ราคาสินค้าเกษตรรายสัปดาห์ |
get_monthly_prices() |
ราคาสินค้าเกษตรรายเดือน |
get_production_index_month() |
ดัชนีผลผลิตการเกษตรรายเดือน |
get_production_index_quarter() |
ดัชนีผลผลิตการเกษตรรายไตรมาส |
get_production_index_year() |
ดัชนีผลผลิตการเกษตรรายปี |
get_price_index_month() |
ดัชนีราคาสินค้าเกษตรรายเดือน |
get_price_index_quarter() |
ดัชนีราคาสินค้าเกษตรรายไตรมาส |
get_price_index_year() |
ดัชนีราคาสินค้าเกษตรรายปี |
Helper functions สำหรับดูรหัสที่ใช้ได้:
| Function | แสดงรหัสของ |
|---|---|
show_daily_categories() |
หมวดสินค้ารายวัน (13 หมวด) |
show_daily_products() |
สินค้ารายวัน (27 รายการ) |
show_weekly_categories() |
หมวดสินค้ารายสัปดาห์/เดือน (15 หมวด) |
show_weekly_products() |
สินค้ารายสัปดาห์/เดือน |
show_index_categories() |
หมวดดัชนี (FISHERY / LIVESTOCK / MAJOR_CROP) |
show_index_groups() |
กลุ่มดัชนี (8 กลุ่ม) |
show_index_products() |
สินค้าดัชนีรายเดือน |
show_quarter_products() |
สินค้าดัชนีรายไตรมาส/ปี |
การใช้งานเบื้องต้น
ข้อมูลราคาสินค้าเกษตรรายวัน
library(talatThaiR)
# ดูว่ามีหมวดสินค้าอะไรบ้าง
show_daily_categories()
# ดึงราคากุ้งทุกรายการ
get_daily_prices(category_code = "SHRIMP")
# ดึงราคามะนาวผลขนาดใหญ่พิเศษ
get_daily_prices(product_code = "LIME_XL")
# ดึงราคาสินค้าทุกชนิดของวันที่กำหนด
get_daily_prices(date = "2025-06-01")
# ดึงราคาข้าวหอมมะลิ ช่วง 1 ม.ค. 2569 ถึงปัจจุบัน
get_daily_prices(category_code = "RICE_MALI", start_date = "2026-01-01")หมายเหตุ: ระบุได้เพียงหนึ่งโหมดต่อการเรียก (category_code, product_code, หรือ date)
ข้อมูลราคาสินค้าเกษตรรายสัปดาห์
# ดูรหัสสินค้าที่มี
show_weekly_categories()
show_weekly_products()
# ดึงราคากระบือทุกรายการ
get_weekly_prices(category_code = "BUFFALO")
# ดึงราคาสุกรมีชีวิต > 100 กก.
get_weekly_prices(product_code = "PORK_LIVE_100")
# ดึงราคาทุกชนิดของเดือนกุมภาพันธ์ พ.ศ. 2569
get_weekly_prices(year_th = 2569, month = 2)
# ดึงราคายางพาราเฉพาะเดือน มิ.ย. 2568
get_weekly_prices(category_code = "RUBBER", year_th = 2568, month = 6)หมายเหตุ: year_th และ month ต้องระบุคู่กันเสมอ
ข้อมูลราคาสินค้าเกษตร รายเดือน
# โหมดหลัก: ค้นด้วย category หรือ product
get_monthly_prices(category_code = "BUFFALO")
get_monthly_prices(product_code = "PORK_LIVE_100")
# ค้นด้วยปีและเดือน
get_monthly_prices(year_th = 2569, month = 2)
# กรองเพิ่มเติม: เฉพาะปี หรือ ปี+เดือน
get_monthly_prices(product_code = "PORK_LIVE_100", year_th = 2569)
get_monthly_prices(category_code = "BUFFALO", year_th = 2569, month = 2)ข้อมูลดัชนีผลผลิตสินค้าเกษตร รายเดือน
# ดูรหัสหมวด กลุ่ม และสินค้า
show_index_categories()
show_index_groups()
show_index_products()
# ดึงดัชนีทุกรายการในหมวดปศุสัตว์
get_production_index_month(category_code = "LIVESTOCK")
# ดึงดัชนีกลุ่มพืชน้ำมัน เฉพาะเดือน มิ.ย.
get_production_index_month(group_code = "OIL_CROP", month = 6)
# ดึงดัชนีกล้วยหอมทอง เฉพาะเดือน มี.ค.
get_production_index_month(product_code = "BANANA_HOM_THONG", month = 3)
# ดึงดัชนีทุกสินค้าของเดือน ธ.ค. 2568
get_production_index_month(year_th = 2568, month = 12)
# ดึงรายชื่อ sector ทั้งหมด
get_production_index_month(sector = TRUE)ข้อมูลดัชนีผลผลิตการเกษตร รายไตรมาส
# หมายเหตุ: product_code ใช้รหัสจาก show_quarter_products()
show_quarter_products()
get_production_index_quarter(category_code = "LIVESTOCK")
get_production_index_quarter(group_code = "OIL_CROP", quarter = 3)
get_production_index_quarter(product_code = "GARLIC", quarter = 2)
# ดึงทุกสินค้าของไตรมาส 4 ปี 2568
get_production_index_quarter(year_th = 2568, quarter = 4)
get_production_index_quarter(sector = TRUE)ดัชนีผลผลิตการเกษตรรายปี
get_production_index_year(category_code = "LIVESTOCK")
get_production_index_year(group_code = "OIL_CROP", year_th = 2567)
get_production_index_year(product_code = "GARLIC") # ใช้ .QUARTER_PRODUCT_MAP
# ดึงทุกสินค้าของปี 2568
get_production_index_year(year_th = 2568)
# sector + กรองปี
get_production_index_year(sector = TRUE)
get_production_index_year(sector = TRUE, year_th = 2567)ข้อมูลดัชนีราคาสินค้าเกษตร รายเดือน
# หมายเหตุ: product_code ใช้รหัสจาก show_index_products()
get_price_index_month(category_code = "LIVESTOCK")
get_price_index_month(group_code = "OIL_CROP", year_th = 2567, month = 1)
get_price_index_month(product_code = "GARLIC_DRY_MIX", month = 1)
# ดึงทุกสินค้าของเดือน ธ.ค. 2568
get_price_index_month(year_th = 2568, month = 12)
# sector สามารถกรองด้วย year_th ได้
get_price_index_month(sector = TRUE)
get_price_index_month(sector = TRUE, year_th = 2567)ดัชนีราคาสินค้าเกษตรรายไตรมาสและรายปี
# รายไตรมาส — product_code ใช้ show_index_products()
get_price_index_quarter(category_code = "LIVESTOCK", quarter = 1)
get_price_index_quarter(product_code = "GARLIC_DRY_MIX", year_th = 2567, quarter = 3)
get_price_index_quarter(year_th = 2568, quarter = 4)
# รายปี
get_price_index_year(category_code = "LIVESTOCK", year_th = 2567)
get_price_index_year(product_code = "GARLIC_DRY_MIX")
get_price_index_year(year_th = 2568)รหัสสินค้าและหมวดหมู่
สำหรับการเรียกชื่อสินค้า product_name และ product_group หรือ แม่แต่ product_category นั้น สำหรับฐานข้อมูลของ NABC นั้น พบว่ามีข้อจำกัดและเกิดความสับสนได้ไง ยกตัวอย่างเช่น product_name สำหรับราคาสินค้าเกษตร รายวัน เรียก น้ำยางพาราสด แต่ ราคาสินค้าเกษตร รายสัปดาห์ และ ราคาสินค้าเกษตรรายเดือน เรียก น้ำยางสด คละ (คิดว่าเป็นคนละสินค้ากัน แต่ก็ มีความคล้ายกัน) product_name ที่จะให้เป็นมาตราฐาน เรยก จึงไม่เหมือนกัน ดังนั้น จึงต้องมี list ของ รายการ แยกกันไป ดังนั้น เพื่อกันความสับสน ผู้ใช้ จึงควรดูก่อนว่า แต่ละรายการ นั้นเป็นอย่างไร และเพื่อให้ ป้องกันการสับสน หรือ พิมพ์(ภาษาไทย) ไม่ถูกต้อง จึงเหลีกเลี่ยงโดยการเรียก คำ(ภาษาอังกฤษ) แทน
หมวดราคาสินค้า (https://agriapi.nabc.go.th/home/production-api)
รายวัน
ขอให้ใช้ หรือ เลือก list รายการจาก show_daily_categories() และ show_daily_products()
รายสัปดาห์ และ รายเดือน
ขอให้ใช้ list รายการจาก show_weekly_categories() และ show_weekly_products()
หมวดดัชนี (https://agriapi.nabc.go.th/home/index-api)
ดัชนีผลผลิตสินค้าเกษตร
- รายเดือน ขอให้ใช้ หรือ เลือกจาก
show_index_products()show_index_categories()และshow_index_groups() - รายไตรมาส และ รายปี ขอให้ใช้ หรือ เลือก จาก
show_quarter_products()ส่วนอื่น เหมือนกันshow_index_categories()และshow_index_groups()
ดัชนีราคาสินค้าเกษตร
ทั้ง รายเดือน รายไตรมาส และ รายปี ขอให้ใช้ หรือ เลือกจาก show_index_products() show_index_categories() และ show_index_groups()
| Code | หมวด |
|---|---|
FISHERY |
หมวดประมง |
LIVESTOCK |
หมวดปศุสัตว์ |
MAJOR_CROP |
หมวดพืชผลสำคัญ |
ตัวอย่างการใช้งานจริง
library(talatThaiR)
library(dplyr)
library(ggplot2)
# วิเคราะห์แนวโน้มราคายางพาราปี 2569
rubber <- get_daily_prices(
category_code = "RUBBER",
start_date = "2026-01-01",
end_date = "2026-03-31"
)
rubber |>
mutate(data_date = as.Date(data_date)) |>
ggplot(aes(x = data_date, y = price, color = product_name)) +
geom_line() +
labs(title = "ราคายางพาราปี 2569", x = "วันที่", y = "ราคา (บาท/กก.)")
# เปรียบเทียบดัชนีผลผลิตปศุสัตว์ รายไตรมาส
livestock_q <- get_production_index_quarter(
category_code = "LIVESTOCK"
)
livestock_q |>
filter(year_th >= 2565) |>
group_by(year_th, quarter) |>
summarise(mean_index = mean(production_index, na.rm = TRUE))หมายเหตุ
Pagination: talatThaiR ได้จัดการ pagination ให้อัตโนมัติ ผู้ใช้ได้รับข้อมูลครบทุก record โดยไม่ต้องระบุ page ใด ๆ และ จะกำหนดจำนวนหน้า จากข้อมูลที่ต้องการ เช่น ระหว่างวันที่ จำนวนหน้าจะถูกคำนวน แล้วนำเข้าไปเรียก เพื่อให้ได้ข้อมูลตามที่ต้องการ
API Key: API ส่วนใหญ่ไม่ต้องการ key แต่หากต้องการสามารถระบุผ่าน parameter api_key ได้ทุก function
ปีพุทธศักราช: parameter year_th ใช้ปี พ.ศ. เช่น 2568 (ไม่ใช่ ค.ศ.)
Rate limiting: talatThaiR จำกัดป้องกันการ request ที่ถี่เกินไป เมื่อผู้ใช้ดึงข้อมูลจำนวนหน้าเป็นจำนวนมาก จะทำการ sleep 0.3 วินาทีระหว่าง page ดังนั้นอาจใช้เวลาสักครู่
