2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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\")"]. $\endgroup$ – Henrik Schumacher May 29 '18 at 11:05
  • $\begingroup$ @HenrikSchumacher Thanks for the comment. However, now I get Java(TM) Plataform SE binary critical error... $\endgroup$ – An old man in the sea. May 29 '18 at 11:09
  • $\begingroup$ @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. $\endgroup$ – An old man in the sea. May 29 '18 at 13:34
  • $\begingroup$ That's something you should ask @Szabolcs. $\endgroup$ – Henrik Schumacher May 29 '18 at 14:08
  • $\begingroup$ 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. $\endgroup$ – halirutan May 29 '18 at 14:35
4
$\begingroup$

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\")"]
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Henrik Schumacher May 29 '18 at 11:32
  • $\begingroup$ @HenrikSchumacher Actually, I'm using Windows... ;) $\endgroup$ – An old man in the sea. May 29 '18 at 11:36
  • 1
    $\begingroup$ 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 "'\\\\'". $\endgroup$ – Szabolcs May 29 '18 at 11:48
  • 2
    $\begingroup$ 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. $\endgroup$ – Szabolcs May 29 '18 at 11:50
  • $\begingroup$ @Szabolcs Double escaping? Oo Good point! Btw., this might be helpful if one has to this often: StringReplace[ ToString[source["path\gensys.R "], InputForm], {"[" -> "(", "]" -> ")"} ] $\endgroup$ – Henrik Schumacher May 29 '18 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.