From a7b86a053ffe249deb8f6202ee6fa2625590d75d Mon Sep 17 00:00:00 2001
From: LeafLight <841789075@qq.com>
Date: Thu, 16 Oct 2025 00:55:27 +0800
Subject: [PATCH] refactor: simplify and standardize table saving in
interactionR_table
---
.idea/.gitignore | 8 +++++
.idea/copilot.data.migration.agent.xml | 6 ++++
.idea/copilot.data.migration.ask.xml | 6 ++++
.idea/copilot.data.migration.ask2agent.xml | 6 ++++
.idea/copilot.data.migration.edit.xml | 6 ++++
.idea/interactionR.iml | 8 +++++
.idea/misc.xml | 10 ++++++
.idea/modules.xml | 8 +++++
.idea/vcs.xml | 6 ++++
R/interactionR_table.R | 40 +++++++++-------------
10 files changed, 80 insertions(+), 24 deletions(-)
create mode 100644 .idea/.gitignore
create mode 100644 .idea/copilot.data.migration.agent.xml
create mode 100644 .idea/copilot.data.migration.ask.xml
create mode 100644 .idea/copilot.data.migration.ask2agent.xml
create mode 100644 .idea/copilot.data.migration.edit.xml
create mode 100644 .idea/interactionR.iml
create mode 100644 .idea/misc.xml
create mode 100644 .idea/modules.xml
create mode 100644 .idea/vcs.xml
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/copilot.data.migration.agent.xml b/.idea/copilot.data.migration.agent.xml
new file mode 100644
index 0000000..4ea72a9
--- /dev/null
+++ b/.idea/copilot.data.migration.agent.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/copilot.data.migration.ask.xml b/.idea/copilot.data.migration.ask.xml
new file mode 100644
index 0000000..7ef04e2
--- /dev/null
+++ b/.idea/copilot.data.migration.ask.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/copilot.data.migration.ask2agent.xml b/.idea/copilot.data.migration.ask2agent.xml
new file mode 100644
index 0000000..1f2ea11
--- /dev/null
+++ b/.idea/copilot.data.migration.ask2agent.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/copilot.data.migration.edit.xml b/.idea/copilot.data.migration.edit.xml
new file mode 100644
index 0000000..8648f94
--- /dev/null
+++ b/.idea/copilot.data.migration.edit.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/interactionR.iml b/.idea/interactionR.iml
new file mode 100644
index 0000000..d8b3f6c
--- /dev/null
+++ b/.idea/interactionR.iml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..13c3e30
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..60dfb31
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/R/interactionR_table.R b/R/interactionR_table.R
index cb99324..c3e2ce5 100644
--- a/R/interactionR_table.R
+++ b/R/interactionR_table.R
@@ -7,7 +7,7 @@
#'
#' @param p.value Includes p-values in the generated table if set to TRUE. Default is FALSE.
#'
-#' @param file_path An optional user-specified string representing the file path to save the generated Word table instead of the current working directory
+#' @param file Optional. File path (directory or full file name) to save the generated Word table. If NULL, the table is not saved. If a directory is provided, the file will be saved as 'interaction_table.docx' in that directory. If a file name is provided, it will be used as is. Default is NULL.
#'
#' @return saves a publication-ready microsoft word Table corresponding to Table 1 or Table 3 respectively in Knol and Vanderweele (2012) to the working directory (with user's permission).
#' It also returns an object of class flextable corresponding to the saved table for further manipulation.
@@ -33,10 +33,9 @@
#' @import flextable
#' @import officer
#' @export
-interactionR_table <- function(obj, p.value = FALSE, file_path = NA) {
+interactionR_table <- function(obj, p.value = FALSE, file = NULL) {
if (!is(obj, "interactionR")) {
- stop("Argument 'obj' must be an object of class 'interactionR',
- use the interactionR() function to generate such object ")
+ stop("Argument 'obj' must be an object of class 'interactionR',\n use the interactionR() function to generate such object ")
}
beta1 <- obj$exp_names[1]
@@ -191,28 +190,21 @@ interactionR_table <- function(obj, p.value = FALSE, file_path = NA) {
}
- if (!is.na(file_path)) {
- # checks if user pre-specified a file path
- path <- paste(file_path, "interaction.docx", sep = "\\")
- save_as_docx(t2, path = path)
- print(paste("The file 'interaction_table.docx' has been saved to", file_path, sep = " "))
+ if (!is.null(file)) {
+ if (dir.exists(file)) {
+ file <- file.path(file, "interaction_table.docx")
+ } else if (grepl("/$", file)) {
+ dir.create(file, showWarnings = FALSE, recursive = TRUE)
+ file <- file.path(file, "interaction_table.docx")
+ } else if (dirname(file) != "." && !dir.exists(dirname(file))) {
+ dir.create(dirname(file), showWarnings = FALSE, recursive = TRUE)
+ }
+ save_as_docx(t2, path = file)
+ message(sprintf("The file '%s' has been saved.", file))
print(t2)
invisible(t2)
} else {
- uprompt <- askYesNo("Do you want to save a Microsoft Word copy of the em/interaction table to your working directory?", default = FALSE)
- # Gets permission from the user to save Word file into the working directory
- # Default is NO
-
- if (is.na(uprompt) || !uprompt) {
- # if permission is declined or NA, working directory is untouched
- print(t2)
- invisible(t2)
- } else if (uprompt) {
- # if permission is given (as Yes), table is saved to working directory and user is informed
- save_as_docx(t2, path = "interaction_table.docx")
- print(paste("The file 'interaction_table.docx' has been saved to", getwd(), sep = " "))
- print(t2)
- invisible(t2)
- }
+ print(t2)
+ invisible(t2)
}
}