wlandau / targets-tutorial

Short course on the targets R package

Home Page:https://rstudio.cloud/project/1699460

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in compute_cor() function while running 4-files.Rmd

durraniu opened this issue · comments

Prework

  • I have read and agree to the code of conduct.
  • I checked in the previous issues, but did not find this question asked before.
  • The minimum reprex is below

Description

Thank you for this tutorial. I am at the document 4-files.Rmd where we add the churn_cor target in _target file. After adding the target, I ran tar_make() at line 116, but it throws the following error:

Error: callr subprocess failed: Can't rename columns that don't exist. x Column rowname doesn't exist.

Reproducible example

I checked the relevant functions.R file. The function compute_cor() is defined at line 279. I tried to reproduce the example above the function:

> library(corrr)
> library(keras)
> library(recipes)
> library(rsample)
> library(tidyverse)
> library(yardstick)
> churn_data <- split_data("data/churn.csv")
> churn_recipe <- prepare_recipe(churn_data)
> compute_cor(churn_recipe)
 Error: Can't rename columns that don't exist.
x Column `rowname` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.

The function definition is as follows:

compute_cor <- function(churn_recipe) {
  churn_recipe %>%
    juice() %>%
    correlate(quiet = TRUE) %>%
    focus(Churn) %>%
    rename(feature = rowname) %>%
    arrange(abs(Churn)) %>%
    mutate(feature = as_factor(feature)) 
}

So, I ran the code line by line, outside of the function:

> churn_recipe %>%
+     juice() %>%
+     correlate(quiet = TRUE) %>%
+     focus(Churn) %>%
+     rename(feature = rowname)
Error: Can't rename columns that don't exist.
x Column `rowname` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.

I think the error occurs because rename function has an undefined column name rowname.

Expected result

compute_cor should create a tibble of correlations. If I put term inside the rename function, it works:

> churn_recipe %>%
+     juice() %>%
+     correlate(quiet = TRUE) %>%
+     focus(Churn) %>%
+     rename(feature = term) %>%
+     arrange(abs(Churn)) %>%
+     mutate(feature = as_factor(feature)) 
# A tibble: 35 x 2
   feature                          Churn
   <fct>                            <dbl>
 1 gender_Male                    -0.0128
 2 PhoneService_Yes                0.0133
 3 MultipleLines_No.phone.service -0.0133
 4 tenure_bin3                    -0.0172
 5 MultipleLines_Yes               0.0541
 6 DeviceProtection_Yes           -0.0603
 7 tenure_bin4                    -0.0640
 8 OnlineBackup_Yes               -0.0733
 9 StreamingTV_Yes                 0.0780
10 StreamingMovies_Yes             0.0834
# ... with 25 more rows

Diagnostic information

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

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

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

other attached packages:
 [1] yardstick_0.0.7    rsample_0.0.8      recipes_0.1.15    
 [4] keras_2.3.0.0      corrr_0.4.3        forcats_0.5.0     
 [7] stringr_1.4.0      dplyr_1.0.2        purrr_0.3.4       
[10] readr_1.4.0        tidyr_1.1.2        tibble_3.0.4      
[13] ggplot2_3.3.3      tidyverse_1.3.0    targets_0.0.0.9004

loaded via a namespace (and not attached):
 [1] fs_1.5.0             usethis_2.0.0       
 [3] lubridate_1.7.9.2    httr_1.4.2          
 [5] tools_4.0.3          backports_1.2.0     
 [7] utf8_1.1.4           R6_2.5.0            
 [9] rpart_4.1-15         DBI_1.1.0           
[11] colorspace_2.0-0     nnet_7.3-14         
[13] withr_2.3.0          tidyselect_1.1.0    
[15] processx_3.4.5       compiler_4.0.3      
[17] cli_2.2.0            rvest_0.3.6         
[19] xml2_1.3.2           labeling_0.4.2      
[21] scales_1.1.1         callr_3.5.1         
[23] tfruns_1.4           digest_0.6.27       
[25] rmarkdown_2.6        base64enc_0.1-3     
[27] pkgconfig_2.0.3      htmltools_0.5.0     
[29] parallelly_1.22.0    dbplyr_2.0.0        
[31] rlang_0.4.10         readxl_1.3.1        
[33] rstudioapi_0.13      farver_2.0.3        
[35] generics_0.1.0       jsonlite_1.7.2      
[37] tensorflow_2.2.0     magrittr_2.0.1      
[39] Matrix_1.2-18        Rcpp_1.0.5          
[41] munsell_0.5.0        fansi_0.4.1         
[43] reticulate_1.18-9000 lifecycle_0.2.0     
[45] furrr_0.2.1          pROC_1.16.2         
[47] stringi_1.5.3        whisker_0.4         
[49] yaml_2.2.1           MASS_7.3-53         
[51] plyr_1.8.6           grid_4.0.3          
[53] parallel_4.0.3       listenv_0.8.0       
[55] crayon_1.3.4         lattice_0.20-41     
[57] haven_2.3.1          splines_4.0.3       
[59] hms_0.5.3            zeallot_0.1.0       
[61] knitr_1.30           ps_1.5.0            
[63] pillar_1.4.7         igraph_1.2.6        
[65] codetools_0.2-16     reprex_0.3.0        
[67] glue_1.4.2           evaluate_0.14       
[69] data.table_1.13.6    modelr_0.1.8        
[71] vctrs_0.3.6          cellranger_1.1.0    
[73] gtable_0.3.0         future_1.21.0       
[75] assertthat_0.2.1     xfun_0.20           
[77] gower_0.2.2          prodlim_2019.11.13  
[79] broom_0.7.3          class_7.3-17        
[81] survival_3.2-7       timeDate_3043.102   
[83] lava_1.6.8.1         globals_0.14.0      
[85] ellipsis_0.3.1       ipred_0.9-9 

Thanks for catching that. Must have been a recent update to corrr. Should be fixed now.