Don't want original data.table to be modified when passed to a function

后端 未结 3 2043
我寻月下人不归
我寻月下人不归 2021-01-18 20:49

I am a fan of data.table, as of writing re-usable functions for all current and future needs.

Here\'s a challenge I run into while working on the answer

3条回答
  •  执笔经年
    2021-01-18 21:37

    Just leaving out the := function seemed to succeed. Of course I wrapped the ggplot value in print(.) as would be standard practice when working inside a function and wanting output.:

    plotYofX <- function(.dt,x,y) {
      dt <- .dt
      dt[,  lapply(.SD, function(x) {as.numeric(x)}), .SDcols = c(x,y)]
      print( ggplot(dt) + geom_step(aes(x=get(names(dt)[x]), y=get(names(dt)[y]))) + labs(x=names(dt)[x], y=names(dt)[y]) )
    }
    
    > png(); plotYofX(dtDiamonds,1,2); dev.off()
    quartz 
         2 
    >  dtDiamonds
       carat     cut color
    1:  0.21 Premium     E
    2:  0.23    Good     E
    3:  0.29 Premium     I
    4:  0.31    Good     J
    

提交回复
热议问题