Two sequence Session.set cause one rerun? - meteor

In Chrome console,
> Tracker.autorun(function() {console.log(Session.get('show'))});
> Session.set('show',2);
2
> Session.set('show',3);
3
> Session.set('show',4);Session.set('show',5);
5
Two sequence Session.set cause one rerun?
How can I make it run twice?

You can use Tracker.flush() to immediately rerun the computation after the first reactive data source update :
> Session.set("show",4);Tracker.flush();Session.set("show",5);

Related

Responding to multiple mouse clicks to produce a different output - Tkinter

I'm working on a problem requiring me to create a GUI (in Tkinter) which shows a different word in the label (referencing from a list) each time the button is pressed.
I've tried researching and have found similar problems but haven't found a working solution yet. I have tried 'for each' and 'while' loops, and 'if' statements, but haven't been able to get the code working correctly.
the_window.counter = 0
if the_window.counter == 0:
top_label['text'] = words [0]
the_window.counter + 1
elif the_window.counter == 1:
top_label['text'] = words [1]
the_window.counter + 1
the code shown above produces the first word in the list only, and multiple clicks don't have any effect. does anyone have any ideas?
Thanks.
You need need to keep a global counter, and update it each time it is clicked.
The following code illustrates the technique:
# initialized to -1, so that the first time it is called
# it gets set to zero
the_window_counter = -1
def handle_click():
global the_window_counter
the_window_counter += 1
try:
top_label.configure(text=words[the_window_counter])
except IndexError:
top_label.configure(text="no more words")

Fill-In validation with N format

I have a fill-in with the following code, made using the AppBuilder
DEFINE VARIABLE fichNoBuktiTransfer AS CHARACTER FORMAT "N(18)":U
LABEL "No.Bukti Transfer"
VIEW-AS FILL-IN NATIVE
SIZE 37.2 BY 1 NO-UNDO.
Since the format is N, it blocks the user from entering non-alphanumeric entries. However, it does not prevent the user from copypasting such entries into the fill-in. I have an error checking like thusly to prevent such entries using the on leave trigger:
IF LENGTH(SELF:Screen-value) > 18 THEN DO:
SELF:SCREEN-VALUE = ''.
RETURN NO-APPLY.
END.
vch-list = "!,*, ,#,#,$,%,^,&,*,(,),-,+,_,=".
REPEAT vinl-entry = 1 TO NUM-ENTRIES(vch-list):
IF INDEX(SELF:SCREEN-VALUE,ENTRY(vinl-entry,vch-list) ) > 0 THEN DO:
SELF:SCREEN-VALUE = ''.
RETURN NO-APPLY.
END.
END.
However, after the error handling kicked in, when the user inputs any string and triggers on leave, error 632 occurs:
error 632 occurs
Is there any way to disable the error message? Or should I approach the error handling in a different way?
EDIT: Forgot to mention, I am running on Openedge version 10.2B
You didn't mention the version, but I'll assume you have a version in which the CLIPBOARD system handle already exists.
I've simulated your program and I believe it shouldn't behave that way. It seems to me the error flag is raised anyway. My guess is even though those symbols can't be displayed, they are assigned to the screen value somehow.
Conjectures put aside, I've managed to suppress it by adding the following code:
ON CTRL-V OF FILL-IN-1 IN FRAME DEFAULT-FRAME
DO:
if index(clipboard:value, vch-list) > 0 then
return no-apply.
END.
Of course this means vch-list can't be scoped to your trigger anymore, in case it is, because you'll need the value before the leave. So I assigned the special characters list as an INIT value to the variable.
After doing this, I didn't get the error anymore. Hope it helps.
To track changes in a fill-in I always use at first this code:
ON VALUE-CHANGED OF FILL-IN-1 IN FRAME DEFAULT-FRAME
DO:
/* proofing part */
if ( index( clipboard:value, vch-list ) > 0 ) then do:
return no-apply.
end.
END.
You could add some mouse or developer events via AppBuilder to track changes in a fill-in.

Remove connection object immediately after closing connection

This is a two-part question regarding the following chunk of code.
> ## ran rm(list = ls()) prior to the following
> closeAllConnections()
> tc <- textConnection("messages", "w")
> isOpen(tc)
# [1] TRUE
> close(tc)
> ls()
# [1] "messages" "tc"
> is.object(tc)
# [1] TRUE
> class(tc)
# [1] "textConnection" "connection"
> tc
# Error in summary.connection(x) : invalid connection
Why isn't tc removed from the list of objects, ls(), immediately when the tc connection is closed, and what does invalid connection mean? Is there a reason why R keeps tc in the list?
Is there a way to remove it from the object list immediately after it's closed? I really don't want to call rm() if it's not necessary. Perhaps I missed an argument somewhere while scanning the help files.
The reason this is important is because I have a function called list.objects that returns an error after I run the above code, but does not otherwise (possibly because tc has two classes).
For 1., tc isn't removed from the list of objects because close doesn't delete the variable you use to contain the pointer to the connection. Rather, close closes the pointer and effectively removes it from the open file connections list (see showConnections). The variable that contains the pointer still exists, it's just that the pointer points nowhere. This explains why you get an error when you type tc after you close it, you're trying to look at a file connection that goes nowhere.
For 2., what's so hard about close(tc); rm(tc)? Hardly any more typing than if there actually was a "delete my first argument" parameter.
tc is a variable that holds a reference to certain state. There is no particular reason that a call to close() should come with an rm() built-in. That would be like expecting a TV remote to disappear on its own after you turn off the TV by hitting the power button.
I think you will have to call rm(tc) to remove it.

is it possible to redirect console output to a variable?

In R, I'm wondering if it's possible to temporarily redirect the output of the console to a variable?
p.s. There are a few examples on the web on how to use sink() to redirect the output into a filename, but none that I could find showing how to redirect into a variable.
p.p.s. The reason this is useful, in practice, is that I need to print out a portion of the default console output from some of the built-in functions in R.
I believe results <- capture.output(...) is what you need (i.e. using the default file=NULL argument). sink(textConnection("results")); ...; sink() should work as well, but as ?capture.output says, capture.output() is:
Related to ‘sink’ in the same way that ‘with’ is related to ‘attach’.
... which suggests that capture.output() will generally be better since it is more contained (i.e. you don't have to remember to terminate the sink()).
If you want to send the output of multiple statements to a variable you can wrap them in curly brackets {}, but if the block is sufficiently complex it might be better to use sink() (or make your code more modular by wrapping it in functions).
For the record, it's indeed possible to store stdout in a variable with the help of a temorary connection without calling capture.output -- e.g. when you want to save both the results and stdout. Example:
Prepare the variable for the diverted R output:
> stdout <- vector('character')
> con <- textConnection('stdout', 'wr', local = TRUE)
Divert the output:
> sink(con)
Do some stuff:
> 1:10
End the diversion:
> sink()
Close the temporary connection:
> close(con)
Check results:
> stdout
[1] " [1] 1 2 3 4 5 6 7 8 9 10"

`data.table` error: "reorder received irregular lengthed list" in setkey

I have a fairly basic data.table in R, with 250k rows and 90 columns. I am trying to key the data.table on one of the columns which is of class character. When I call:
setkey(my.dt,my.column)
I receive the following cryptic error message:
"Error in setkeyv(x, cols, verbose=verbose) :
reorder received irregular lengthed list"
I have found a source-code commit with this message, but can't quite decipher what it means. My key column contains no NA or blank values, seems perfectly reasonable to look at (it contains stock tickers), and behaves well with the default order() command.
Even more frustrating, the following code completes correctly:
first.dt <- my.dt[1:100000]
setkey(first.dt,my.column)
second.dt <- my.dt[100001:nrow(my.dt]
setkey(second.dt,my.column)
I have no idea what could be going on here. Any tips?
Edit 1: I have confirmed every value in the key fits a fairly standard format:
> length(grep("[A-Z]{3,4}\\.[A-Z]{2}",my.dt$my.column)) == nrow(my.dt)
[1] TRUE
Edit 2: My system info is below (note that I'm actually using Windows 7). I am using data.table version 1.8.
> Sys.info()
sysname release version nodename machine login
"Windows" "Server 2008 x64" "build 7600" "WIN-9RH28AH0CKG" "x86-64" "Administrator"
user effective_user
"Administrator" "Administrator"
Please run :
sapply(my.dt, length)
I suspect that one or more columns have a different length to the first column, and that's an invalid data.table. It won't be one of the first 5 because your .Internal(inspect(my.dt)) (thanks) shows those and they're ok.
If so, there is this bug fix in v1.8.1 :
o rbind() of DT with an irregular list() now recycles the list items
correctly, #2003. Test added.
Any chance there's an rbind() at an earlier point to create my.dt together with an irregular lengthed list? If not, please step through your code running the sapply(my.dt,length) to see where the invalidly lengthed column is being created. Armed with that we can make a work around and also fix the potential bug. Thanks.
EDIT :
The original cryptic error message is now improved in v1.8.1, as follows :
DT = list(a=6:1,b=4:1)
setattr(DT,"class",c("data.table","data.frame"))
setkey(DT,a)
Error in setkeyv(x, cols, verbose = verbose) :
Column 2 is length 4 which differs from length of column 1 (6). Invalid
data.table. Check NEWS link at top of ?data.table for latest bug fixes. If
not already reported and fixed, please report to datatable-help.
NB: This method to create a data.table is not recommended because it lets you create an invalid data.table. Unless, you are really sure the list is regular and you really do need speed (i.e. for speed you want to avoid the checks that as.data.table() and data.table() do), or you need to demonstrate an invalid data.table, as I'm doing here.

Resources