microsoft / microsoft-r-open

Microsoft R Open Source
212 stars 69 forks source link

function with grepl in package does not work with `devtools::load_all()` #87

Closed filipwastberg closed 5 years ago

filipwastberg commented 5 years ago

I have the following simple function that I have wrapped in a package:

str_grepl <- function(data) {
  data <- as.data.table(data)
  data <- data[!grepl("^set", Species)]
  return(data)
}

When I run this function and apply the function on my data, it works fine.

library(data.table)
str_grepl(iris)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#>   1:          7.0         3.2          4.7         1.4 versicolor
#>   2:          6.4         3.2          4.5         1.5 versicolor
#>   3:          6.9         3.1          4.9         1.5 versicolor
#>   4:          5.5         2.3          4.0         1.3 versicolor
#>   5:          6.5         2.8          4.6         1.5 versicolor
#>   6:          5.7         2.8          4.5         1.3 versicolor

However, when I save this function inside a package and load it with devtools::load_all() the function does not work:

devtools::load_all()
> str_grepl(iris)
 Error in grepl("^set", Species) : object 'Species' not found 

The funny thing is that it works if I install the package and load it.

devtools::install()
str_grepl(iris)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#>   1:          7.0         3.2          4.7         1.4 versicolor
#>   2:          6.4         3.2          4.5         1.5 versicolor
#>   3:          6.9         3.1          4.9         1.5 versicolor
#>   4:          5.5         2.3          4.0         1.3 versicolor
#>   5:          6.5         2.8          4.6         1.5 versicolor
#>   6:          5.7         2.8          4.5         1.3 versicolor

I use Microsoft R Open 3.5.1 on a virtual machine, however, when I switch to R 3.5.2 this issue disappears. Any idea what kind of bug this might be? Or is it something that I am missing?

I have put my example package on Github: https://github.com/filipwastberg/greplpackage

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server >= 2012 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.11.4    greplpackage_0.1.0   RevoUtils_11.0.1     RevoUtilsMath_11.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18    roxygen2_6.1.0  digest_0.6.15   withr_2.1.2     commonmark_1.5  R6_2.2.2        magrittr_1.5   
 [8] stringi_1.1.7   xml2_1.2.0      devtools_1.13.6 tools_3.5.1     stringr_1.3.1   yaml_2.2.0      compiler_3.5.1 
[15] memoise_1.1.0  
jeroenterheerdt commented 5 years ago

Hi Filip,

I was unable to reproduce the problem on my system. Here is what I did:

library(devtools)
install_github("filipwastberg/greplpackage")
library(greplpackage)
str_grepl(iris)

the output is as the table you have above:

     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
  1:          7.0         3.2          4.7         1.4 versicolor
  2:          6.4         3.2          4.5         1.5 versicolor
  3:          6.9         3.1          4.9         1.5 versicolor
  4:          5.5         2.3          4.0         1.3 versicolor
  5:          6.5         2.8          4.6         1.5 versicolor
  6:          5.7         2.8          4.5         1.3 versicolor
  7:          6.3         3.3          4.7         1.6 versicolor
  8:          4.9         2.4          3.3         1.0 versicolor
  9:          6.6         2.9          4.6         1.3 versicolor
 10:          5.2         2.7          3.9         1.4 versicolor
 11:          5.0         2.0          3.5         1.0 versicolor
 12:          5.9         3.0          4.2         1.5 versicolor
 13:          6.0         2.2          4.0         1.0 versicolor
 14:          6.1         2.9          4.7         1.4 versicolor
 15:          5.6         2.9          3.6         1.3 versicolor
 16:          6.7         3.1          4.4         1.4 versicolor
 17:          5.6         3.0          4.5         1.5 versicolor
 18:          5.8         2.7          4.1         1.0 versicolor
 19:          6.2         2.2          4.5         1.5 versicolor
 20:          5.6         2.5          3.9         1.1 versicolor
 21:          5.9         3.2          4.8         1.8 versicolor
 22:          6.1         2.8          4.0         1.3 versicolor
 23:          6.3         2.5          4.9         1.5 versicolor
 24:          6.1         2.8          4.7         1.2 versicolor
 25:          6.4         2.9          4.3         1.3 versicolor
 26:          6.6         3.0          4.4         1.4 versicolor
 27:          6.8         2.8          4.8         1.4 versicolor
 28:          6.7         3.0          5.0         1.7 versicolor
 29:          6.0         2.9          4.5         1.5 versicolor
 30:          5.7         2.6          3.5         1.0 versicolor
 31:          5.5         2.4          3.8         1.1 versicolor
 32:          5.5         2.4          3.7         1.0 versicolor
 33:          5.8         2.7          3.9         1.2 versicolor
 34:          6.0         2.7          5.1         1.6 versicolor
 35:          5.4         3.0          4.5         1.5 versicolor
 36:          6.0         3.4          4.5         1.6 versicolor
 37:          6.7         3.1          4.7         1.5 versicolor
 38:          6.3         2.3          4.4         1.3 versicolor
 39:          5.6         3.0          4.1         1.3 versicolor
 40:          5.5         2.5          4.0         1.3 versicolor
 41:          5.5         2.6          4.4         1.2 versicolor
 42:          6.1         3.0          4.6         1.4 versicolor
 43:          5.8         2.6          4.0         1.2 versicolor
 44:          5.0         2.3          3.3         1.0 versicolor
 45:          5.6         2.7          4.2         1.3 versicolor
 46:          5.7         3.0          4.2         1.2 versicolor
 47:          5.7         2.9          4.2         1.3 versicolor
 48:          6.2         2.9          4.3         1.3 versicolor
 49:          5.1         2.5          3.0         1.1 versicolor
 50:          5.7         2.8          4.1         1.3 versicolor
 51:          6.3         3.3          6.0         2.5  virginica
 52:          5.8         2.7          5.1         1.9  virginica
 53:          7.1         3.0          5.9         2.1  virginica
 54:          6.3         2.9          5.6         1.8  virginica
 55:          6.5         3.0          5.8         2.2  virginica
 56:          7.6         3.0          6.6         2.1  virginica
 57:          4.9         2.5          4.5         1.7  virginica
 58:          7.3         2.9          6.3         1.8  virginica
 59:          6.7         2.5          5.8         1.8  virginica
 60:          7.2         3.6          6.1         2.5  virginica
 61:          6.5         3.2          5.1         2.0  virginica
 62:          6.4         2.7          5.3         1.9  virginica
 63:          6.8         3.0          5.5         2.1  virginica
 64:          5.7         2.5          5.0         2.0  virginica
 65:          5.8         2.8          5.1         2.4  virginica
 66:          6.4         3.2          5.3         2.3  virginica
 67:          6.5         3.0          5.5         1.8  virginica
 68:          7.7         3.8          6.7         2.2  virginica
 69:          7.7         2.6          6.9         2.3  virginica
 70:          6.0         2.2          5.0         1.5  virginica
 71:          6.9         3.2          5.7         2.3  virginica
 72:          5.6         2.8          4.9         2.0  virginica
 73:          7.7         2.8          6.7         2.0  virginica
 74:          6.3         2.7          4.9         1.8  virginica
 75:          6.7         3.3          5.7         2.1  virginica
 76:          7.2         3.2          6.0         1.8  virginica
 77:          6.2         2.8          4.8         1.8  virginica
 78:          6.1         3.0          4.9         1.8  virginica
 79:          6.4         2.8          5.6         2.1  virginica
 80:          7.2         3.0          5.8         1.6  virginica
 81:          7.4         2.8          6.1         1.9  virginica
 82:          7.9         3.8          6.4         2.0  virginica
 83:          6.4         2.8          5.6         2.2  virginica
 84:          6.3         2.8          5.1         1.5  virginica
 85:          6.1         2.6          5.6         1.4  virginica
 86:          7.7         3.0          6.1         2.3  virginica
 87:          6.3         3.4          5.6         2.4  virginica
 88:          6.4         3.1          5.5         1.8  virginica
 89:          6.0         3.0          4.8         1.8  virginica
 90:          6.9         3.1          5.4         2.1  virginica
 91:          6.7         3.1          5.6         2.4  virginica
 92:          6.9         3.1          5.1         2.3  virginica
 93:          5.8         2.7          5.1         1.9  virginica
 94:          6.8         3.2          5.9         2.3  virginica
 95:          6.7         3.3          5.7         2.5  virginica
 96:          6.7         3.0          5.2         2.3  virginica
 97:          6.3         2.5          5.0         1.9  virginica
 98:          6.5         3.0          5.2         2.0  virginica
 99:          6.2         3.4          5.4         2.3  virginica
100:          5.9         3.0          5.1         1.8  virginica
     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species

I am running Microsoft R Open 3.5.1:

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] greplpackage_0.1.0   devtools_1.13.6      RevoUtils_11.0.1     RevoUtilsMath_11.0.0

loaded via a namespace (and not attached):
 [1] httr_1.3.1        compiler_3.5.1    Matrix_1.2-14     R6_2.2.2          withr_2.1.2       curl_3.2          memoise_1.1.0     grid_3.5.1        rtvs_1.0.0.0      data.table_1.11.4 git2r_0.23.0      digest_0.6.15     lattice_0.20-35  
filipwastberg commented 5 years ago

The problem wasn't actually to install the package and run the function. I got that working to.

The problem is that when I develop the package with devtools I load the package with devtools::load_all(). This is because, when developing, I change the function from time to time and then imitate library(greplpackage) with devtools::load_all(), so that I don't have to reinstall the package every time I change a function.

So for you to reproduce the error you need to clone my repository, open the R-project and try devtools::load_all() and then try the function.

I actually think that the problem is my description file, but it is still weird that it works on R 3.5.2 and not Microsoft Open R 3.5.1.

jeroenterheerdt commented 5 years ago

I can now produce the error on MRO 3.5.1.0:

> library(devtools)
+ library(data.table)
+ devtools::load_all()
+ str_grepl(iris)
data.table 1.11.4  Latest news: http://r-datatable.com
Loading greplpackage
Error in grepl("^set", Species) : object 'Species' not found
> 

I tested it in R Client 3.3.2.0, with the same result. So far the same results as you had.

When I test this in R 3.5.2 I get a different result compared to yours - I get the same result as before:

> library(devtools)
+ library(data.table)
+ devtools::load_all()
+ str_grepl(iris)
Loading greplpackage
Error in grepl("^set", Species) : object 'Species' not found
> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] greplpackage_0.1.0 data.table_1.11.4  devtools_1.13.6   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18   roxygen2_6.1.1 digest_0.6.15  withr_2.1.2    commonmark_1.5 R6_2.3.0       magrittr_1.5   stringi_1.1.7  xml2_1.2.0     tools_3.5.2    stringr_1.3.1 
[12] compiler_3.5.2 memoise_1.1.0  rtvs_1.0.0.0  

So, I am not sure what causes all of this, but I cannot get your package to work at all using devtools::load_all(). install_github() still works fine though. That is kind of the same thing, right? install_github() clones your repo and installs it. With devtools::load_all() I have to clone it first and then load it. Ok, there is less install going on, but still.

filipwastberg commented 5 years ago

Hm, I tested it on R 3.5.1 and R 3.5.2 on a mac and it works fine.

jeroenterheerdt commented 5 years ago

So it must be something in the base R system then.