R Markdown

Petit test pour avoir tout mon code MineAPI_TW et eviter de le perdre

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

#####(VIDE) n°18_Test RiverDredgebycompany byriver#### response <- GET(“https://amis.mine.gov.tw/api/RiverDredge.aspx?YearMonth=202401”) QualityDetect <- content(response, “text”) %>% fromJSON(flatten = TRUE) print(QualityDetect) df <- as.data.frame(QualityDetect) simple_table <- df[, c(“data.CityName”, “data.CompanyName”, “data.Tele”)] print(simple_table)

#Traduction translated_names <- gtranslate::translate(simple_table[[“data.CompanyName”]], to = “en”) simple_table <- simple_table %>% mutate(data.CompanyName(en) = translated_names) %>% select(1:2, data.CompanyName(en), 3:length(.))

####N°2 GravelQuarry/county##### response <- GET(“https://amis.mine.gov.tw/api/_CHANGJSON.aspx?Firm=%e5%85%a8%e9%83%a8%e7%b8%a3%e5%b8%82”) QualityDetect <- content(response, “text”) %>% fromJSON(flatten = TRUE)

Convert the data to a data frame

df <- as.data.frame(QualityDetect)

####n°4 Sand&GravelPrudction#####

library(httr)
library(xml2)
library(dplyr)
## 
## Attachement du package : 'dplyr'
## Les objets suivants sont masqués depuis 'package:stats':
## 
##     filter, lag
## Les objets suivants sont masqués depuis 'package:base':
## 
##     intersect, setdiff, setequal, union
library(xml2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.1     ✔ readr     2.1.5
## ✔ ggplot2   4.0.0     ✔ stringr   1.5.2
## ✔ lubridate 1.9.4     ✔ tibble    3.3.0
## ✔ purrr     1.1.0     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
url <- "https://amis.mine.gov.tw/api/CODELIST.aspx?S=1"
response <- GET(url)

# Parse the XML data
xml_data <- read_xml(content(response, "text"))
## No encoding supplied: defaulting to UTF-8.
# Extract the data for each column
years <- xml_data %>%
  xml_find_all("//Years") %>%
  xml_text() %>%
  as.numeric()

months <- xml_data %>%
  xml_find_all("//Months") %>%
  xml_text() %>%
  as.numeric()

sand <- xml_data %>%
  xml_find_all("//sand") %>%
  xml_text() %>%
  as.numeric()

stone <- xml_data %>%
  xml_find_all("//stone") %>%
  xml_text() %>%
  as.numeric()

grading <- xml_data %>%
  xml_find_all("//grading") %>%
  xml_text() %>%
  as.numeric()

other <- xml_data %>%
  xml_find_all("//other") %>%
  xml_text() %>%
  as.numeric()

total <- xml_data %>%
  xml_find_all("//total") %>%
  xml_text() %>%
  as.numeric()

# Combine the columns into a data frame
TOTAL_PROD <- data.frame(years, months, sand, stone, grading, other, total)
# Convert YearMonth to Date format
TOTAL_PROD$YearMonth <- sprintf("%s-%s", years, months)

TOTAL_PROD %>%
  ggplot(aes(
    y = total,
    x = YearMonth
  )) +
  labs(
    title = "Volume d'extraction entre 2021 et 2023 par rivière",
    x = "YearMonth",
    y = "total",
    fill = "Rivières"
  ) +
  geom_col() +
  scale_fill_viridis_d() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    title = element_text(face = "bold.italic")
  ) +
  guides(fill = guide_legend(ncol = 2))
## Ignoring unknown labels:
## • fill : "Rivières"

####n°6 Sand&GravelInventory######## library(httr) library(xml2) library(dplyr) library(xml2)

url <- “https://amis.mine.gov.tw/api/CODELIST.aspx?S=3” response <- GET(url)

Parse the XML data

xml_data <- read_xml(content(response, “text”))

Extract the data for each column

years <- xml_data %>% xml_find_all(“//Years”) %>% xml_text() %>% as.numeric()

months <- xml_data %>% xml_find_all(“//Months”) %>% xml_text() %>% as.numeric()

fprod <- xml_data %>% xml_find_all(“//sstock”) %>% xml_text() %>% as.numeric()

raw <- xml_data %>% xml_find_all(“//mtock”) %>% xml_text() %>% as.numeric()

total<- xml_data %>% xml_find_all(“//total”) %>% xml_text() %>% as.numeric()

Combine the columns into a data frame

TOTAL_STOCK <- data.frame(years, months, fprod, raw, total) # Convert YearMonth to Date format years <- years+1911 TOTAL_STOCK$YearMonth <- sprintf(“%s-%s”, years, months)

p<- TOTAL_STOCK%>% ggplot(aes( y = total, x = YearMonth )) + labs( title = “Stocks de granulats à l’échelle du pays”, x = “YearMonth”, y = “total” ) + geom_col() + scale_fill_viridis_d() + theme( axis.text.x = element_text(angle = 45, hjust = 1), title = element_text(face = “bold.italic”) ) + guides(fill = guide_legend(ncol = 2))

ggplotly(p) ####n°7 Imported Volume#### library(httr) library(jsonlite) library(dplyr) library(lubridate) library(tidyverse)

#⚠️Avant 2006 pas de variation mensuelle, est-ce que ça veut #dire qu’ils avaient des valeurs annuelles qu’ils ont divisées par 12 ?

Define the years and months you want to query

years <- c(1995:2023) # Example years months <- sprintf(“%02d”, 1:12) # All months from 01 to 12

Initialize an empty list to store the data frames

all_data <- list()

Loop over each year and month

for (year in years) { for (month in months) { # Construct the URL for the current year and month url <- sprintf(“https://amis.mine.gov.tw/api/_CHANGJSON.aspx?SandStoneSaleYearMonth=%s%s”, year, month)

# Make the GET request
response <- GET(url)

# Parse the JSON content if the request was successful
if (status_code(response) == 200) {
  QualityDetect <- content(response, "text", encoding = "UTF-8") %>%
    fromJSON(flatten = TRUE)
  
  # Convert the data to a data frame and add a column for the date
  df <- as.data.frame(QualityDetect)
  df$YearMonth <- sprintf("%s-%s", year, month)
  
  # Append the data frame to the list
  all_data[[length(all_data) + 1]] <- df
} else {
  warning(sprintf("Failed to retrieve data for %s-%s", year, month))
}

} }

Combine all data frames into a single data frame

combined_data <- bind_rows(all_data)

Group by YearMonth and summarize the data

summarized_data <- combined_data %>% group_by(YearMonth) %>% summarize( Years = first(Years), Months = first(Months), North = sum(as.numeric(North), na.rm = TRUE), Center = sum(as.numeric(Center), na.rm = TRUE), South = sum(as.numeric(South), na.rm = TRUE), East = sum(as.numeric(East), na.rm = TRUE), Total = sum(as.numeric(Total), na.rm = TRUE) ) # Convert YearMonth to Date format summarized_data <- summarized_data %>% mutate(YearMonth = as.Date(parse_date_time(YearMonth, c(“ym”))))

Reshape the data from wide to long format

long_data <- summarized_data %>% pivot_longer(cols = c(“North”, “Center”, “South”, “East”,“Total”), names_to = “Region”, values_to = “Value”)

#Plot p <- long_data %>% #filter(Years== 110) %>% ggplot(aes(x = YearMonth, y = Value, color = Region, group = Region)) + geom_line() + scale_x_date() + labs(title = “Importation de sable par région”, x = “Year-Month”, y = “Value”, color = “Region”) + theme_minimal()

####n°8 TestSandPrice NORTH/SOUTH/CENTRAL/EAST####

library(httr) library(jsonlite) library(dplyr) library(lubridate) library(tidyverse) library(wesanderson)

Define the years and months you want to query

years <- c(1990:2024) # Example years months <- sprintf(“%02d”, 1:12) # All months from 01 to 12

Initialize an empty list to store the data frames

all_data <- list()

Loop over each year and month

for (year in years) { for (month in months) { # Construct the URL for the current year and month url <- sprintf(“https://amis.mine.gov.tw/api/_CHANGJSON.aspx?SandCostYearMonth=%s%s”, year, month)

# Make the GET request
response <- GET(url)

# Parse the JSON content if the request was successful
if (status_code(response) == 200) {
  QualityDetect <- content(response, "text", encoding = "UTF-8") %>%
    fromJSON(flatten = TRUE)
  
  # Convert the data to a data frame and add a column for the date
  df <- as.data.frame(QualityDetect)
  df$YearMonth <- sprintf("%s-%s", year, month)
  
  # Append the data frame to the list
  all_data[[length(all_data) + 1]] <- df
} else {
  warning(sprintf("Failed to retrieve data for %s-%s", year, month))
}

} }

Combine all data frames into a single data frame

combined_PRICE <- bind_rows(all_data)

Convert YearMonth to Date format + SandPrice to number

combined_PRICE <- combined_PRICE %>% mutate(YearMonth = as.Date(parse_date_time(YearMonth, c(“ym”)))) %>% mutate(SandPrice = as.numeric(SandPrice))

Filter out rows where the SandPrice is 0

combined_PRICE <- combined_PRICE %>% filter(SandPrice != 0)

#Plot combined_PRICE %>% #filter(Area %in% c(“North”, “Central”, “South”, “East”)) %>% ggplot(aes(x = YearMonth, y = SandPrice, color = Area)) + geom_line() + #geom_point()+ labs(title = “Coût de la tonne de sable à l’achat par région”, x = “Date”, y = “NTD/tonne”, color = “Area”) + theme(axis.text.x = element_text(angle = 45, hjust = 1), title = element_text(face=“bold.italic” )) + guides(fill = guide_legend(ncol = 2))

####n°10 TestSandPrice PER COUNTY####

library(httr) library(jsonlite) library(dplyr) library(lubridate) library(tidyverse) library(wesanderson) library(plotly)

Define the years and months you want to query

years <- c(1990:2024) # Example years months <- sprintf(“%02d”, 1:12) # All months from 01 to 12

Initialize an empty list to store the data frames

all_data <- list()

Loop over each year and month

for (year in years) { for (month in months) { # Construct the URL for the current year and month url <- sprintf(“https://amis.mine.gov.tw/api/_CHANGJSON.aspx?SandCostCityYearMonth=%s%s”, year, month)

# Make the GET request
response <- GET(url)

# Parse the JSON content if the request was successful
if (status_code(response) == 200) {
  QualityDetect <- content(response, "text", encoding = "UTF-8") %>%
    fromJSON(flatten = TRUE)
  
  # Convert the data to a data frame and add a column for the date
  df <- as.data.frame(QualityDetect)
  df$YearMonth <- sprintf("%s-%s", year, month)
  
  # Append the data frame to the list
  all_data[[length(all_data) + 1]] <- df
} else {
  warning(sprintf("Failed to retrieve data for %s-%s", year, month))
}

} }

Combine all data frames into a single data frame

combined_PRICE <- bind_rows(all_data)

Convert YearMonth to Date format + SandPrice to number

combined_PRICE <- combined_PRICE %>% mutate(YearMonth = as.Date(parse_date_time(YearMonth, c(“ym”)))) %>% mutate(SandPrice = as.numeric(SandPrice))

Filter out rows where the SandPrice is 0

combined_PRICE <- combined_PRICE %>% filter(SandPrice != 0)

#Plot p <- combined_PRICE %>% filter(CityName %in% c(“台北市”, “高雄市”, “花蓮縣”,“台中市”,“新北市”)) %>% filter(Years %in% c(“105”)) %>% ggplot(aes(x = YearMonth, y = SandPrice, color = CityName)) + geom_line() + #geom_point()+ labs(title = “Coût de la tonne de sable à l’achat par région”, x = “Date”, y = “NTD/tonne”, color = “City Name”) + scale_color_manual(values=wes_palette(“Darjeeling1”))+ theme(axis.text.x = element_text(angle = 45, hjust = 1), title = element_text(face=“bold.italic” )) + guides(fill = guide_legend(ncol = 2))

ggplotly(p)

#Nouveau type de plot : plot année-type avec évolutions par villes p <- combined_PRICE %>% #filter(CityName %in% c(“台北市”, “高雄市”, “花蓮縣”, “台中市”, “新北市”)) %>% ggplot(aes(x = month(YearMonth, label = TRUE), y = SandPrice, color = as.numeric(year(YearMonth)), # Couleur basée sur l’année (numérique) group = interaction(CityName, year(YearMonth)))) + geom_line() + scale_color_gradientn( colors = RColorBrewer::brewer.pal(n = 9, name = “Blues”) # Palette dégradée ) + labs(title = “Coût de la tonne de sable à l’achat par région sur une année”, x = “Mois”, y = “NTD/tonne”, color = “Année”) + theme(axis.text.x = element_text(angle = 45, hjust = 1), title = element_text(face = “bold.italic”)) + facet_wrap(~ CityName, ncol = 4) # Optionnel : afficher chaque année dans un panneau séparé

ggplotly(p) ###(OK)##n°15_Test Main Companies##### response <- GET(“https://amis.mine.gov.tw/api/QUALITYDETECT.aspx”) QualityDetect <- content(response, “text”) %>% fromJSON(flatten = TRUE) print(QualityDetect) df <- as.data.frame(QualityDetect) simple_table <- df[, c(“data.CityName”, “data.CompanyName”, “data.Tele”)] print(simple_table)

#Traduction translated_names <- gtranslate::translate(simple_table[[“data.CompanyName”]], to = “en”) simple_table <- simple_table %>% mutate(data.CompanyName(en) = translated_names) %>% select(1:2, data.CompanyName(en), 3:length(.))

print(simple_table)

####TestLoopSALES##### library(httr) library(jsonlite) library(dplyr) library(lubridate) library(tidyverse)

#⚠️Avant 2006 pas de variation mensuelle, est-ce que ça veut #dire qu’ils avaient des valeurs annuelles qu’ils ont divisées par 12 ?

Define the years and months you want to query

years <- c(1995:2023) # Example years months <- sprintf(“%02d”, 1:12) # All months from 01 to 12

Initialize an empty list to store the data frames

all_data <- list()

Loop over each year and month

for (year in years) { for (month in months) { # Construct the URL for the current year and month url <- sprintf(“https://amis.mine.gov.tw/api/_CHANGJSON.aspx?SandStoneSaleYearMonth=%s%s”, year, month)

# Make the GET request
response <- GET(url)

# Parse the JSON content if the request was successful
if (status_code(response) == 200) {
  QualityDetect <- content(response, "text", encoding = "UTF-8") %>%
    fromJSON(flatten = TRUE)
  
  # Convert the data to a data frame and add a column for the date
  df <- as.data.frame(QualityDetect)
  df$YearMonth <- sprintf("%s-%s", year, month)
  
  # Append the data frame to the list
  all_data[[length(all_data) + 1]] <- df
} else {
  warning(sprintf("Failed to retrieve data for %s-%s", year, month))
}

} }

Combine all data frames into a single data frame

combined_data <- bind_rows(all_data)

Group by YearMonth and summarize the data

summarized_data <- combined_data %>% group_by(YearMonth) %>% summarize( Years = first(Years), Months = first(Months), North = sum(as.numeric(North), na.rm = TRUE), Center = sum(as.numeric(Center), na.rm = TRUE), South = sum(as.numeric(South), na.rm = TRUE), East = sum(as.numeric(East), na.rm = TRUE), Total = sum(as.numeric(Total), na.rm = TRUE) ) # Convert YearMonth to Date format summarized_data <- summarized_data %>% mutate(YearMonth = as.Date(parse_date_time(YearMonth, c(“ym”))))

Reshape the data from wide to long format

long_data <- summarized_data %>% pivot_longer(cols = c(“North”, “Center”, “South”, “East”), names_to = “Region”, values_to = “Value”)

#Plot long_data %>% filter(Years== 110) %>% ggplot(aes(x = YearMonth, y = Value, color = Region, group = Region)) + geom_line() + scale_x_date() + labs(title = “Achat de sable par région”, x = “Year-Month”, y = “Value”, color = “Region”) + theme_minimal()

####n°18 TestLoop RIVERDREDGE#####