I think the problem lies in the fact that during the load script you're only processing one item at a time, while the kmeans clustering needs all data-points in the cluster to determine the clusters. So you call it once for all data points and you get a cluster index for each datapoint.
So you can run the functions in scripts, but not all R functions are suitable.
To see a working example of the iris dataset, have a look at the Advanced Analytics Expression Builder which will write the code for you. There is a youtube video which uses this dataset as an example:
my point here is not strinctly bounded to iris dataset.
In order to create real business cases I think that being able to run scripts such as cluster analysis is basically. Usually you want to cluster when you need insights from the data much more complex than iris.
If you have to perform it on a data set with 1000000 record the graph takes a lot, and user loose the Qlik's experience of dynamic navigation.
As per my understanding, use R functions ( excluded the sum() ) in the script is not possible, as confirmed also by Qiyu.
If someone has a script, even an easy one, that wants to share it would be very helpful.
It would be good to be able to run R from script on data sets also. Clustering is a good example but apriori analysis is an even better one - it takes too long to calculate dynamically.
I am also looking for a workaround, lie storing data to CSV and running R or Python in the background (it would load data from that CSV and export results to another CSV - which would be read by Qlik again).
An interesting solution can be provided with RapidMiner tool. It can be called from Qlik with simple Web Services request.
This way we call it from within the script and receive the result directly into the script. (1. store CSV, 2. call RapidMinder via Web Services, which reads CSV and does the calculation, 3. receive back the result.