1

I want to add a infix function to a package but the %%s challenge me.

I found this solution but unfortunately it doesn't explain where exactly the line export("%IN%") should be added. I didn't get any smarter from that question either. Since the questions are five years old I decided to ask a new question.

Consider the same function as in the first linked question.

"%IN%" <- function(x, table) x & match(x, table, nomatch = 0) > 0

I usually add a new function to my package writing a <myfun>_function.R file according to this rough template.

#' Title
#'
#' \code{%IN%} does this and that
#' @param x texttext    
#' @param table texttext
#' @return texttext
#' @export
#' @examples
#' 1:5 %IN% 1:3
"%IN%" <- function(x, table) x & match(x, table, nomatch = 0) > 0

Accordingly I'd save a file named "`%IN%`_function.R" to the R folder of my package directory X.

Then in setwd("./X") I run these lines of code

library(digest)
R.utils::reassignInPackage("digest", "digest", mydigest)
roxygen2::roxygenize()

(Where I got mydigest from there).

At the end in the terminal I create the package with R CMD build X.

So, where exactly is the export("%IN%") line to be added?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    the name of your file doesn't matter, though I would choose a less weird one, like `infix_ops.R`, but you need to add `@export`, as you should for other functions that you want to export. – moodymudskipper Jan 22 '19 at 10:57
  • 1
    take a look here https://github.com/moodymudskipper/mmassign/blob/master/R/more_ops.R and here : https://github.com/moodymudskipper/mmassign/blob/master/NAMESPACE – moodymudskipper Jan 22 '19 at 11:02
  • 1
    "where exactly is the export("%IN%") line to be added?" In your package's NAMESPACE file. (The same as for all other functions you want to export.) – Roland Jan 22 '19 at 11:38
  • @Moody_Mudskipper Thanks for your clues and your examples, I made it work! – jay.sf Jan 22 '19 at 11:42
  • @Roland Thx, got it and summarized it in an own answer! – jay.sf Jan 22 '19 at 11:55

1 Answers1

0

According to comments the trick is to do in this order:

  1. write the function's *.R file as usual, name it arbitrary
  2. run roxygenize()
  3. in package folder edit the generated NAMESPACE file by adding a line export("%IN%") with great delight by hand
  4. run R CMD build <package name> in terminal
  5. perhaps update version number
  6. install
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • You do not need to edit the file by hand if you use the `@export` directive with roxygen2: https://cran.r-project.org/web/packages/roxygen2/vignettes/namespace.html – Roland Jan 22 '19 at 15:26
  • @Roland Could you be more specific how this relates to my problem? – jay.sf Jan 24 '19 at 11:48
  • Per your answer you are using roxygen2. You are not using its full potential if you don't use it to create/edit your NAMESPACE file. I don't edit my NAMESPACE files by hand and still export functions. – Roland Jan 24 '19 at 11:54