These functions can be used to export data sets and plots. They invisibly return the object itself again, allowing for usage in pipes (except for the plot-exporting functions export_pdf(), export_png() and export_html()). The functions work closely together with the certeprojects package to support Microsoft Planner project numbers.

export(
  object,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  fn = NULL,
  ...
)

export_rds(
  object,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  ...
)

export_xlsx(
  ...,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  sheet_names = NULL,
  autofilter = TRUE,
  rows_zebra = TRUE,
  cols_zebra = FALSE,
  freeze_top_row = TRUE,
  table_style = "TableStyleMedium2",
  align = "center"
)

export_excel(
  ...,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  sheet_names = NULL,
  autofilter = TRUE,
  rows_zebra = TRUE,
  cols_zebra = FALSE,
  freeze_top_row = TRUE,
  table_style = "TableStyleMedium2",
  align = "center"
)

export_csv(
  object,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  na = "",
  ...
)

export_csv2(
  object,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  na = "",
  ...
)

export_tsv(
  object,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  na = "",
  ...
)

export_txt(
  object,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  sep = "\t",
  na = "",
  ...
)

export_sav(
  object,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  ...
)

export_spss(
  object,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  ...
)

export_feather(
  object,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  ...
)

export_pdf(
  plot,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  size = "A5",
  portrait = FALSE,
  ...
)

export_png(
  plot,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  width = 1000,
  height = 800,
  dpi = NULL,
  ...
)

export_html(
  plot,
  filename = NULL,
  project_number = project_get_current_id(ask = FALSE),
  overwrite = NULL,
  ...
)

export_clipboard(
  object,
  sep = "\t",
  na = "",
  header = TRUE,
  quote = FALSE,
  decimal.mark = dec_mark(),
  ...
)

export_teams(
  object,
  filename = NULL,
  full_teams_path = NULL,
  account = connect_teams(),
  ...
)

Arguments

object, plot

the R object to export

filename

the full path of the exported file

project_number

a Microsoft Planner project number

overwrite

a logical value to indicate if an existing file must be overwritten. In interactive mode, this will be asked if the file exists. In non-interactive mode, this has a special default behaviour: the original file will be copied to filename_datetime.ext before overwriting the file. Exporting with existing files is always non-destructive: if exporting fails, the original, existing file will not be altered.

fn

a manual export function, such as haven::write_sas to write SAS files. This function has to have the object as first argument and the future file location as second argument.

...

arguments passed on to methods

sheet_names

sheet names

autofilter

create autofilter on columns in first row

rows_zebra

create banded rows

cols_zebra

create banded columns

freeze_top_row

freeze the first row of the sheet

table_style

style(s) for each table, see below. This can also be a vector with the same length as ....

align

horizontal alignment of text

na

replacement character for empty values (default: "")

sep

separator for values in a row (default: tab)

size

paper size, defaults to A5. Can be A0 to A7.

portrait

portrait mode, defaults to FALSE (i.e., landscape mode)

width

required width of the PNG file in pixels

height

required height of the PNG file in pixels

dpi

plot resolution, defaults to DPI set in showtext package

header

(for export_clipboard()) use column names as header (default: TRUE)

quote

(for export_clipboard()) use quotation marks (default: FALSE)

decimal.mark

(for export_clipboard()) character to use for decimal numbers, defaults to dec_mark()

full_teams_path

path in Teams to export object to. Can be left blank to use interactive folder picking mode in the console.

account

a Teams account from Azure or an AzureAuth Microsoft 365 token, e.g. retrieved with certeprojects::connect_teams()

Details

The export() function can export to any file format, also with a manually set export function when passed on to the fn argument. This function fn has to have the object as first argument and the future file location as second argument. If fn is left blank, the export_* function will be used based on the filename.

RDS files as created using export_rds() are compatible with R3 and R4.

The export_xlsx() and export_excel() functions use save_excel(as_excel(...)) internally. IMPORTANT: these two functions can accept more than one data.frame. When naming the data sets, the names will become sheet names in the resulting Excel file. For a complete visual overview of supported table styles, see as_excel(). If the last value in ... is a character of length 1 and filename is NULL, this value is assumed to be the filename.

For export_csv(), export_csv2() and export_tsv(), files will be saved in UTF-8 encoding and NA values will be exported as "" at default. Like other *.csv and *.csv2 functions, csv is comma (,) separated and csv2 is semicolon (;) separated.

The export_txt() function exports to a tab-separated file.

Exporting to an SPSS file using export_sav() or export_spss() requires the haven package to be installed.

Exporting to a Feather file using export_feather() requires the arrow package to be installed. Apache Feather provides efficient binary columnar serialization for data sets, enabling easy sharing data across data analysis languages (such as between Python and R).

Exporting to a PDF file using export_pdf() requires the ggplot2 package to be installed. If the filename is left blank in export_pdf(), export_png() or export_html(), the title of plot will be used if it's available and the certeplot2 package is installed, and a timestamp otherwise. NOTE: All export functions invisibly return object again, but the plotting functions invisibly return the file path

Exporting to a PNG file using export_png() requires the ggplot2 and showtext packages to be installed.

Exporting to an HTML file using export_html() requires the ggplot2 and htmltools packages to be installed. The arguments put in ... will be passed on to plotly::layout() if plot is not yet a Plotly object (but rather a ggplot2 object), which of course then requires the plotly package to be installed as well.

Exporting to the clipboard using export_clipboard() requires the clipr package to be installed. The function allows any object (also other than data.frames) to be exported to the clipboard and is only limited to the available amount of RAM memory.

Exporting to Microsoft Teams using export_teams() requires the AzureGraph package to be installed. The function allows any object (also other than data.frames) to be exported to any Team channel. The filename set in filename will determine the exported file type and defaults to an RDS file.

See also

Examples

library(dplyr, warn.conflicts = FALSE)

# export to two files: 'whole_file.rds' and 'first_ten_rows.xlsx'
starwars |>
  export_rds("whole_file") |>
  slice(1:10) |>
  export_xlsx("first_ten_rows")
#> Exported data set (87x14) to '/tmp/Rtmpzh1vOI/file172576ca6b56/reference/whole_file.rds' (3.5 kB).
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Exported data set (10x14) to '/tmp/Rtmpzh1vOI/file172576ca6b56/reference/first_ten_rows.xlsx' (8.4 kB).
  
# the above is equal to:
# starwars |>
#   export("whole_file.rds") |>
#   slice(1:10) |>
#   export("first_ten_rows.xlsx")


# Apache's Feather format is column-based
# and allow for cross-language specific and fast file reading
starwars |> export_feather()
#> Exported data set (87x14) to '/tmp/Rtmpzh1vOI/file172576ca6b56/reference/starwars.feather' (12.2 kB).
import("starwars.feather",
       col_select = starts_with("h")) |> 
  head()
#>   height  hair_color homeworld
#> 1    172       blond  Tatooine
#> 2    167        <NA>  Tatooine
#> 3     96        <NA>     Naboo
#> 4    202        none  Tatooine
#> 5    150       brown  Alderaan
#> 6    178 brown, grey  Tatooine
  

# (cleanup)
file.remove("whole_file.rds")
#> [1] TRUE
file.remove("first_ten_rows.xlsx")
#> [1] TRUE
file.remove("starwars.feather")
#> [1] TRUE

if (FALSE) {

# ---- Microsoft Teams support -------------------------------------------

# IMPORTING

# import from Teams by picking a folder interactively from any Team
x <- import_teams()

# to NOT pick a Teams folder (e.g. in non-interactive mode), set `full_teams_path`
x <- import_teams(full_teams_path = "MyTeam/MyChannel/MyFolder/MyFile.xlsx")


# EXPORTING

# export to Teams by picking a folder interactively from any Team
mtcars |> export_teams()

# the default is RDS, but you can set `filename` to specify yourself
mtcars |> export_teams("mtcars.xlsx")

# to NOT pick a Teams folder (e.g. in non-interactive mode), set `full_teams_path`
mtcars |> export_teams("mtcars.xlsx", full_teams_path = "MyTeam/MyChannel/MyFolder")
mtcars |> export_teams(full_teams_path = "MyTeam/MyChannel/MyFolder")

}