r devtools test() errors but testthat test_file() works

前端 未结 1 1734
Happy的楠姐
Happy的楠姐 2021-01-19 19:59

I have a function in a package I\'m building that assigns a hex-code to the global environment for use by analysts...

optiplum<-function(){
  assign(
             


        
1条回答
  •  臣服心动
    2021-01-19 20:10

    Assignment to the global environment is a no-no, see R Inferno and testthat isolates tests as much as possible (see test_that() details). As a consequence, the optiplum() assignment to the global environment would not succeed because the testthat function strictly prohibits such behaviour.

    @Hadley rightly points out that the function should just return the string instead of assigning it, particularly since it is just two extra characters for each use.

    So not

    optiplum<-function(){
      assign(
        x="optiplum",
        value=rgb(red=129,green=61,blue=114, maxColorValue = 255),
        envir=.GlobalEnv)
      }
    

    but

    optiplum <- function() rgb(red=102,green=17,blue=109, maxColorValue = 255)
    

    0 讨论(0)
提交回复
热议问题