I have a dataset of Key Performance Indicator (KPI) and for each KPI I have a current level of achivement and 2 targets : Target1 and Target2.

How can I automatically generate one graphic for each achievement as in the file attached here.

As I have many KPIs I would like to generate my graphics in a batch process


1 Answer 1


There are multiple ways you can achieve this. I guess the easiest would be to create a function that saves a bar chart for your KPI to a file:

save_KPI_plot <- function(fn, kpi_data) {
    png(paste0(fn, ".png"))
    # Plot code

You can call the function either as follows,

save_KPI_plot("kpi_1", kpi_1_data)

or store your file names in one list and your data in another and loop over both:

kpi_fns <- list("filename_1", "filename_2")
kpi_data <- list(kpi_1_data, kpi_2_data)
for (i in seq_along(kpi_fns)) {
    save_KPI_plot(kpi_fns[i], kpi_data[i])

If you prefer to have a different image format you can change png() to bmp() or jpeg().


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.