# =========================================================

# 1. 加載必要套件 (新增 htmltools 以確保下拉選單運作)

# =========================================================

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.0     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.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
library(sf)
## Warning: package 'sf' was built under R version 4.5.3
## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
library(tigris)
## Warning: package 'tigris' was built under R version 4.5.3
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(plotly)
## Warning: package 'plotly' was built under R version 4.5.3
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(htmltools)
## Warning: package 'htmltools' was built under R version 4.5.3
options(tigris_use_cache = TRUE)



# =========================================================

# 2. 下載與整理地圖資料

# =========================================================

us_states <- states(cb = TRUE, resolution = "20m") %>%
  
  filter(!NAME %in% c("Alaska", "Hawaii", "Puerto Rico", "Guam", 
                      
                      "American Samoa", "Virgin Islands", 
                      
                      "Commonwealth of the Northern Mariana Islands")) %>%
  
  st_transform(4326)
## Retrieving data for the year 2024
# 各州詳細資訊

state_details <- list(
  "Tennessee" = "Enacted legislation allowing internationally trained physicians to practice without U.S. residency, effective July 2024.",
  "Florida" = "Implemented a law in March 2024 permitting foreign-trained doctors to obtain licensure without U.S. residency, provided they meet specific criteria. Has recently implemented legislative measures to facilitate the integration of foreign-trained physicians into its healthcare system, aiming to address the growing demand for medical professionals. In early 2024, Governor Ron DeSantis signed the 'Live Healthy' initiative, which includes provisions to streamline the licensure process for internationally trained doctors. Under this new law, foreign-trained physicians who meet specific criteria can obtain a medical license in Florida without completing a U.S. residency program.",
  "Virginia" = "Passed legislation in April 2024 enabling internationally trained physicians to practice under provisional licensure, leading to full licensure after meeting certain requirements.",
  "Wisconsin" = "Approved a law in March 2024 creating a pathway for foreign-trained physicians to practice without repeating residency, effective early 2025.",
  "Illinois" = "Legislation effective January 2025 allows internationally trained physicians to practice under provisional licensure, with full licensure attainable after supervised practice.",
  "Arizona" = "Set to implement a law in January 2025 granting provisional licenses to internationally trained physicians, requiring supervised practice in rural areas for four years before full license.",
  "Arkansas" = "From 2024, allows internationally trained physicians to achieve full licensure after two years of practice under an academic license.",
  "Colorado" = "Offers a re-entry license for internationally trained physicians who meet board evaluations and complete educational programs; full licensure details are pending.",
  "Idaho" = "As of July 2024, permits internationally trained physicians to obtain a provisional license after completing supervised clinical experience, with full licensure after additional supervised practice.",
  "Iowa" = "Starting early 2025, allows internationally trained physicians to receive provisional licensure, leading to full licensure after supervised practice.",
  "Missouri" = "Provides an assistant physician license for internationally trained physicians to practice in underserved areas without U.S. residency, given they meet specific criteria.",
  "Washington" = "Offers a limited license for internationally trained physicians nominated by specific state departments or health facilities, valid for two years with one renewal. Enacted SB 5185 on March 23, 2026, with an effective date of June 11, 2026.",
  "Massachusetts" = "Has recently enacted legislation to facilitate the integration of internationally trained physicians into its healthcare system, aiming to address the shortage of primary care providers in underserved areas. On November 20, 2024, Governor Maura Healey signed the Mass Leads Act, which includes provisions that create a new pathway toward full medical licensure for foreign-trained doctors. This legislation removes the requirement for these physicians to repeat residency training in the United States. Instead, eligible internationally trained doctors can obtain a limited license to practice under supervision in participating healthcare facilities, such as federally qualified health centers and community health centers located in physician shortage areas.",
  "New York" = "While it is not a state practice yet, Assembly Bill A.7319/S.7840 of 2025, would create a new New York licensure pathway for internationally trained physicians, including physicians trained or licensed in U.S. territories, so they can help address doctor shortages, especially in underserved and rural communities. Under the bill, eligible physicians could receive a two-year limited permit to practice under the supervision of a licensed physician at approved shortage-area facilities such as community health centers, federally qualified health centers, and hospitals; after successfully completing that supervised period, they could receive a two-year restricted license to practice independently in shortage areas in primary care, psychiatry, or other approved specialties; and after that, they could become eligible for full licensure. The bill also requires implementing regulations and annual reporting on participation, specialty areas, and where these physicians are serving."
)



# 政策分類

policy_data <- tribble(
  
  ~state, ~category,
  
  "Arkansas", "Full pathway", "Florida", "Full pathway", "Idaho", "Full pathway",
  
  "Illinois", "Full pathway", "Indiana", "Full pathway", "Iowa", "Full pathway",
  
  "Kentucky", "Full pathway", "Louisiana", "Full pathway", "Massachusetts", "Full pathway",
  
  "Minnesota", "Full pathway", "Nevada", "Full pathway", "North Carolina", "Full pathway",
  
  "Oklahoma", "Full pathway", "Oregon", "Full pathway", "Rhode Island", "Full pathway",
  
  "Tennessee", "Full pathway", "Texas", "Full pathway", "Virginia", "Full pathway",
  
  "Washington", "Full pathway", "Wisconsin", "Full pathway", "West Virginia", "Full pathway",
  
  "California", "Limited pathway", "New York", "Limited pathway",
  
  "Arizona", "Pending", "Georgia", "Pending", "Kansas", "Pending",
  
  "Maine", "Pending", "Maryland", "Pending", "Michigan", "Pending",
  
  "Mississippi", "Pending", "Missouri", "Pending", "Nebraska", "Pending",
  
  "New Jersey", "Pending", "Ohio", "Pending", "Pennsylvania", "Pending",
  
  "South Carolina", "Pending", "Vermont", "Pending", "Wyoming", "Pending",
  
  "Alabama", "No action", "Colorado", "No action", "Delaware", "No action",
  
  "Montana", "No action", "New Mexico", "No action", "South Dakota", "No action",
  
  "Utah", "No action", "District of Columbia", "No action"
  
)



map_data <- us_states %>%
  
  left_join(policy_data, by = c("NAME" = "state")) %>%
  
  mutate(
    
    full_detail = map_chr(NAME, ~ ifelse(.x %in% names(state_details), state_details[[.x]], "")),
    
    wrapped_detail = str_replace_all(str_wrap(full_detail, width = 60), "\n", "<br>"),
    
    hover_text = paste0("<b>", NAME, "</b><br>Status: ", category, "<br><br>", ifelse(full_detail == "", "", wrapped_detail))
    
  )



# =========================================================

# 3. 繪製基本圖形

# =========================================================

p <- ggplot(map_data) +
  
  geom_sf(aes(fill = category, text = hover_text), color = "white", size = 0.2) +
  
  scale_fill_manual(
    
    values = c("Full pathway" = "#1b9e77", "Limited pathway" = "#d95f02", "Pending" = "#7570b3", "No action" = "grey85")
    
  ) +
  
  labs(title = "State Pathways for International Medical Graduates (IMGs)", fill = "Policy Status") +
  
  theme_void() + 
  
  theme(plot.title = element_text(size = 16, face = "bold", hjust = 0.5))
## Warning in layer_sf(geom = GeomSf, data = data, mapping = mapping, stat = stat,
## : Ignoring unknown aesthetics: text
# =========================================================

# 4. 轉換為 Plotly (僅保留地圖與上方說明)

# =========================================================

fig <- ggplotly(p, tooltip = "text") %>%
  
  layout(
    
    margin = list(l = 20, r = 20, t = 80, b = 20),
    
    annotations = list(
      
      list(
        
        x = 0.5, y = 1.0, xref = "paper", yref = "paper",
        
        text = paste0("<b>How to read this map.</b> “Full pathway” means the state has enacted a route that can culminate in full medical licensure without repeating a U.S. residency.<br>",
                      
                      "“Limited pathway” means the state allows some form of supervised or restricted practice but does not yet have a fully enacted broad pathway to full licensure in this policy category."),
        
        showarrow = FALSE, align = "left", xanchor = "center", yanchor = "bottom", 
        
        font = list(size = 8, color = "black")
        
      )
      
    )
    
  )



# =========================================================

# 5. 使用 HTML 封裝 (解決重疊問題並實現下拉選單)

# =========================================================

final_output <- tags$div(
  
  style = "font-family: sans-serif; max-width: 1000px; margin: auto; padding: 20px;",
  
  
  
  # 地圖部分
  
  as_widget(fig),
  
  
  
  # 下拉選單部分 (左方與右方並排)
  
  tags$div(
    
    style = "display: flex; justify-content: space-between; margin-top: 20px; gap: 20px;",
    
    
    
    # 左側下拉
    
tags$div(
  
  style = "flex: 1; border: 1px solid #ddd; padding: 10px; border-radius: 5px;",
  
  tags$details(
    
    tags$summary(
      style = "cursor: pointer; font-weight: bold;",
      "Important Classification Note"
    ),
    
    tags$p(
      style = "font-size: 13px; margin-top: 10px;",
      
      "Connecticut and North Dakota do not fit neatly into the four buckets above. ",
      "FSMB lists them as states that previously proposed pathway legislation, ",
      "but not as states with an enacted pathway, a current active pending bill, ",
      "or a 'no action' status."
    )
  )
  ),
    
    
    
    # 右側下拉
    
    tags$div(
      
      style = "flex: 1; border: 1px solid #ddd; padding: 10px; border-radius: 5px;",
      
      tags$details(
        
        tags$summary(style = "cursor: pointer; font-weight: bold;", "Selected Recent Developments"),
        
        tags$ul(style = "font-size: 13px; margin-top: 10px;",
                
                tags$li("Washington: Enacted SB 5185 (Mar 2026), eff. June 2026."),
                
                tags$li("Texas: Accepting applications for provisional license (Feb 2026)."),
                
                tags$li("North Carolina: Accepting applications for employee license (Feb 2026)."),
                
                tags$li("New York: A.7319A/S.7840A remains proposed."))
        
      )
      
    )
    
  ),
  
  
  
  # 底部數據來源
  
  tags$p(style = "text-align: center; color: grey; font-size: 11px; margin-top: 30px; font-style: italic;",
         
         "Source basis: Federation of State Medical Boards state-by-state overview (updated April 2026).")
  
)



# 顯示結果

browsable(final_output)
Important Classification Note

Connecticut and North Dakota do not fit neatly into the four buckets above. FSMB lists them as states that previously proposed pathway legislation, but not as states with an enacted pathway, a current active pending bill, or a 'no action' status.

Selected Recent Developments
  • Washington: Enacted SB 5185 (Mar 2026), eff. June 2026.
  • Texas: Accepting applications for provisional license (Feb 2026).
  • North Carolina: Accepting applications for employee license (Feb 2026).
  • New York: A.7319A/S.7840A remains proposed.

Source basis: Federation of State Medical Boards state-by-state overview (updated April 2026).