Error with Rexcel when starting up Excel - r

Today something happened (never had this problem before) when I start up Excel workbook where I use Rexcel.
And then there are some more errors... Everything worked properly before.. Does anyone have an idea about what the problem could be?
The other strange thing is that after that the Rapply and so on works.. But I get errors again when I close workbook.
So the problem appears when I startR from Rexcel meny in Excel.
EDIT:
Thanks to flodel for his answer. But is it not possible to run the Sub below now or?
Sub create_efficient_frontier()
RInterface.StartRServer
Sheets("Analys").Range("A52:K82").ClearContents
RInterface.PutDataframe "datat", Range("ChosenData!X181:AD352")
RInterface.PutArray "startdate", Range("Analys!K2")
RInterface.PutArray "enddate", Range("Analys!K3")
RInterface.RunRFile "C:/Users/Documents/EffFront.R"
RInterface.GetDataframe "hmz$pweight", Range("Analys!A51:E76")
End Sub
I get runtime error 13 ("incompatible types") at line
RInterface.GetDataframe "hmz$pweight", Range("Analys!A51:E76")
That worked yesterday, see Run macro with Rexcel
Solution:
Ctrl+Alt+Shift+F9 seems to work:)
Best Regards

Yes. I have struggled with this myself. Most likely your computations must be computed in a specific order. Typically, some of your (RApply, REval, etc.) calls create some data (e.g. x <- 10) in the R session and later calls will expect that data to exist: compute x^2.
Everything works fine when you build your spreadhseet from scratch. The problem is that when you save it and reopen it, Excel does not know in what order it needs to compute your cells: it figures out its dependency tree by firing out cell computations in a somewhat random order. So what happens is the cell supposed to compute x^2 might be run before the cell that creates x in your R environment, which triggers an error.
This is annoying but as you noticed, the errors do eventually go away after Excel is able to build its dependency tree. If you absolutely need to find a solution to that problem, it will have to involve VBA code to help Excel build its dependency tree in a very specific order.

Related

I cannot obtain scores for a metaMDS object in RStudio (package: vegan)

I'm using vegan 2.6.4 in RStudio, and have had an unusual error message pop up when I run the the following:
nmds11 = metaMDS(m_com11, distance = "bray")
data.scores11 = as.data.frame(scores(nmds11)$sites)
Error in UseMethod("scores") :
no applicable method for 'scores' applied to an object of class "c('metaMDS', 'monoMDS')
I can safely say this has never happened to me, and I was using the exact same code on a different dataset 5 minutes ago with no issues. I have also previously run this same script on at least a dozen other matrices with no errors.
I have tried calling scores.metaMDS as suggested when looking up the scores function (to help specify what type of object I'm trying to get scores from), but that function apparently does not exist. I've also tried running some old scripts that always worked in the past, with the same unfortunate results.
Any idea what I can do to address this?
Try using vegan::scores(); it could be that some other package you have loaded also has a scores() generic that is overwriting vegan::scores(). You can also try the much more specific vegan:::scores.metaMDS() if the whole S3 system has gotten clobbered.
Beyond that, restart R (in RStudio, find the Restart R option in the menus) so you get a clean session and try running your code again.
I I tried vegan:::scores.metaMDS() without restarting RStudio and it works ! Thanks !!!

What exactly does Source on Save mean or do?

Despite numerous searches, I can't seem to find a clear explanation as to what "Source on Save" means in RStudio.
I have tried ?source and the explanation there isn't clear, either.
As far as I can tell, it seems to run the script when I hit Save, but I don't understand the relevance/significance of it.
In simple terms, what exactly does Source on Save do and why would/should I use it?
This is kind of a shortcut to save and execute your code. You type something, save the script and it will be automatically sourced.
Very useful for short scripts but very annoying for time consuming longer scripts.
So sourcing is basically running each line of your file.
EDIT:
SO thinking of a scenario where this might be useful...
You developing a function which you will later put into a package... So you write this function already in an extra file but execute the function for testing in the command line...
Normally, you have to execute the whole function again, when you changed something. While using "Source on Save" the function will be executed and you can use Ctrl + 2 to jump into command line and test the function directly.
Since I am working with R, my datasets are much bigger. But I am remembering starting coding in python and vi, I updated my setting in a way to execute the code on save, since these little scripts where done in less then 10 seconds...
So maybe it is just not standard to work with small datasets... But I can still recommend it, for development, to use only 10% of a normal dataset. It will speed up the graphics creation and a lot of other things as well. Test it with the complete dataset every now and then.

RSAP package to connect to SAP through R (windows)

I'd need to be able to grab data straight from into R without going through using its GUI. I've found that the RSAP package seems to be exactly what I'm looking for.
I followed the steps recommended by Piers and Alvaro Tejada Galindo (made it work on windows environment) and here is where I'm stuck:
managed to compile the RSAP package
managed to install it
everything is looking in good shape when I run library(RSAP)
whatever i try in the RSAPConnect command, my R session crashes without any log or tools to be able to debug.
Of course I've tried a few combinations of arguments in this command, but in every single case it still crashed without me knowing why. It does not matter whether i enter a valid ashost or just aaa for instance, still crashes...
Here is the code I was thinking would work (of course I added stars in there):
conn <- RSAPConnect(ashost = "*****.****.com", sysnr = "00", client = "410",
user = "*****", passwd = "*********", TRACE = "3")
Has anyone experienced something similar ? I don't even know in which direction to look to try and make this work. In fact I'd have expected some error message like "server could not be reach" for instance should the ashost not be right, but none of that happens.
I'd appreciate any assistance on this.
Thanks ahead for your support.
Kind regards
After some talking with Piers Harding, it appears that the segfault happens because of some code changes between previous version and version 3.x, which I use.
M. Alvaro Tejada Galindo also tried to use RSAP on a windows machine like me, but if you read his post, you'll see that he was using R 2.15.0 at the time.
Unfortunately I do not have the skills to locate these changes and make the required adjustments within the RSAP code.
Piers did confirm though that RSAP is still working great using R latest build for linux.
Lastly, for those like me who struggled to find the NW RFC library, you can find it on GitHub.
If this can help anyone...
Well I thought I'd add this as another answer.
It is possible to code some vba embedded in an excel file to go fetch stuff into SAP. The interesting part is that I just ran into some code to run a specific vba macro from a specific excel file, all from R :
# Open a specific workbook in Excel:
xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open("C:\\Excel_file.xlsm")
# Run the macro called "MyMacro"
vxlApp$Run("MyMacro")
# Close the workbook (and save it) and quit the app:
xlWbk$Close(TRUE)
vxlApp$Quit()
# Release resources:
rm(xlWbk, xlApp)
So in the end, if your macro is set up to grab and store the SAP data, all you have to do next is just read this file using XLConnect or any other package as you'd normally do, and you're all set !

RStudio does not display any output in console after entering code

The problem is that when I run the code, there's no return in the console; I mean it does run the code, but does not return any output.
For example, if I write
v <- c(1, 2, 3, 4, 5)
v
I would expect in return
[1] 1 2 3 4 5
But it's not working.
I have version RStudio Version 0.98.1079 and R Version 3.1.1
Possibility 1 (until the + sign was mentioned): I was wondering if you had been doing a tutorial where they were demonstrating the sink function and you hadn't gotten to the point where it was reversed.
> sink('out.txt') # diverts all output to a disk file
> v <- c(1,2)
> v # output went to file
> sink() # sets the output back to the console
> v
[1] 1 2
Another way would be to call closeAllConnections:
> sink('out.txt')
> v
> v
> closeAllConnections()
> v
[1] 1 2
Possibility 2: To address the lack of response with a "+" showing at the Rstudio console ... that is a sign that the R parser "thinks" the entered text has not completed a full R command. It may indicate that you haven't typed a closing bracket or parenthesis. If typing one or two of those is unsuccessful and you keep getting mor +'s then you may be successful with typing the [esc]-key. If it is showing up immediately after a restart then you should check your code for correctness and make sure that the .Rdata file is deleted from your working directory. If you don't know what that means then you may need to search for the methods appropriate to your operating system. You could also have an error in the code of one of your .rprofile files.
In any case these two possibilities have nothing to do with Rstudio per se and everything to to with the typical behavior of an R console session in pretty much any IDE.
Do the lines still start with a "+"? It is also possible you forgot to close the brackets of a function. Try "}".
I had the same issue and none of the tips mentioned here were working.
Session > Restart R did the trick for me, possibly suggesting that I had a similar problem as andrewH but was not patient enough to wait for R to behave again.
This is a very old question, but I just had the same problem with a different cause, so I thought I would describe it here case it should be useful to someone else. I was getting the regular command prompt, with nothing more, no matter what I typed at the command line. I tried multiple returns, escape, sink, traceback, closeAllConnections (which did give me a response, "error: unexpected ) in (), but then went back to the command prompt and ignored a second traceback).
Anyway after half an hour or so of pulling my hair out, up pops "View(Mid2)". Mid2 is a tibble with 8.5 million observations of 88 numeric variables. I must have tapped it in the environment pane accidentally. I suppose it just took that long for the viewer to render it. I assume that all the other things I did hit at once, because RStudio crashed immediately thereafter.
The interesting thing about this particular version of the problem is what didn't happen. The red stop sign in the upper right of the console window, that lights when R is busy, didn't light. That is unfortunate -- but understandable, if the RStudio viewer is a different process. But also, when my computer is working hard on a really big computation or IO task, the fan usually starts, but it didn't. Don't know why. . I took its absence, incorrectly, to mean no such computation was underway.
If the lines in console are starting with "+".
Save your work and close the 'RStudio' or other tool which you are using and Start it again, it worked for me.
If you are using R Studio Cloud, refresh or re-opening won't work.
Only clue from the above posts or answers is your console will always start with '+'
In my case I tried all possibilities of closing braces.
And ")" worked for me when I typed that into the console and press enter.
sink() function did nothing in R Studio Cloud
A simple mistake might have also caused this problem:
A rather lengthy command left abandoned in the console is blocking the appearance of the result line.
Thus, the console only shows that line, but the result from any code run from the source, will not appear.
To solve this, just switch to the console, remove any remaining command and try again.
Experiencing something like that explained here as an unresponsive console to the R-Code running was just devastating for me when I experienced it. But luckly although I tried every trick explained in this page, it did not work for me. At last I clicked on the "To console" option available just below the Environment, History, Connections, Tutorial Tab on the R Studio. It solved the puzzle for me just now.
The best solution I've found is closeAllConnections and/or sink which almost always work
But as a stop gap measure, View()'ing always works. It's sort of a pain but whatever you wanted to print out, surround by View and you can see it

R - "Browser()" on Error?

I have been using some R libraries to analyze some large data recently, and I find myself frustrated by waiting several hours for the beginning of an analysis, just to get to the end and receive some trivial error, like that I did not install a prerequisite library, or that one of my parameters was wrong. So, then I have to start all over, do the exact same analysis, generate the same variables that it had when it died, and wait a long time. Please note that these are not handled exceptions--they are fatal errors from R.
This is just a thought--and perhaps it is too good to be true, so please at least explain why it wouldn't work--but is there any way to cause R to execute "browser()" in the environment whenever it has a fatal error? For example, say it is executing a script, and encounters "require(notInstalledYet)". Instead of just dying, and losing all the variables in the memory, it would be great if it would give me a browser() at the place it died, so that I could at least save the variables, and at best, fix the problem (e.g. install the library) and try again.
You can change the error option to open a browser on error
options(error=browser)
the default is
options(error=NULL)

Resources