假设(使用 R/gWidgets)我们有一个简单的文本窗口,如下所示:
require(gWidgets)
textwin <- gtext("The camel grazes in the desert.", container=gwindow())
当用鼠标选择文本时,我们可以检索选定的文本。
svalue(textwin, drop=T) # returns text
## [1] "grazes"
现在我的问题是:有没有办法替换 gtext 中的选定文本?
检索整个字符串和选定的字符串。
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
svalue
计划 B:使用 with操作返回值index = 1
。这几乎是正确的,但我认为有一个错误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"))
(尽管其他工具包中也可能存在问题)。
既然 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)
}