ECO-BIRTH – Environmental and COmponent factors on BIrth outcomes: Retrospective Trends in Health
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
library(dplyr)library(haven)library(openxlsx)library(gt)library(gtsummary)# load data "C:\Users\andpyk\OneDrive - Region Stockholm\ExposomenIMM\EXPOSOME requests\2025\January\ECO-BIRTH\Birth_coordinates_KI.xlsx"# use opensxlsxdata<-read.xlsx("C:/Users/andpyk/OneDrive - Region Stockholm/ExposomenIMM/EXPOSOME requests/2025/January/ECO-BIRTH/Birth_coordinates_KI.xlsx")# names(data)#"Participant.number" "GIS.coordinate" "GIS.coordinate" "Birth.year" "Birth.month" #rename 2 column as x and 3rd as ynames(data)[2]<-"x"names(data)[3]<-"y"#"Participant.number" "x" "y" "Birth.year" "Birth.month" # get table on missing in each var with gt or gtsummarydata %>%group_by(Birth.year) %>%summarise(n =n(),#coordinates_missing=n(ifelse(is.na(x)|is.na(y),1,0) )coordinates_missing=sum(ifelse(is.na(x)|is.na(y),1,0) ) ) %>%mutate(percent_missing=coordinates_missing/n) %>%gt() %>%tab_header(title ="Missing coordinates by year") %>%fmt_percent(columns = percent_missing) %>%fmt_number(columns =c(n, coordinates_missing),decimals =0)
Missing coordinates by year
Birth.year
n
coordinates_missing
percent_missing
2004
13,476
1,202
8.92%
2005
13,830
1,223
8.84%
2006
14,285
1,205
8.44%
2007
15,185
1,239
8.16%
2008
15,319
1,280
8.36%
2009
15,063
1,248
8.29%
2010
15,172
1,209
7.97%
2011
14,023
1,018
7.26%
2012
13,388
953
7.12%
2013
13,180
903
6.85%
2014
13,034
708
5.43%
2015
13,216
521
3.94%
2016
13,169
438
3.33%
2017
12,738
405
3.18%
2018
13,235
341
2.58%
You can add options to executable code like this
library(sf)library(tmap)library(tmaptools)# data_sf<-st_as_sf(data, coords = c("x","y"), crs = 3301, na.fail=FALSE, remove = F )data_sf<-st_as_sf(data, coords =c("y","x"), crs =3301, na.fail=FALSE, remove = F )tmap_mode("plot") # Use interactive modetm_shape(data_sf %>%head(10000)) +# tm dots with colour and fill by Birth.year gradient of red, legend format year as digit without thousands separatortm_dots(col ="Birth.year", fill ="Birth.year", legend.format =list(text.separator ="-"), ) +tm_basemap(server ="OpenStreetMap")
The echo: false option disables the printing of code (only output is displayed).
# Install packages if not already installed# Install package if needed# install.packages("leaflet")# Load librarylibrary(leaflet)# prepare data_sf for leafletdat_sf<-data_sf %>%head(100)# Create an interactive map with OSMleaflet() %>%addTiles() %>%# OSM backgroundaddMarkers(data_sf %>%head(100) ) # Add markers for the first 100 rows