-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
glue_col to work without attaching crayon ? (+ a few comments) #138
Comments
In a package import the specific functions from crayon you want with For custom functions, assign them to an object and then use that, one of the examples shows this usage Here are your examples my_color <- crayon::yellow$bgMagenta$bold
glue::glue_col("{my_color Hello} world!")
blue <- crayon::blue
glue::glue_col("{blue hello}") |
Thanks it makes sense, my workaround was quite convoluted :). |
In hindsight Jim this is not good enough, check out this more realistic example :
Here's what I would like to work : box_print <- function(txt){
if(!requireNamespace("glue"))
stop("The package glue must be installed to use box_print")
if(!requireNamespace("crayon"))
stop("The package crayon must be installed to use box_print")
txt <- glue::glue_col(txt)
line <- strrep("#", crayon::col_nchar(txt)+4)
cat(sep="", "\n", line, "\n# ", txt, " #\n", line ,"\n")
}
box_print("foo")
#> Loading required namespace: glue
#> Loading required namespace: crayon
#>
#> #######
#> # foo #
#> #######
box_print("{green hello} {blue world}")
#> Error in get(fun, envir = envir, mode = "function"): object 'green' of mode 'function' was not found
# oops, I need to attach
library(crayon)
box_print("{green hello} {blue world}")
#>
#> ###############
#> # hello world #
#> ############### If I don't want to attach or import I need a workaround, which is a bit more complex than it could be given r-lib/withr#107 :).
Created on 2019-06-26 by the reprex package (v0.3.0) The function
It seems to me |
Just import the functions you need, crayon is an R only package with no non-base dependencies, you should not be worried about including it. If you really want to do what you propose just assign the functions you want to use in whatever environment you want to use them in. |
The doc seems to imply that only crayon functions can be used with glue_col, considering this I have a few comments:
If it can in some circumstances make sense to use a function that is not from crayon, could it be documented ?
As crayon functions will be the ones mostly used in any case, isn't it redundant to require the pakage to be attached, couldn't
glue_col
access crayon's namespace ?glue::glue_col("{crayon::blue hello}")
triggers the following error, which means there's no simple way out if crayon isn't attached, isn't it wrong to "force" the user to attach a package if it can be avoided ? (note :glue_col("{yellow$bgMagenta$bold Hello} world!")
doesn't work either, maybe it could be supported by the same token ?)4
glue::glue_col("{blue hello}")
triggers the following error when crayon is not attached. the doc is very clear but the error itself might be clearer if it mentions the need to attach crayon. If you consider my point 2. this point becomes irrelevant, and a helpful error would suggest installing crayon if it's not installed.My use case is that I want to wrap
glue_col
in a package, and I'd rather not attach crayon. My work around is to callglue_col
wrapped inside your ownwithr::with_package("crayon", *code using glue_col*)
but it means one more dependency and is not obvious.The text was updated successfully, but these errors were encountered: