2

Let's imagine that I have a file called gensys.R where a function gensys is defined, which I would like to use in the mathematica environment and work with it.

When using RLink, I've tried

REvaluate[
 "source(" path\gensys.R ")"]

However, I receive an error message when I run the command, related to the double use of the " sign...

Any help would be appreciated.

An old man in the sea.
  • 2,527
  • 1
  • 18
  • 26
  • You have to escape quotation marks in a string with """. The same hold true for backslashes (you need "\"). So try this: REvaluate["source(\"path\\gensys.R\")"]. – Henrik Schumacher May 29 '18 at 11:05
  • @HenrikSchumacher Thanks for the comment. However, now I get Java(TM) Plataform SE binary critical error... – An old man in the sea. May 29 '18 at 11:09
  • @HenrikSchumacher Is there a pdf user guide which I can learn about using RLink. The documentation on the wolfram site seems to be lacking... maybe it's some inattention on my part. – An old man in the sea. May 29 '18 at 13:34
  • That's something you should ask @Szabolcs. – Henrik Schumacher May 29 '18 at 14:08
  • The right one to ask would be @LeonidShifrin, as was working on this. Have you ensured, that your path is indeed correctly escaped and exists? It seems odd, that such a simple call should crash the JRE. – halirutan May 29 '18 at 14:35

1 Answers1

4

Well, the comment by Henrik was helpful in reaching the right answer. In R, the sign we should use is / not \ nor double \.

So, the answer should be

REvaluate["source(\"path1/path2/gensys.R\")"]
An old man in the sea.
  • 2,527
  • 1
  • 18
  • 26
  • Path seperators should be platform dependant, even in R. I first assumed you where using Windows but now I think that you work on a unixoid OS, right? – Henrik Schumacher May 29 '18 at 11:32
  • @HenrikSchumacher Actually, I'm using Windows... ;) – An old man in the sea. May 29 '18 at 11:36
  • 1
    Windows usually supports both \ and / as a path separator. To write \ in an R string, one needs to escape it as "\\". You are writing an R string inside of a Mathematica string, which needs even more escaping. Now it would look like "\"\\\\\"". Alternatively, you can use single quotes for R strings, which would make this "'\\\\'". – Szabolcs May 29 '18 at 11:48
  • 2
    I believe @Henrik is right, and the path separator depends on the OS, not on the language. The behaviour you observe is due to the need to escape in R fist, then to escape in Mathematica a second time. Of course, the most practical solution is to use / as a path separator (even on Windows) and to use single quotes for R strings. – Szabolcs May 29 '18 at 11:50
  • @Szabolcs Double escaping? Oo Good point! Btw., this might be helpful if one has to this often: StringReplace[ ToString[source["path\gensys.R "], InputForm], {"[" -> "(", "]" -> ")"} ] – Henrik Schumacher May 29 '18 at 11:58
  • @Szabolcs by the way, What's preferable, to define the input variables of the gensys function in mathematica, and then run the gensys function from R, or define the data in R input and then run the REvaluate ? – An old man in the sea. May 29 '18 at 13:31