Search code examples
rgwidgets

Insert "" instead of NA when adding rows in gdf [gWidgets2RGtk2]


Is it possible to insert "" instead of NA when creating a new row in gdf?

EDIT: Here's some sample code that I tried

require(gWidgets2RGtk2)
df <- data.frame(x=1:5,y=6:10) #Sample data frame
w2 <- gwindow("keyfile editor")
h <- gdf(df,cont=w2)
addHandlerChanged(h, handler = function(h,...){ #Handler to remove NA
h<<-apply(h[1:nrow(h),1:ncol(h)], 2, function(x) gsub("NA","",x))
})

Solution

  • svalue(h$obj, drop = FALSE)
    

    gives you the new value for the updated row. So in theory,

    addHandlerChanged(h, handler = function(h,...) {
      svalue(h$obj, drop = FALSE)[] <- lapply(
        svalue(h$obj, drop = FALSE), 
        function(x) {
          x[is.na(x)] <- ""
        }
      )
    }
    

    should replace all the NAs with "". There are two problems:

    Firstly, replacing the missing values with an empty string converts the whole column to be a character vector, which you probably don't want, and secondly, there seems to be a problem with svalue<- that means the values aren't updating.

    I think that the problem is this:

    methods(`svalue<-`)
    ## [1] svalue<-.default*     svalue<-.GCheckbox*   svalue<-.GFormLayout* svalue<-.GGroup*     
    ## [5] svalue<-.GHtml*       svalue<-.GLabel*      svalue<-.GMenuBar*    svalue<-.GRadio*     
    ## [9] svalue<-.GToolBar*    svalue<-.GTree*
    

    shows that there is no GDf-specific method for setting the svalue, so svalue<-.default will be called.

    gWidgets2:::`svalue<-.default`
    ## function (obj, index = NULL, ..., value) 
    ## {
    ##     if (!isExtant(obj)) {
    ##         return(obj)
    ##     }
    ##     if (getWithDefault(index, FALSE)) 
    ##         obj$set_index(value, ...)
    ##     else obj$set_value(value, ...)
    ##     obj
    ## }
    

    This calls the object's set_value method.

    ls(attr(h, ".xData"))
    ##  [1] "add_cell_popup"          "add_popup_to_view_col"   "add_to_parent"          
    ##  [4] "add_view_columns"        "block"                   "block_editable_column"  
    ##  [7] "cell_popup_id"           "change_signal"           "clear_stack"            
    ## [10] "clear_view_columns"      "cmd_coerce_column"       "cmd_insert_column"      
    ## [13] "cmd_remove_column"       "cmd_replace_column"      "cmd_set_column_name"    
    ## [16] "cmd_set_column_names"    "cmd_stack"               "coerce_with"            
    ## [19] "connected_signals"       "default_cell_popup_menu" "default_expand"         
    ## [22] "default_fill"            "default_popup_menu"      "freeze_attributes"      
    ## [25] "get_column_index"        "get_column_value"        "get_dim"                
    ## [28] "get_name"                "get_view_column"         "handler_id"             
    ## [31] "initFields"              "initialize"              "initialize#GComponent"  
    ## [34] "initialize#GWidget"      "invoke_change_handler"   "invoke_handler"         
    ## [37] "is_editable"             "map_j"                   "model"                  
    ## [40] "not_deleted"             "notify_observers"        "parent"                 
    ## [43] "set_editable"            "set_frame"               "set_name"               
    ## [46] "set_names"               "set_parent"              "store"                  
    ## [49] "toolkit"                 "unblock_editable_column" "widget" 
    

    but there doesn't seem to be one implemented yet.