Chart Generation for 'Microsoft Word', 'Microsoft Excel' and 'Microsoft PowerPoint' Documents

Create native charts for 'Microsoft PowerPoint', 'Microsoft Excel' and 'Microsoft Word' documents. The resulting charts can then be edited and annotated in the host application. It provides functions to create charts and to modify their content and formatting. The chart's underlying data is automatically saved within the 'Word', 'Excel' or 'PowerPoint' file. It extends the 'officer' package, which does not provide native 'Microsoft' chart production.


mschart R package

R buildstatus version testcoverage Active

The mschart package provides a framework for easily creating charts for ‘Microsoft PowerPoint’ presentations, ‘Microsoft Word’ documents and ‘Microsoft Excel’ workbooks. It has to be used with package officer that will produce the charts in new or existing PowerPoint, Word or Excel files. With ‘Microsoft Charts’, the data is integrated into the document and linked to the chart. The result can be edited, annotated and resized. If the data is updated in the document, the chart is also updated.

Chart types

Classical charts (Office 2007+): ms_barchart(), ms_linechart(), ms_areachart(), ms_scatterchart(), ms_piechart(), ms_bubblechart(), ms_radarchart(), ms_stockchart().

chartEx charts (Office 2016+): ms_boxplotchart(), ms_funnelchart(), ms_histogramchart(), ms_paretochart(), ms_sunburstchart(), ms_treemapchart(), ms_waterfallchart().

Classical charts can be combined on the same plot area with ms_chart_combine().

Example

This is a basic example which shows you how to create a bar chart.

library(mschart)

sales <- data.frame(
  quarter = rep(c("Q1", "Q2", "Q3", "Q4"), each = 2),
  revenue = c(12, 9, 15, 11, 18, 14, 21, 17),
  region  = rep(c("EU", "US"), times = 4)
)

bars <- ms_barchart(
  data = sales, x = "quarter", y = "revenue", group = "region"
)

Then use package officer to send the object as a chart.

library(officer)
doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with(doc, value = bars, location = ph_location_fullsize())

print(doc, target = "example.pptx")

The same chart object can be dropped into an Excel sheet with officer::sheet_add_drawing(). By default the chart’s underlying data is written next to the chart automatically.

You can also write the data first with officer::sheet_write_data() and ask the chart to reuse it by passing write_data = FALSE. This is useful when the data should appear at a specific position, or when several charts share the same dataset:

wb <- read_xlsx()
wb <- add_sheet(wb, label = "sales")

# 1. Write the data on the sheet at the desired position.
wb <- sheet_write_data(wb, sheet = "sales", value = bars$data_series,
                       start_col = 1, start_row = 1)

# 2. Add the chart and tell it to reference the data already in place.
wb <- sheet_add_drawing(wb, sheet = "sales", value = bars,
                        write_data = FALSE,
                        start_col = 1, start_row = 1,
                        left = 5, top = 0.5, width = 6, height = 4)

print(wb, target = "example.xlsx")

Use print(your_chart, preview = TRUE) to open the chart in PowerPoint while iterating.

Installation

You can get the development version from GitHub:

devtools::install_github("ardata-fr/mschart")

Or the latest version on CRAN:

install.packages("mschart")

Contributing to the package

Bug reports

When you file a bug report, please spend some time making it easy for me to follow and reproduce. The more time you spend on making the bug report coherent, the more time I can dedicate to investigating the bug as opposed to the bug report.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("mschart")

0.5.1 by David Gohel, 2 months ago


https://ardata-fr.github.io/officeverse/, https://ardata-fr.github.io/mschart/


Report a bug at https://github.com/ardata-fr/mschart/issues


Browse source code at https://github.com/cran/mschart


Authors: David Gohel [aut, cre] , ArData [cph] , YouGov [fnd] , Jan Marvin Garbuszus [ctb] (support for openxlsx2 and combo charts) , Stefan Moog [ctb] (support to set chart and plot area color and border , fix for line style none) , Eli Daniels [ctb] , Marlon Molina [ctb] (added table feature) , Rokas Klydzia [ctb] (custom labels) , David Camposeco [ctb] (chart_data_smooth function) , Dan Joplin [ctb] (fix scatter plot data structure)


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports cellranger, data.table, grDevices, htmltools, officer, scales, stats, utils, writexl, xml2

Suggests doconv, testthat


Imported by saros.

Suggested by officer, openxlsx2, surveytable.


See at CRAN