StatisticsGreenland / pxmake

pxmake: Read and write px files in R.
https://statisticsgreenland.github.io/pxmake/
Other
6 stars 3 forks source link

Check if it is possible to create error message for name clash with gt:px #302

Open johan-ejstrud opened 4 months ago

larpSTATGL commented 2 weeks ago

chatGPT says:

Yes, you can create a custom error message for a name clash between pxmake::px and gt::px in your package. You can add checks to detect if gt is loaded and if gt::px has been masked by pxmake::px. Here’s a simple approach to add this check when your package is loaded.

Step 1: Create an .onLoad() function Add an .onLoad() function in your package’s main file (e.g., zzz.R), which will execute when the package is loaded. Inside this function, you can check for potential name clashes.

r Kopier kode .onLoad <- function(libname, pkgname) {

Check if gt is loaded

if ("package:gt" %in% search()) {

Check if gt::px has been masked by pxmake::px

if (exists("px", where = "package:pxmake") && exists("px", where = "package:gt")) {
  warning("Name clash detected: 'px' exists in both 'pxmake' and 'gt'. ",
          "Please use 'pxmake::px' or 'gt::px' to specify explicitly.")
}

} } Step 2: Test the Behavior Install and load your package alongside gt and ensure that the warning message appears if there’s a name conflict. This approach will give your users a clear indication of the conflict and provide them with guidance on how to resolve it.

johan-ejstrud commented 2 weeks ago

ChatGPT is probably right, but isn't this kind of error message already what R is doing?

Examples

image image

larpSTATGL commented 2 weeks ago

but that is a general starter warning - it always slips my eyes - like advertisements. Perhaps improved if thrown only when needed. that is if gt::px is actually in r-script?