3535# ' * `"Excel"`: print to an Excel workbook. Please specify the name of an Excel file using
3636# ' the `file` argument. Before using Excel printing, please be sure to install these
3737# ' packages: `openxlsx2` and `mschart`.
38+ # ' * `"CSV"`: print to a comma-separated values (CSV) file. Please specify the name of a
39+ # ' CSV file using the `file` argument.
3840# '
3941# ' @param reset reset all options to their default values?
4042# ' @param mode `"general"` or `"NCHS"`. See below for details.
4143# ' @param adj adjustment to the Korn and Graubard confidence intervals for proportions. See
4244# ' `svyciprop_adjusted()` for details.
4345# ' @param output specify how the output is printed: `"auto"` (default); `"huxtable"`, `"gt"`, or
44- # ' `"kableExtra"`; `"raw"`; or `"Excel "`. If `output` is `"Excel"`: must also specify `file`; be
45- # ' sure to install `openxlsx2` and `mschart`.
46+ # ' `"kableExtra"`; `"raw"`; `"Excel"` or `"CSV "`. If `output` is `"Excel"` or `"CSV"`, must also specify
47+ # ' `file`. If `output` is `"Excel"`, be sure to install `openxlsx2` and `mschart`.
4648# ' @param file file name (see `output`).
4749# ' @param .file_temp place `file` in a temporary folder?
4850# ' @param count round counts to the nearest integer (`"int"`) or one thousand (`"1k"`).
4951# ' @param lpe identify low-precision estimates?
5052# ' @param drop_na drop missing values (`NA`)? Categorical variables only.
5153# ' @param max_levels a categorical variable can have at most this many levels.
5254# ' Used to avoid printing huge tables.
53- # ' @param csv the name of a CSV file or `""` to turn off CSV output.
5455# '
5556# ' @return (Nothing.)
5657# ' @family options, print
5758# ' @export
5859# '
5960# ' @examples
60- # ' # Send output to a CSV file:
61- # ' file_name = tempfile(fileext = ".csv")
62- # ' suppressMessages( set_opts(csv = file_name) )
6361# ' set_survey(namcs2019sv)
62+ # '
63+ # ' # Round counts to the nearest one thousand:
64+ # ' set_opts(count = "1k")
6465# ' tab("AGER")
65- # ' set_opts(csv = "") # Turn off CSV output
66+ # ' set_opts(count = "int")
6667# '
6768# ' show_opts()
6869set_opts = function (
@@ -76,7 +77,6 @@ set_opts = function(
7677 , lpe = NULL
7778 , drop_na = NULL
7879 , max_levels = NULL
79- , csv = NULL
8080 ) {
8181
8282 # ### !!! If making changes, update: .onLoad(), set_opts(), show_opts(), .check_options()
@@ -124,7 +124,7 @@ set_opts = function(
124124 if (! is.null(output )) {
125125 if (getOption(" surveytable.print" ) == " .print_excel" ) .print_excel_finish()
126126
127- output %<> % .mymatch(c(" huxtable" , " gt" , " kableExtra" , " auto" , " raw" , " excel" ))
127+ output %<> % .mymatch(c(" huxtable" , " gt" , " kableExtra" , " auto" , " raw" , " excel" , " csv " ))
128128 if (output == " auto" ) {
129129 message(" * Printing with huxtable for screen, gt for HTML, or kableExtra for PDF." )
130130 options(surveytable.raw = FALSE
@@ -140,26 +140,8 @@ set_opts = function(
140140 options(surveytable.raw = TRUE
141141 , surveytable.print = " .print_raw"
142142 , surveytable.file = " " , surveytable.file_show = " " )
143- } else if (output == " excel" ) {
144- assert_that(is.string(file ), nzchar(file )
145- , msg = " For Excel printing, please specify a file name using the file argument." )
146- if (! endsWith(tolower(file ), " .xlsx" )) {
147- file = glue(" {file}.xlsx" )
148- }
149- if (! isTRUE(.file_temp )) {
150- file %<> % normalizePath(mustWork = FALSE )
151- file_show = file
152- } else {
153- file = file.path(tempdir(), file )
154- file_show = file %> % basename()
155- }
156- message(glue(" * Printing to Excel workbook {file_show}." ))
157- if (file.exists(file )) {
158- message(" * NOTE: file already exists!" )
159- }
160- options(surveytable.raw = FALSE
161- , surveytable.print = " .print_excel"
162- , surveytable.file = file , surveytable.file_show = file_show )
143+ } else if (output %in% c(" excel" , " csv" )) {
144+ .set_output_file(output = output , file = file , .file_temp = .file_temp )
163145 }
164146 }
165147
@@ -206,21 +188,36 @@ set_opts = function(
206188 options(surveytable.max_levels = max_levels )
207189 }
208190
209- if (! is.null(csv )) {
210- assert_that(is.string(csv )
211- , msg = " CSV file name must be a character string." )
212- if (nzchar(csv )) {
213- message(paste0(" * Sending CSV output to: " , csv ))
214- if (file.exists(csv )) {
215- message(" * (File already exists. Output will be appended to the end of the file.)" )
216- }
217- message(" * To turn off CSV output: set_opts(csv = '')" )
218- } else {
219- message(" * Turning off CSV output." )
220- }
221- options(surveytable.csv = csv )
222- }
223-
224191 .check_options()
225192 invisible (NULL )
226193}
194+
195+ .set_output_file = function (output , file , .file_temp ) {
196+ assert_that(output %in% c(" excel" , " csv" ))
197+ type = switch (output
198+ , excel = " Excel"
199+ , csv = " CSV" )
200+ extension = switch (output
201+ , excel = " .xlsx"
202+ , csv = " .csv" )
203+ assert_that(is.string(file ), nzchar(file )
204+ , msg = glue(" For {type} printing, please specify a file name using the file argument." ))
205+ if (! endsWith(tolower(file ), extension )) {
206+ file = glue(" {file}{extension}" )
207+ }
208+ if (! isTRUE(.file_temp )) {
209+ file %<> % normalizePath(mustWork = FALSE )
210+ file_show = file
211+ } else {
212+ file = file.path(tempdir(), file )
213+ file_show = file %> % basename()
214+ }
215+ message(glue(" * Printing to {type} file {file_show}." ))
216+ if (file.exists(file )) {
217+ message(" * NOTE: file already exists!" )
218+ }
219+ options(surveytable.raw = FALSE
220+ , surveytable.print = glue(" .print_{output}" )
221+ , surveytable.file = file , surveytable.file_show = file_show )
222+ }
223+
0 commit comments