Skip to content

Commit e0566b6

Browse files
committed
first commit
0 parents  commit e0566b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+9685
-0
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
^LICENSE\.md$

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
data filter=lfs diff=lfs merge=lfs -text
2+
Stranieri[[:space:]]in[[:space:]]Italia/data filter=lfs diff=lfs merge=lfs -text
3+
*.rds filter=lfs diff=lfs merge=lfs -text
4+
*.csv filter=lfs diff=lfs merge=lfs -text
5+
*.zip filter=lfs diff=lfs merge=lfs -text
6+
DCIS_POPSTRCIT1[[:space:]]-[[:space:]]Stranieri[[:space:]]residenti[[:space:]]al[[:space:]][[:space:]]gennaio[[:space:]]-[[:space:]]Cittadinanza[[:space:]]-[[:space:]]intero[[:space:]]ds.csv filter=lfs diff=lfs merge=lfs -text

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
data/.*
6+
Stranieri in Italia/data/residenti_in_italia.rds
7+
Stranieri in Italia/data/residenti_in_italia_small.rds

Istat R.Rproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Version: 1.0
2+
ProjectId: b990bc77-f86f-4412-bd91-002b208e97ce
3+
4+
RestoreWorkspace: Default
5+
SaveWorkspace: Default
6+
AlwaysSaveHistory: Default
7+
8+
EnableCodeIndexing: Yes
9+
UseSpacesForTab: Yes
10+
NumSpacesForTab: 2
11+
Encoding: UTF-8
12+
13+
RnwWeave: Sweave
14+
LaTeX: pdfLaTeX

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2025 Erik De Luca
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Stranieri in Italia - Shiny App
2+
3+
4+
# Stranieri in Italia
5+
6+
Welcome to the **Stranieri in Italia** Shiny Application, designed to
7+
visualize and analyze ISTAT data on residents in Italy, categorized by
8+
citizenship and area of residence.
9+
10+
## Overview
11+
12+
This application provides interactive insights into the distribution and
13+
demographics of foreign residents in Italy. The app is structured into
14+
three main sections:
15+
16+
1. **Statistics by Italian Location**:
17+
- View the most populated cities, regions, and municipalities by
18+
foreign residents.
19+
- Understand the demographics of these areas and from which
20+
countries these residents originate.
21+
2. **Statistics by Citizenship**:
22+
- Analyze data for specific cities or regions and discover the
23+
countries of origin of their residents.
24+
- Gain insights into the foreign demographics of selected Italian
25+
locations.
26+
3. **Reverse Lookup by Country**:
27+
- Given a country, identify where its citizens reside in Italy.
28+
- View this information in terms of absolute numbers and proportions
29+
relative to the local population.
30+
31+
## Key Features
32+
33+
- **Interactive Visualizations**: Utilize plots and maps to explore and
34+
understand data.
35+
- **Dynamic Input Options**: Select regions, municipalities, or
36+
countries of interest using interactive widgets.
37+
- **Proportional Analysis**: Toggle between absolute numbers and
38+
population proportions to gain different perspectives.
39+
40+
## Installation and Usage
41+
42+
To run this application locally, ensure you have R and the necessary
43+
libraries installed. Follow these steps:
44+
45+
1. Clone the repository:
46+
47+
``` bash
48+
git clone https://github.com/erikdeluca/ISTAT-R.git
49+
cd ISTAT-R
50+
```
51+
52+
2. Install the required R packages:
53+
54+
``` r
55+
install.packages(c("shiny", "shinydashboard", "shinycssloaders", "shinyWidgets", "tidyverse", "bsicons", "sass", "leaflet", "sf", "maps", "plotly", "countrycode", "eurostat", "janitor"))
56+
```
57+
58+
3. Run the Shiny app:
59+
60+
<!-- -->
61+
62+
shiny::runApp()
63+
64+
## Contributing
65+
66+
Contributions are welcome! Feel free to open issues or submit pull
67+
requests for improvements or bug fixes.
68+
69+
## License
70+
71+
This project is licensed under the MIT License. See the LICENSE file for
72+
details.
73+
74+
## Acknowledgements
75+
76+
This app utilizes the ISTAT data on residents in Italy and various R
77+
packages for data manipulation and visualization.

README.qmd

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: "Stranieri in Italia - Shiny App"
3+
format: gfm
4+
---
5+
6+
Welcome to the **Stranieri in Italia** Shiny Application, designed to visualize and analyze ISTAT data on residents in Italy, categorized by citizenship and area of residence.
7+
8+
## Overview
9+
10+
This application provides interactive insights into the distribution and demographics of foreign residents in Italy. The app is structured into three main sections:
11+
12+
1. **Statistics by Italian Location**:
13+
- View the most populated cities, regions, and municipalities by foreign residents.
14+
- Understand the demographics of these areas and from which countries these residents originate.
15+
2. **Statistics by Citizenship**:
16+
- Analyze data for specific cities or regions and discover the countries of origin of their residents.
17+
- Gain insights into the foreign demographics of selected Italian locations.
18+
3. **Reverse Lookup by Country**:
19+
- Given a country, identify where its citizens reside in Italy.
20+
- View this information in terms of absolute numbers and proportions relative to the local population.
21+
22+
## Key Features
23+
24+
- **Interactive Visualizations**: Utilize plots and maps to explore and understand data.
25+
- **Dynamic Input Options**: Select regions, municipalities, or countries of interest using interactive widgets.
26+
- **Proportional Analysis**: Toggle between absolute numbers and population proportions to gain different perspectives.
27+
28+
## Installation and Usage
29+
30+
To run this application locally, ensure you have R and the necessary libraries installed. Follow these steps:
31+
32+
1. Clone the repository:
33+
34+
``` bash
35+
git clone https://github.com/erikdeluca/ISTAT-R.git
36+
cd ISTAT-R
37+
```
38+
39+
2. Install the required R packages:
40+
41+
``` r
42+
install.packages(c("shiny", "shinydashboard", "shinycssloaders", "shinyWidgets", "tidyverse", "bsicons", "sass", "leaflet", "sf", "maps", "plotly", "countrycode", "eurostat", "janitor"))
43+
```
44+
45+
3. Run the Shiny app:
46+
47+
```
48+
shiny::runApp()
49+
```
50+
51+
## Contributing
52+
53+
Contributions are welcome! Feel free to open issues or submit pull requests for improvements or bug fixes.
54+
55+
## License
56+
57+
This project is licensed under the MIT License. See the LICENSE file for details.
58+
59+
## Acknowledgements
60+
61+
This app utilizes the ISTAT data on residents in Italy and various R packages for data manipulation and visualization.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Apolidi nelle provincie italiane
2+
3+
df |>
4+
filter(iso == "999",
5+
time == 2022,
6+
sesso == "totale",
7+
value >10,
8+
str_detect(itter107, "ITC[:digit:]{2}")
9+
)
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
library(countrycode)
2+
library(eurostat)
3+
library(tidyverse)
4+
library(sf)
5+
library(maps)
6+
7+
# unzip the data
8+
# unzip("data/residenti_stranieri_in_italia.zip", exdir = "data")
9+
# unzip("data/residenti_in_italia.zip", exdir = "data")
10+
11+
# rename the files
12+
# file.rename("data/DCIS_POPRES1 - Popolazione residente al 1° gennaio - intero ds.csv", "data/residenti_in_italia.csv")
13+
# file.rename("data/DCIS_POPSTRCIT1 - Stranieri residenti al 1° gennaio - Cittadinanza - intero ds.csv", "data/residenti_stranieri_in_italia.csv")
14+
#
15+
16+
# load the data -----------------------------------------------------------------------------------------------
17+
18+
# stranieri in italia
19+
read_delim("data/residenti_stranieri_in_italia.csv", delim = "|") |>
20+
tibble() |>
21+
janitor::clean_names() |>
22+
janitor::remove_empty(c("rows", "cols")) |>
23+
janitor::remove_constant() -> df_stranieri
24+
# mutate(across(where(is.character), as.factor)) |>
25+
# filter(value > 30)
26+
27+
# residenti in italia
28+
read_delim("data/residenti_in_italia.csv", delim = ",") |>
29+
tibble() |>
30+
janitor::clean_names() |>
31+
janitor::remove_empty(c("rows", "cols")) |>
32+
janitor::remove_constant() |>
33+
# mutate(across(where(is.character), as.factor)) |>
34+
# filter(value > 30) |>
35+
select(itter107, time, sesso, value) -> df_residenti
36+
37+
# join the two datasets
38+
left_join(
39+
df_stranieri,
40+
df_residenti,
41+
by = c("itter107", "time", "sesso"),
42+
suffix = c("", "_residenti")
43+
) |>
44+
mutate(
45+
ratio = value / value_residenti
46+
) |>
47+
select(itter107, territorio, sesso, iso, paese_di_cittadinanza, time, value, ratio) -> df
48+
49+
# read the continents and the macroregions
50+
df |>
51+
distinct(iso, nome = paese_di_cittadinanza) |>
52+
mutate(
53+
iso = case_when(
54+
nome == "Kosovo" ~ "XK",
55+
nome == "Namibia" ~ "NA",
56+
nome == "Regno unito" ~ "GB",
57+
TRUE ~ iso
58+
),
59+
category = case_when(
60+
iso %in% c("EU28", "WORLD", "999") ~ "macroregion", # 999 apolide
61+
str_detect(iso, "_") ~ "macroregion",
62+
TRUE ~ "country"
63+
),
64+
name = countrycode(iso, origin = "iso2c", destination = "country.name.en"),
65+
name = ifelse(is.na(name), nome, name),
66+
name_map = case_when(
67+
name == "United Kingdom" ~ "UK",
68+
name == "United States" ~ "USA",
69+
name == "Czechia" ~ "Czech Republic",
70+
name == "Bosnia & Herzegovina" ~ "Bosnia and Herzegovina",
71+
name == "Côte d’Ivoire" ~ "Ivory Coast",
72+
name == "Congo - Kinshasa" ~ "Democratic Republic of the Congo",
73+
name == "Congo - Brazzaville" ~ "Republic of Congo",
74+
name == "Myanmar (Burma)" ~ "Myanmar",
75+
name == "Palestinian Territories" ~ "Palestine",
76+
TRUE ~ name
77+
),
78+
emoji = countrycode(iso, origin = "iso2c", destination = "unicode.symbol"),
79+
continent = countrycode(iso, origin = "iso2c", destination = "continent"),
80+
region = countrycode(iso, origin = "iso2c", destination = "region"),
81+
region23 = countrycode(iso, origin = "iso2c", destination = "region23"),
82+
iso = case_when(
83+
iso == "GB" ~ "UK",
84+
TRUE ~ iso
85+
)
86+
) -> mapping_world_locations
87+
88+
mapping_italian_locations <- df |>
89+
distinct(code = itter107) |>
90+
left_join(df |> select(itter107, name = territorio), by = c("code" = "itter107")) |>
91+
distinct(code, name) |>
92+
mutate(
93+
category = case_when(
94+
str_detect(code, "^[A-Z]{3}$") ~ "macroregione",
95+
str_detect(code, "^[A-Z]{3}[0-9]{1}$") ~ "regione",
96+
str_detect(code, "^[A-Z]{3}[0-9]{2}$") ~ "provincia",
97+
str_detect(code, "^[0-9]{6}$") ~ "comune",
98+
)
99+
) |>
100+
filter(!is.na(name))
101+
102+
# MAPS -----------------------------------------------------------------------------------------------
103+
# Get the borders of the world countries
104+
world_borders <- map_data("world", exact = F)
105+
106+
world_borders <- st_as_sf(world_borders, coords = c("long", "lat"), crs = 4326, remove = FALSE) %>%
107+
arrange(group, order) %>% # Assicurati che i punti siano nell'ordine corretto
108+
group_by(region, group) %>%
109+
summarise(geometry = st_combine(geometry), .groups = 'drop') %>%
110+
st_cast("POLYGON")
111+
112+
world_borders <- st_cast(world_borders, "MULTIPOLYGON")
113+
114+
italian_borders <- get_eurostat_geospatial(resolution = 60) |>
115+
filter(CNTR_CODE %in% c("IT"))
116+
117+
118+
## get the countries that are not in the world map
119+
mapping_world_locations |>
120+
left_join(df |> summarise(value = max(value), .by = paese_di_cittadinanza), by = c("nome" = "paese_di_cittadinanza")) |>
121+
anti_join(map_data("world", exact = F), by = c("name_map" = "region")) |>
122+
filter(category == "country") |>
123+
arrange(desc(value))
124+
125+
# map_data("world", exact = F) |>
126+
# distinct(region) |>
127+
# filter(!region %in% mapping_world_locations$name_map) |>
128+
# arrange(region)
129+
130+
# resize the df to work in shinyapp.com and github.com
131+
df |>
132+
filter(
133+
# !(ratio < 1e-8 & territorio != "Italia"),
134+
value > 5
135+
) -> df_small
136+
137+
138+
write_rds(df, "Stranieri in Italia/data/residenti_in_italia.rds")
139+
write_rds(df_small, "Stranieri in Italia/data/residenti_in_italia_small.rds")
140+
write_rds(italian_borders, "Stranieri in Italia/data/italian_borders.rds")
141+
write_rds(world_borders, "Stranieri in Italia/data/world_borders.rds")
142+
write_rds(mapping_world_locations, "Stranieri in Italia/data/mapping_world_locations.rds")
143+
write_rds(mapping_italian_locations, "Stranieri in Italia/data/mapping_italian_locations.rds")
144+
145+

0 commit comments

Comments
 (0)