Skip to content

Instantly share code, notes, and snippets.

@munichrocker
Last active January 3, 2021 19:07
Show Gist options
  • Save munichrocker/2ee3d4b6a98a975c95a53f9fa79b7685 to your computer and use it in GitHub Desktop.
Save munichrocker/2ee3d4b6a98a975c95a53f9fa79b7685 to your computer and use it in GitHub Desktop.
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")
@fibke
Copy link

fibke commented Jan 3, 2021

Hi, I would suggest revising the script as follows:

published_chart <- dw_publish_chart(current_chart) into published_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]] into result_list[[country]].

@munichrocker
Copy link
Author

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