Search code examples
rdata.tablecolon-equals

R - Error in `:=`(variable, as.character(variable))


I'm trying to run an R script, which can be found here with the command plot_trace.R -m ./log.model.csv ./log.trace.csv.

The two csv files can be found here.

This produces the error:

Error in `:=`(variable, as.character(variable)) : 
  Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
Calls: [ -> [.tbl_df -> check_names_df -> :=

Last but not least my sessionInfo() output:

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

loaded via a namespace (and not attached):
[1] compiler_3.4.4

Thanks


Solution

  • UPDATE

    I found two problems with this script:

    • The author mixes functions from tidyr and variables of class data.table. These functions will likely return data frames and using data.table syntax on these will cause errors. Unfortunately, some tidyr functions may return data.table when operating on one, but this seems to vary greatly depending on the version of the tidyr package.

    • The unnest function from tidyr has a new interface. The script won't work as is with tidyr version 1.0.0 or later.

    Below are the lines I changed to fix the script, using R version 3.6.1 and tidyr version 1.0.0:

    • line 56: added trace.df <- as.data.table(trace.df)
    • line 64: changed to trace.df <- unnest(trace.df, cols)
    • line 65: added trace.df <- as.data.table(trace.df)

    Original answer

    I was able to run the script on the data you provided without error. Both

    Rscript --vanilla plot_trace.R -m ./log.model.csv ./log.trace.csv
    

    and

    chmod +x plot_trace.R
    ./plot_trace.R -m ./log.model.csv ./log.trace.csv
    

    produced the following plot:

    Result plot

    Can you run the script step by step in RStudio to check the class of trace.df after each step?