Question

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))
})
Was it helpful?

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.

OTHER TIPS

Well, Richie did his usual thorough job. This question has a few problems: One you use the variable h as a global variable (for the gdf object) and as the argument to the handler, so within the handler h does not refer to the object, but h$obj would. Second To set values for selection in the gdf object uses the [<- method (h[i,j] <- "" calls the h object's set_items method). You tried to modify the object, not call a method on it. As for NA values, underlying the items to select from is an RGtk2DataFrame, which like a data frame in R will coerce values to character if you try to put a character value into a numeric value. Best, to use R as it is intended. If you really want to get rid of NA values you can do so when you go to use the values that the user has edited, modifying h[,] as you want.

Now, if you really wanted to do this, I think you could at the RGtk2 level by writing an appropriate cell renderer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top