title: “Primary Health Care Centers in Tunisia” author: “Dr. Shadi Henchiri” output: html_document —

# Install and load required packages
required_packages <- c("leaflet", "jsonlite", "htmlwidgets", "htmltools")
for (pkg in required_packages) {
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg)
    library(pkg, character.only = TRUE)
  }
}
## Loading required package: leaflet
## Warning: package 'leaflet' was built under R version 4.3.3
## Loading required package: jsonlite
## Loading required package: htmlwidgets
## Loading required package: htmltools
# Load required libraries
library(leaflet)
library(jsonlite)
library(htmlwidgets)
library(htmltools)

# Define the file path to the JSON file and logo
file_path <- "C:/Users/shenshiri/Desktop/IM/Liste des CSB à traduire.json"
logo_path <- "C:/Users/shenshiri/Desktop/IM/MHD_LOGO_BLUE.png"  # Path to your logo

# Check if the JSON file exists
if (!file.exists(file_path)) {
  stop("File does not exist. Please check the file name and location.")
} else {
  print("File exists.")
}
## [1] "File exists."
# Load the JSON data and convert to DataFrame
data <- fromJSON(file_path)
df <- as.data.frame(data$`Liste des CSB`)

# Print the column names of the DataFrame
print(colnames(df))
## [1] "OBJECTID"    "id"          "Gouvernorat" "Delegation"  "Centre"     
## [6] "Latitude"    "Longitude"   "Altitude"    "type"
# Filter for specific Governorates
gouvernorat_filter <- c('Tunis', 'Manouba', 'Ariana', 'Ben Arous', 'Sfax', 'Medenine')
filtered_df <- df[df$Gouvernorat %in% gouvernorat_filter, ]

# Check if the filtered DataFrame is empty
if (nrow(filtered_df) == 0) {
  stop("No centers found for the specified Gouvernorat.")
}
# Create a leaflet map centered on Tunisia
csb_map <- leaflet() %>%
  addTiles() %>%
  setView(lng = 9.5375, lat = 33.8869, zoom = 6)

# Add markers to the map for each center in the filtered DataFrame
for (i in 1:nrow(filtered_df)) {
  csb_map <- csb_map %>%
    addMarkers(
      lng = as.numeric(filtered_df[i, 'Longitude']),
      lat = as.numeric(filtered_df[i, 'Latitude']),
      popup = filtered_df[i, 'Centre']
    )
}

# Display the map
csb_map
# Save the map as an HTML file
html_file <- "csb_tunisia_map_filtered_44.html"
saveWidget(csb_map, file = html_file, selfcontained = TRUE)

# Function to add a custom header and logo to the HTML file
add_custom_header <- function(html_file, logo_path) {
  # Read the content of the saved HTML file
  content <- readLines(html_file, encoding = "UTF-8")
  
  # Create the custom header HTML with the logo
  header_html <- paste0('
    <div style="background-color: #ffffff; padding: 10px;">
        <img src="', logo_path, '" alt="Logo" style="float: left; width: 200px; height: auto;">
        <h1 style="color: #0033cc; text-align: center; padding: 10px; font-weight: bold;">
            Primary Health Care Centers locations in Tunis and Sfax
        </h1>
    </div>
  ')
  
  # Insert the custom header before the <body> tag in the HTML file
  new_content <- gsub('<body>', paste0('<body>', header_html), content, fixed = TRUE)
  
  # Write the modified content back into the HTML file
  writeLines(new_content, con = html_file, useBytes = TRUE)
  
  print(paste("Custom header and larger logo added to", html_file))
}
# Add the custom header with the logo to the HTML file
add_custom_header(html_file, logo_path)
## [1] "Custom header and larger logo added to csb_tunisia_map_filtered_44.html"