Skip to content

Commit

Permalink
Use cli instead of crayon. (fix spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Jul 22, 2024
1 parent 01124c2 commit 128ddb5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ License: MIT + file LICENSE
Encoding: UTF-8
Imports:
ggplot2 (>= 3.5.0),
crayon,
cli,
generics,
jtools (>= 2.0.3),
Expand Down
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ import(jtools)
import(rlang)
importFrom(cli,cat_rule)
importFrom(cli,rule)
importFrom(crayon,bold)
importFrom(crayon,inverse)
importFrom(crayon,italic)
importFrom(crayon,red)
importFrom(crayon,underline)
importFrom(generics,glance)
importFrom(generics,tidy)
importFrom(stats,aggregate)
Expand Down
13 changes: 8 additions & 5 deletions R/interact_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,14 @@ interact_plot <- function(model, pred, modx, modx.values = NULL, mod2 = NULL,
# Check for factor predictor and send to plot_cat() if so
if (!is.numeric(d[[pred]])) {
# Warn users that this is kinda janky
msg_wrap("Detected factor predictor. Plotting with cat_plot() instead.
Consult cat_plot() documentation (?cat_plot) for full details
on how to specify models with categorical predictors. If you
experience errors or unexpected results, try using cat_plot()
directly.")
cli::cli_inform(c(
x = "Detected factor predictor.",
i = "Plotting with cat_plot() instead.",
i = "See {.help interactions::cat_plot} for full details on
how to specify models with categorical predictors.",
i = "If you experience errors or unexpected results, try using
cat_plot() directly."
))
# Gather arguments for plot_cat()
args <- list(predictions = pm, pred = pred, modx = modx, mod2 = mod2,
data = d, modx.values = modxvals2, mod2.values = mod2vals2,
Expand Down
14 changes: 8 additions & 6 deletions R/johnson_neyman.R
Original file line number Diff line number Diff line change
Expand Up @@ -603,17 +603,16 @@ johnson_neyman <- function(model, pred, modx, vmat = NULL, alpha = 0.05,
}

#' @export
#' @importFrom crayon bold inverse underline

print.johnson_neyman <- function(x, ...) {

atts <- attributes(x)

# Describe whether sig values are inside/outside the interval
if (atts$inside == FALSE) {
inout <- inverse("OUTSIDE")
inout <- cli::style_inverse("OUTSIDE")
} else {
inout <- inverse("INSIDE")
inout <- cli::style_inverse("INSIDE")
}

b_format <- num_print(x$bounds, atts$digits)
Expand All @@ -622,13 +621,16 @@ print.johnson_neyman <- function(x, ...) {
pmsg <- paste("p <", alpha)

# Print the output
cat(bold(underline("JOHNSON-NEYMAN INTERVAL")), "\n\n")
cli::cat_line(
cli::style_bold(cli::style_underline("JOHNSON-NEYMAN INTERVAL")),
"\n"
)
if (all(is.finite(x$bounds))) {
cat_wrap("When ", atts$modx, " is ", inout, " the interval [",
b_format[1], ", ", b_format[2], "], the slope of ", atts$pred,
" is ", pmsg, ".", brk = "\n\n")
cat_wrap(italic("Note: The range of observed values of", atts$modx,
"is "), "[", m_range[1], ", ", m_range[2], "]", brk = "\n\n")
cat_wrap(cli::style_italic("Note: The range of observed values of ", atts$modx,
" is "), "[", m_range[1], ", ", m_range[2], "]", brk = "\n\n")
} else {
cat_wrap("The Johnson-Neyman interval could not be found.
Is the p value for your interaction term below
Expand Down
10 changes: 6 additions & 4 deletions R/simple_margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ sim_margins <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,

#' @export
#' @importFrom cli cat_rule rule
#' @importFrom crayon red bold italic

print.sim_margins <- function(x, ...) {

Expand Down Expand Up @@ -308,7 +307,10 @@ print.sim_margins <- function(x, ...) {
}

# Clearly label simple slopes
cat(bold(underline("SIMPLE MARGINS")), "\n\n")
cli::cat_line(
cli::style_bold(cli::style_underline("SIMPLE MARGINS")),
"\n"
)

for (i in seq_along(x$modx.values)) {

Expand All @@ -329,8 +331,8 @@ print.sim_margins <- function(x, ...) {
modx_label <- paste0(m[i, x$modx])
}

cat(italic(paste0("Average marginal effect of ", x$pred, " when ",
x$modx, " = ", modx_label, ": \n\n")))
cli::cat_line(cli::style_italic(paste0("Average marginal effect of ", x$pred, " when ",
x$modx, " = ", modx_label, ": \n")))
print(md_table(slopes, digits = x$digits, format = "pandoc",
row.names = FALSE, sig.digits = FALSE))

Expand Down
12 changes: 7 additions & 5 deletions R/simple_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,6 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,

#' @export
#' @importFrom cli cat_rule rule
#' @importFrom crayon red bold italic

print.sim_slopes <- function(x, ...) {

Expand Down Expand Up @@ -848,7 +847,10 @@ print.sim_slopes <- function(x, ...) {
}

# Clearly label simple slopes
cat(bold(underline("SIMPLE SLOPES ANALYSIS")), "\n\n")
cli::cat_line(
cli::style_bold(cli::style_underline("SIMPLE SLOPES ANALYSIS")),
"\n"
)

for (i in seq_along(x$modx.values)) {

Expand All @@ -871,7 +873,7 @@ print.sim_slopes <- function(x, ...) {
# Print conditional intercept
if (x$cond.int == TRUE | fac_pred == TRUE) {
pred_lab <- if (fac_pred) {slopes[["Coef."]]} else {x$pred}
cat(italic(paste0("When ", x$modx, " = ", modx_label, ": \n\n")))
cli::cat_line(cli::style_italic(paste0("When ", x$modx, " = ", modx_label, ": \n")))
if (x$cond.int) {
ints <- m$ints[i,2:ncol(m$slopes)]
slopes <- as.data.frame(rbind(slopes, ints))
Expand All @@ -884,8 +886,8 @@ print.sim_slopes <- function(x, ...) {
print(md_table(slopes %not% "Coef.", digits = x$digits,
format = "pandoc", sig.digits = FALSE))
} else {
cat(italic(paste0("Slope of ", x$pred, " when ", x$modx, " = ",
modx_label, ": \n\n")))
cli::cat_line(cli::style_italic(paste0("Slope of ", x$pred, " when ", x$modx, " = ",
modx_label, ": \n")))
print(md_table(slopes, digits = x$digits, format = "pandoc",
row.names = FALSE, sig.digits = FALSE))
}
Expand Down

0 comments on commit 128ddb5

Please sign in to comment.