saveOutput: Save the current R image or the object rds file and add its...

View source: R/rmd.util.r

saveOutputR Documentation

Save the current R image or the object rds file and add its information to the matrix OUTPUTS

Description

Save the current R image or the object rds file and optionally other formats of the object (e.g. csv for tables and png for figures), its caption, and other information related to its appearance in the R markdown file, to the matrix OUTPUTS

Usage

saveOutput(obj=NULL,oFileName, saveWorkspace=FALSE, oPath=getwd(), caption=NA ,
           rmdInd=NA, eval=TRUE,objID=NA, header=NA, footer=NA,
           rowHeaderInd=NA, colWidths=NA, fontSize=11, nRowScroll = 20,
           nRowDisplay = 200, maxTableWidth = 7.2, theme = "zebra",
           numberOutputFiles=TRUE,...)

ofcmm(num=1)

Arguments

obj

The target object, usually a data.frame or ggplot object. If NULL, oFileName can be used to insert an existing figure file into OUTPUTS, so that the figure file can be auto-included into the Rmd file produced by createRmd function.

oFileName

(character). The file name of the text (e.g. csv) or image (e.g. png) file of the obj to save. Can contain relative or absolute file paths; Use, e.g. ./path1/my.csv, instead of path1/my.csv to include a relative path into the file name. If oFileName contains either a relative or absolute path, the oPath will be replaced by the whole path.

saveWorkspace

(boolean, FALSE)

  • FALSE: Save the rds file of the obj only. If oFileName is provided, the rds file name is paste0(ofNamePrefix,'.rds'); otherwise,

    paste0(sprintf ("%03d", OFCOUNTER),'.rds').

  • TRUE: In addition to the rds file, save the R image of the workspace where obj is generated as paste0(sprintf ("%03d", OFCOUNTER),'.r.image.rdata')

oPath

(character, getwd()). The output path.

caption

(character, NA). The caption of the target object.

rmdInd

(integer, OFCOUNTER). The order to display obj in the Rmd file.

eval

(boolean, TRUE). Should the obj be included/evaluated in the Rmd file?

objID

(character, NA, or paste0('tab',OFCOUNTER), or paste0('fig',OFCOUNTER)). The label of the obj in the Rmd code chunk. Its default value depends on the data type of obj: paste0('tab',OFCOUNTER) for data.frame or matrix, paste0('fig',OFCOUNTER) for ggplot, and NA otherwise. If provided, it is cleaned by gsub("[^A-Za-z0-9]","",objID), and prefixed by tab or fig depending on the type of obj. If it's already present in OUTPUTS[,"objID"], OFCOUNTER is appended, i.e. objID = paste0(objID,OFCOUNTER), to make it unique.

header

(character, NA). The header of the obj, if it is displayed as a table in the Rmd file. See parameter header of rmdTable for more details and its character representation.

footer

(character, NA). The footer of the obj, if it is displayed as a table in the Rmd file. See parameter footer of rmdTable for more details and its character representation.

colWidths

(character, NA). The column width of the obj, if it is displayed as a table in the Rmd file. See parameter colWidths of rmdTable for more details and its character representation.

rowHeaderInd, fontSize, nRowScroll, nRowDisplay, maxTableWidth, theme

Parameters for obj, if it is displayed as a table in the Rmd file. See same parameters in rmdTable for more details.

numberOutputFiles

(boolean, TRUE). Should the output files be numbered?

...

parameters passed to write.csv or ggsave to save the obj, if a data.frame or matrix, as text file.

num

the number to deduct from OFCOUNTER

Details

For the simplicity of coding, two global variables are created for this function:

  • OFCOUNTER (integer,1). A global variable to count the number of outputs, initial value is 1.

  • OUTPUTS (character matrix, NULL). A global variable to record the information of the current r image file and obj. At the 1st run of saveOutput, it is assigned to be a character matrix of following columns:

    • rImageName The name of saved R image or rds file, depending on the flag saveWorkspace.

    • all other parameters of saveOutput except obj, and values assigned by them.

saveOutput does the following:

  1. Save the current R image as paste0(sprintf("%03d", OFCOUNTER),'.r.image.rdata') or the object rds file in the oPath directory.

  2. If oFileName is provided, save obj as paste0(sprintf("%03d", OFCOUNTER),'.',oFileName) in the oPath directory, using either utils::write.csv or ggplot2::ggsave depending on the data type of obj

  3. Create the matrix OUTPUTS if it is NULL, and assign the values of all other parameters to corresponding columns in OUTPUTS

  4. Increment OFCOUNTER by 1

ofcmm deducts OFCOUNTER by num. It can be used to remove the information appended to OUTPUTS from running saveOutput.

Value

none

See Also

OUTPUTS, OFCOUNTER

Examples

library(wfr)
library(ggplot2)

print(OFCOUNTER)
print(OUTPUTS)

df1=data.frame(A=c("a","a","b3"),
               B=c("b1","b2","b3"),
               C1=1:3,C2=2:4)

  tmpDir = tempdir()
  ofn = file.path(tmpDir, "tab.1.csv")
  saveOutput(df1, oFileName=ofn, caption="this is a wrong caption")
  print(OFCOUNTER)
  print(OUTPUTS)
  ofcmm()
  print(OFCOUNTER)
  saveOutput(df1, oFileName=ofn, caption="this is the correct caption")
  print(OUTPUTS)


print(OFCOUNTER)
print(OUTPUTS)


  ofn = file.path(tmpDir, "fig.1.png")
  saveOutput(qplot(1:10,1:10), oFileName=ofn, caption="this is a testing plot")


print(OFCOUNTER)
print(OUTPUTS)

blueskypie/wfr documentation built on Feb. 6, 2024, 4:38 p.m.