Last active
January 3, 2021 19:07
-
-
Save munichrocker/2ee3d4b6a98a975c95a53f9fa79b7685 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(DatawRappr) | |
# First create a single chart in Datawrapper, style it and download the metadata: | |
metadata_chart <- dw_retrieve_chart_metadata(chart_id, api_key) | |
# drag out the visualize-part and save it to disk: | |
visualize <- metadata_chart[["content"]][["metadata"]][["visualize"]] | |
saveRDS(visualize, "visualize.RData") | |
# create a list that stores each created chart id: | |
result_list <- list() | |
# start a for loop (in this example we're using countries out of a vector of countries): | |
for (country in country_vector) { | |
raw_data_frame %>% | |
filter(countries == country) %>% | |
pull(country_name) -> country_name | |
current_chart <- dw_create_chart(title = paste0("Chart für ", country_name),type = "d3-lines") | |
dw_edit_chart(chart_id = current_chart, visualize = visualize) | |
published_chart <- dw_publish_chart(current_chart, return_object = TRUE) | |
result_list[[country]] <- data.frame(country = country_name, url = published_chart$publicUrl, iframe = published_chart$iframeCode, chart_id = published_chart[["content"]][["data"]][[1]][["id"]]) | |
} | |
# save list to disk for updating the charts: | |
result_df <- dplyr::bind_rows(result_list) | |
saveRDS(result_df, "chart_ids.RData") |
Thanks so much for getting my pseudo-codish code right!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I would suggest revising the script as follows:
published_chart <- dw_publish_chart(current_chart)
intopublished_chart <- dw_publish_chart(current_chart, return_object = TRUE)
as otherwise it returns an empty set.
Another minor edit would be to change
result_list[[i]]
intoresult_list[[country]]
.