简体   繁体   English

如何通过单击按钮来增加计数器的值?

[英]How to increment value of a counter by clicking on a button?

I have a button like this : 我有一个像这样的按钮:

win <- gwindow("Extraction des mots clés", visible=TRUE)
obj2 <- gbutton("Valider et afficher les mots suivants",container=win)

I want that each time I press the button, a counter which is set to 0 in my script is incremented. 我希望每次按下按钮时,脚本中设置为0的计数器都会增加。

k<-0

I created this handler : 我创建了这个处理程序:

addHandlerClicked(obj2,handler = function(h,...){
k<-k+1
print (k)
return(k)
})

but when I do print k outside of the addHandlerClicked , k is still 0. So how can I do ? 但是当我在addHandlerClicked之外print k时, k仍为0。那我该怎么办? Moreover, is the instruction return (k) that I wrote above can be used to achieve this ? 此外,我上面编写的指令return (k)是否可以用来实现这一目标?

Thanks in advance 提前致谢

This is the way R functions work. 这就是R函数的工作方式。 Objects in (global) workspace have one value and when you create a same-name object within a function, it has no memory of the same name object outside. (全局)工作空间中的对象具有一个值,并且当您在函数中创建同名对象时,它在外部没有任何同名对象的内存。 R functions don't modify other variables, you have to return the modified value and overwrite it explictily. R函数不会修改其他变量,您必须返回修改后的值并明确覆盖它。 Normally. 一般。 Enter <<- . 输入<<- This assignment operator will go from the function all the way to the global environment and try to find the same name object and write over/to it. 该赋值运算符将从函数一直到全局环境,并尝试查找相同的名称对象并将其覆盖。 See more info by typing ?"<<-" . 通过输入?"<<-"查看更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM