From 673bbbed3e856a38f3813bf4f054a830eb30cdc0 Mon Sep 17 00:00:00 2001 From: Lona Date: Sun, 12 Jan 2025 21:37:44 +0100 Subject: [PATCH 1/2] Add cat_cli function --- DESCRIPTION | 2 ++ NAMESPACE | 4 ++++ R/cat_cli.R | 18 ++++++++++++++++++ R/zzz.R | 1 + man/cat_cli.Rd | 22 ++++++++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 R/cat_cli.R create mode 100644 man/cat_cli.Rd diff --git a/DESCRIPTION b/DESCRIPTION index d51c8f7f..de2b9edc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,6 +22,7 @@ Depends: Imports: backports (>= 0.1.5), checkmate, + cli, data.table, digest, methods, @@ -47,6 +48,7 @@ Collate: 'assert_ro_binding.R' 'calculate_hash.R' 'capitalize.R' + 'cat_cli.R' 'catn.R' 'check_operators.R' 'check_packages_installed.R' diff --git a/NAMESPACE b/NAMESPACE index 6b52b5ec..7104f258 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -54,6 +54,7 @@ export(assert_ro_binding) export(calculate_hash) export(call_back) export(capitalize) +export(cat_cli) export(catf) export(catn) export(check_packages_installed) @@ -171,6 +172,9 @@ import(checkmate) import(data.table) importFrom(R6,R6Class) importFrom(R6,is.R6) +importFrom(cli,cli_format_method) +importFrom(cli,cli_h1) +importFrom(cli,cli_li) importFrom(digest,digest) importFrom(methods,formalArgs) importFrom(methods,hasArg) diff --git a/R/cat_cli.R b/R/cat_cli.R new file mode 100644 index 00000000..dc1fbec0 --- /dev/null +++ b/R/cat_cli.R @@ -0,0 +1,18 @@ +#' @title Function to transform message to output +#' +#' @description +#' Wrapper around [cli::cli_format_method()]. +#' Uses [base::cat()] to transform the printout from a message to an output with a line break. +#' +#' @param expr (`any`)\cr +#' Expression that calls `cli_*` methods, [base::cat()] or[base::print()] to format an object's printout. +#' +#' @export +#' @examples +#' cat_cli({ +#' cli::cli_h1("Heading") +#' cli::cli_li(c("x", "y")) +#' }) +cat_cli = function(expr) { + cat(cli::cli_format_method(expr), sep = "\n") +} diff --git a/R/zzz.R b/R/zzz.R index 2617c234..7f883a1a 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -6,6 +6,7 @@ #' @importFrom digest digest #' @importFrom methods formalArgs hasArg #' @importFrom utils capture.output +#' @importFrom cli cli_h1 cli_li cli_format_method "_PACKAGE" .onLoad = function(libname, pkgname) { diff --git a/man/cat_cli.Rd b/man/cat_cli.Rd new file mode 100644 index 00000000..59101255 --- /dev/null +++ b/man/cat_cli.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/cat_cli.R +\name{cat_cli} +\alias{cat_cli} +\title{Function to transform message to output} +\usage{ +cat_cli(expr) +} +\arguments{ +\item{expr}{(\code{any})\cr +Expression that calls \verb{cli_*} methods, \code{\link[base:cat]{base::cat()}} or\code{\link[base:print]{base::print()}} to format an object's printout.} +} +\description{ +Wrapper around \code{\link[cli:cli_format_method]{cli::cli_format_method()}}. +Uses \code{\link[base:cat]{base::cat()}} to transform the printout from a message to an output with a line break. +} +\examples{ +cat_cli({ + cli::cli_h1("Heading") + cli::cli_li(c("x", "y")) +}) +} From d033f5f1af7a603cbb31ce92d01837c9689f00d7 Mon Sep 17 00:00:00 2001 From: Lona Date: Mon, 13 Jan 2025 11:39:46 +0100 Subject: [PATCH 2/2] Update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 61e1685f..eb24a7b9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ * feat: `as_callbacks()` returns a list named by the callback ids now. * feat: Added logical operators `%check&&%` and `%check||%` for `check_*`-functions from `checkmate` (moved here from `mlr3pipelines`). +* Added helper `cat_cli()`. # mlr3misc 0.16.0