I made some more investigation and I think tsmovavg based solution has a major drawback i.e. will not work for real time application. Lets assume you have 10k bars and you calculate ema on it. Than a new bar comes and calculate again 10k would be very time costly - much easier to calculate just last bar.
But in this case this code will not work because instead of use x-1 data
for initial value (so last data point of 1st calculation of ema) it will use some average values.
in other words it means that ema calculated on 10k+1 bars in one pass will have different value than ema calculated on last bar and it should not be a case.
I also got the error
Error using COM.StatConnectorSrv_StatConnector/Init
Error: Object returned error code: 0x80040013
when I first tried this. However, I reinstalled R using the directions on this website:
Making sure that I was running R as an administrator when I typed in the install.package commands. Now it works fine.