【问题标题】:How to replace text in gtext ( R / gWidgets )?如何替换 gtext ( R / gWidgets ) 中的文本?
【发布时间】:2013-11-20 13:32:26
【问题描述】:

假设(使用 R/gWidgets)我们有一个像这样的简单文本窗口:

require(gWidgets)
textwin <- gtext("The camel grazes in the desert.", container=gwindow())

当用鼠标选择文本时,我们可以检索选定的文本。

svalue(textwin, drop=T) # returns text
## [1] "grazes"

现在我的问题是:有没有办法替换 gtext 中的选定文本?

【问题讨论】:

    标签: r gwidgets


    【解决方案1】:

    检索整个字符串和选定的字符串。

    all_text <- svalue(textwin, index=0)
    selected_text <- svalue(textwin, index=0, drop = TRUE)
    

    然后使用regexpr查找所选子字符串的索引。

    rx_match <- regexpr(selected_text, all_text, fixed = TRUE)
    

    用其他东西替换那个子字符串。

    substring(
      all_text, 
      rx_match, 
      rx_match + attr(rx_match, "match.length") - 1
    ) <- "monkey"
    

    更新文本框。

    svalue(textwin) <- all_text
    

    B 计划:使用index = 1 操作来自svalue 的返回值。这几乎是正确的,但我认为gWidgets 中存在一个错误,导致它无法正常工作。

    # Get the index of the lines/character positions to start and finish
    i <- format(svalue(textwin, index = 1, drop = TRUE), digits = 3)
    i <- sapply(strsplit(i, "\\."), as.numeric)
    
    # Get all the text as a character vector
    all_text <- svalue(textwin, index=0)
    all_text <- strsplit(all_text, "\n")[[1]]
    
    # Replace the selected portion
    replacement <- "monkey"
    
    if(i[1, 1] == i[1, 2])
    {
      svalue(textwin) <- substring(all_text[i[1, 1]], i[2, 1], i[2, 2])
    } else
    {
        all_text[i[1, 1]] <- paste0(
          substring(all_text[i[1, 1]], 1, i[2, 1]),  
          replacement,
          substring(all_text[i[1, 2]], 1, i[2, 2])
        )
        svalue(textwin) <- all_text[-((i[1, 1] + 1):(i[1, 2]))]
    }
    

    最大的问题是调用format 将索引号转换为字符串。与两位数的位置相比,它在个位数的位置上表现不佳。我相当确定这是getMethod(.svalue, signature("gTexttcltk", "guiWidgetsToolkittcltk")) 中的一个错误(尽管其他工具包中也可能存在问题)。

    【讨论】:

    • 但是 RegExp 不知道光标的位置,这仅在提取的模式是唯一的或第一次出现时才有效;否则(使用重复模式)我们无法决定选择哪种模式匹配。换句话说,如果我在这句话中标记了最后一个“grazes”,这将不起作用:The camel grazes in the desert and grazes and grazes. 我会得到:The camel monkey in the desert and grazes and grazes. 而不是 The camel grazes in the desert and grazes and monkey.
    • 哈,那很好——它只适用于我的 tcltk 而不是 GTK,但我不介意——但是这个 *** *** !?")§ 将1.11.10 重新格式化为1.1(第一行第一个字符)现在这一切都没有用了,对吧?--感谢您的努力
    • @marvin_dpr 是的,它坏了。向 John Verzani 发送错误报告。他擅长修理东西。您可以通过packageDescription("gWidgets", fields = "Maintainer")获取他的联系方式。
    【解决方案2】:

    既然 Richie 说我擅长解决问题,那么就开始吧:

    gWidgets2 中,此功能已添加到 github 上的开发版本中。使用 devtools 安装。

    我正在尝试只修复 gWidgets 中的错误,这是一个新功能,所以我不会在那里添加它。但是,您可以在项目中轻松使用以下代码:

    library(RGtk2)    
    replace_selected <- function(gtext_widget, value) {
    
      view <- gtext_widget@widget@widget
    
      buffer <- view$getBuffer()
    
      bnds <- buffer$getSelectionBounds()
    
      if(bnds$retval) {
    
        buffer$insert(bnds$start, as.character(value), -1)
    
        new_bnds <- buffer$getSelectionBounds()
    
        buffer$delete(new_bnds$start, new_bnds$end)
    
      }
    }
    
    ## replace_selected in gWidgetstcltk
    
    replace_selected <- function(gtext_widget, value) {
    
      widget <- gtext_widget@widget@widget
    
      range <- as.numeric(tktag.ranges(widget, "sel"))
    
      if (length(range) > 0)
    
         tcl(widget,"replace", "sel.first", "sel.last", value)
    
    }
    

    【讨论】:

    • 列表与代码块混淆时使用。我不知道为什么,但删除子弹解决了这个问题。
    • 感谢制作 gWidgets 和 gWidgets2
    猜你喜欢
    • 2011-12-13
    • 1970-01-01
    • 2013-01-15
    • 2013-06-21
    • 2021-09-18
    • 1970-01-01
    • 2019-07-10
    • 2014-02-07
    • 1970-01-01
    相关资源
    最近更新 更多