Title: | Read and Write 'Excel' Sheets into and from List of Data Frames |
---|---|
Description: | Reading and writing sheets of a single Excel file into and from a list of data frames. Eases I/O of tabular data in bioinformatics while keeping them in a human readable format. |
Authors: | Gwang-Jin Kim [aut, cre] |
Maintainer: | Gwang-Jin Kim <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2025-03-09 02:55:31 UTC |
Source: | https://github.com/gwangjinkim/xlsx2dfs |
Write a list of data frames into an excel file with each data frame in a new sheet and the list element name as its sheet name.
dfs2xlsx(dfs, fpath, rowNames = TRUE, colNames = TRUE)
dfs2xlsx(dfs, fpath, rowNames = TRUE, colNames = TRUE)
dfs |
A list of data frames (names in the list are the names of the sheets). |
fpath |
A character string representing path and filename of the output. |
rowNames |
A boolean indicating whether the first column of a table in every sheet contains row names of the table. |
colNames |
A boolean indicating whether the first line of a table in every sheet contains a header. |
Nothing. Writes out data frames into specified Excel file.
df1 <- data.frame(A=c(1, 2), B=c(3, 4)) df2 <- data.frame(C=c(5, 6), D=c(7, 8)) xlsx_fpath <- file.path(tempdir(), "testout.xlsx") dfs2xlsx(withNames("sheet1", df1, "sheet2", df2), xlsx_fpath) file.remove(xlsx_fpath)
df1 <- data.frame(A=c(1, 2), B=c(3, 4)) df2 <- data.frame(C=c(5, 6), D=c(7, 8)) xlsx_fpath <- file.path(tempdir(), "testout.xlsx") dfs2xlsx(withNames("sheet1", df1, "sheet2", df2), xlsx_fpath) file.remove(xlsx_fpath)
Helper function for more convenient input (sheet name, data frame, sheet name, data frame, ...).
withNames(...)
withNames(...)
... |
alterning arguments: sheet name 1, data frame 1, sheet name 2, data frame 2, ... |
A list of the data frames with the names given each before.
df1 <- data.frame(A=c(1, 2), B=c(3, 4)) df2 <- data.frame(C=c(5, 6), D=c(7, 8)) xlsx_fpath <- file.path(tempdir(), "testout.xlsx") dfs2xlsx(withNames("sheet1", df1, "sheet2", df2), xlsx_fpath) file.remove(xlsx_fpath)
df1 <- data.frame(A=c(1, 2), B=c(3, 4)) df2 <- data.frame(C=c(5, 6), D=c(7, 8)) xlsx_fpath <- file.path(tempdir(), "testout.xlsx") dfs2xlsx(withNames("sheet1", df1, "sheet2", df2), xlsx_fpath) file.remove(xlsx_fpath)
Read-in Excel file (workbook) as a list of data frames.
xlsx2dfs(xlsxPath, rowNames = TRUE, colNames = TRUE, ...)
xlsx2dfs(xlsxPath, rowNames = TRUE, colNames = TRUE, ...)
xlsxPath |
A path to the Excel file, a character. |
rowNames |
Whether to read-in row names, a boolean. |
colNames |
Whether to read-in column names, a boolean. |
... |
... passed to read.xlsx function in the openxlsx package. |
A list of data frames, each representing a sheet in the Excel file (sheet names are list element names).
# create example file df1 <- data.frame(A=c(1, 2), B=c(3, 4)) df2 <- data.frame(C=c(5, 6), D=c(7, 8)) xlsx_fpath <- file.path(tempdir(), "testout.xlsx") dfs2xlsx(withNames("sheet1", df1, "sheet2", df2), xlsx_fpath) # read created file dfs <- xlsx2dfs(xlsx_fpath) file.remove(xlsx_fpath)
# create example file df1 <- data.frame(A=c(1, 2), B=c(3, 4)) df2 <- data.frame(C=c(5, 6), D=c(7, 8)) xlsx_fpath <- file.path(tempdir(), "testout.xlsx") dfs2xlsx(withNames("sheet1", df1, "sheet2", df2), xlsx_fpath) # read created file dfs <- xlsx2dfs(xlsx_fpath) file.remove(xlsx_fpath)