Skip to content

About

R Package to create appropriately styled outputs (e.g. Excel files) for Statistik Stadt Zürich

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

sszoutputs

CRAN status R-CMD-check GitLab Pipeline

sszoutputs provides customised Excel file from R data frames or tibbles, that match the corporate design of the city of Zurich.

Installation

Install from github:

# install.packages("devtools")
devtools::install_github("StatistikStadtZuerich/sszoutputs")

Install from source: Download the files (by clicking on 'Clone or download' / 'Download Zip'), extract them to any location on your computer, e.g. to your Desktop. Then run: remotes::install_local("<path_to_location>/sszoutputs-main")

Version

Check your version of sszoutputs by running: packageVersion("sszoutputs")

How to use

Excel

write_ssz_excel writes Excel files from a data frame or a tibble. You can either write one Excel spreadsheet from a data frame, or split a data frame into different sheets using a split variable. The Excel file will contain a content sheet with meta information about the data before the data sheet(s).

Simple spreadsheet

To write a simple data frame/tibble on a single spreadsheet:

# define filepath where you want your excel to be stored, .xlsx file extension will be automatically added when not supplied
temp.path <- tempfile()
# some data to write
data(penguins)
# some meta information for the content/title-sheet
meta.info <- c(t1 = "Simple excel", t2 = "car data", t3 = "written with sszoutputs")
# write simple spreadsheet
write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info
)

For more customisation options, see below

Multiple sheets using a split variable

If you have data that has the same structure but should be written onto different sheets, you can split your data frame by a split variable. For each value of the split variable, a new data sheet will be created where the data, excluding the split variable, will be added. The values of the split variable serve as sheet names name, and will be appended to the the content/title information on the first sheet that you define with title_list.

meta.info <- c(t1 = "Split car data", t2 = "on different sheets")
# write multiple spreadsheets by splitting the penguins data on the variable island.
write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info,
  split_variable = "island",
  split_order = "as_is"
)

Please note that you can only use one variable for splitting. If you want to split by several variables, you must create one synthetic variable for splitting.

Multiple sheets with list of data frames

If you want to write data that differs structurally (different variables or number of rows), writing data with a split variable is not a viable option. Instead, we can wrap separate data frames into a list of data frames, where each data frame from the list will be written on a separate sheet. This is in development for next version of the package, but not supported in version 0.1.0.

Customisation {#customisation}

These customisation options work for the simple Excel version as well as the multiple sheet by split variable version.

Column formats

The default value column_formats="auto" lets the ssz-outputs package detect the column format of each variable for the Excel output file. If you want to specify formats yourself, you can provide a vector with column formats. When defining column-formats by yourself, you must supply the formats for all variables and cannot use "auto". The order of the variables in the data frame must match the order of the supplied column formats:

spaltenformate <- c(rep("string", 2), rep("decimal1", 2), rep("integer", 2), "string", "year")
write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info,
  column_formats = spaltenformate
)

If you are splitting data onto different sheets, you supply column_formats for all but the split variable. The implemented formats are:

  • string (excel standard format)

  • integer (excel format: "# ### ##0;– # ### ##0;0;–")

  • year (excel format: "########0;")

  • decimal1 (excel format: "# ### ##0.0;– # ### ##0.0;0.0;–")

  • decimal2 (excel format: "# ### ##0.00;– # ### ##0.00;0.00;–")

  • date (excel format: "dd.mm.yyyy")

Column widths

The default value column_widths="auto" uses excel-default widths based on the space needed to display the values. This may not always result in visually appealing spreadsheets. In this case, you can set customised column widths supplying a named vector, where you can set the width for each variable. You can also set the width only for specific columns and define a default for all the others. Because "default" is a placeholder for all variables not specified by name, your data cannot have variable called default.

spaltenbreiten <- c(species = 75, island = 75, sex = 60, default = 50)
write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info,
  column_widths = spaltenbreiten
)

When splitting a data frame into different sheets by a split variable and specifying column_widths, all the columns across the different sheets will have the same width. If you define "auto", the width of the same variable may differ across sheets depending on how much space Excel needs to show the variable content.

Please note that Excel column widths are specified in relation to the default font set in Excel's general options and not to the number of pixels. Depending on the default font, the same column width may be displayed with a different number of pixels. For example, width 40 will have a different number of pixel for a someone who has Arial 11 as their default font setting compared to someone with Calibri 8. As a result, column widths may differ slightly across users. SSZoutputs uses Arial 11 as the default font to create column widths.

Publisher details

The first sheet contains publisher details set to Statisitk Stadt Zürich by default. However, you can also add custom publisher details if you like.

publisher.info <- c(author = "Max Mustermann", organisation = "Organisationseinheit X", address = "Zürichsee")
write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info,
  publisher_details = publisher.info
)

You can also suppress any publisher details by providing an empty string:

write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info,
  publisher_details = ""
)
Metadata title

The file name will also be the title for the metadata properties of the file by default. However, you can also add a separate meta data title for the file properties.

metadata.title <- "SSZoutputs"
write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info,
  metadata_title = metadata.title
)
Comments file

You might want to include a sheet that explains details of the data. If that's the case, you can provide a comments file. The comments file will be inserted between the title sheet and the first data sheet. The comments file must be a single sheet excel file. Be aware that the comments file format will be applied 1:1 to the comments sheet in the Excel file created with SSZoutputs. This means that all column widths, row heights, fonts, colors and line breaks in cells will be exactly like those in the comments file provided.

# insert here the path to your comments file
path.comments.file <- "yourpath.xlsx"
write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info,
  comments_file = path.comments.file
)
Data origin

The default source of all data is "Statistik Stadt Zürich" and will appear as "Quelle" on all data sheets. However, you can provide a string pointing to another source. Note that if you use a split-variable the same information for the data origin will be written on all data sheets.

data.origin.custom <- "Bundesamt für Statistik"
write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info,
  data_origin = data.origin.custom
)
Split order

If you split a data frame by a split variable on different sheets, you can define the sheets' order based on the split-variable:

  • split_order="desc" is the default and orders the sheets from the highest to the lowest value of the split variable. E.g. if your split variable is numeric and has the values 2000,1999,1998, the first data sheet will be sheet will be "2000", then "1999" and "1998". If it is a string, sheet "Mann" precedes sheet "Frau".

  • split_order="asc" orders the sheets from the lowest to the highest value of the split variable. E.g. if your split variable is numeric and has the values 2000,1999,1998, the first data sheet will be sheet will be "1998", then "1999" and "2000". If it is a string variable, sheet "Frau" precedes sheet "Mann".

  • split_order="as_is" orders the sheets based on the order in which the values of the split variables appear. So if your split variable has 1999 in the first observation, 2000 in the second, and 1998 in the third observation, the data sheet order will be "1999","2000" and"1998".

write_ssz_excel(
  df = penguins,
  file = temp.path,
  title_list = meta.info,
  split_variable = "island",
  split_order = "asc"
)

CSV for d3-Visualisation

write_ssz_v simply wraps readr::write_csv and allows to write plain csv for d3 graphics.

write_ssz_v(
  x = penguins,
  file = temp.path
)

Contact {#contact}

If you spot a bug or want to suggest any further developments, get in touch with Statistik Stadt Zürich

About

R Package to create appropriately styled outputs (e.g. Excel files) for Statistik Stadt Zürich

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages