Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to return to R from lua_shell() while keeping the lua state intact? #3

Closed
SugarRayLua opened this issue Apr 27, 2024 · 4 comments

Comments

@SugarRayLua
Copy link

Hi, thanks for making a great R-lua bridge! There aren't many in existence... :-)

Your lua_shell function is really helpful, as it is easier just to open the lua_shell to do some lua programming while in R instead of passing everything in a lua() call. However, it doesn't seem that one can then easily get back to R from the shell; it would be helpful if I could do some programming in the lua_shell and then return to R and then perhaps return the variables or selected functions from luajit that I programmed when in the lua_shell. Is that possible or might that be possible in a future version of luajr? The only way for now that I could figure out to get out of lua_shell is by the standard lua os.exit() method which then unfortunately returns me to the bash command line and closes my R session.

@nicholasdavies
Copy link
Owner

Hello!

If you just hit return on an empty line, the Lua shell should return you to R leaving the Lua state intact (as it operates on the default state, unless you pass in an alternative state). I didn't realise I hadn't documented this, sorry! Let me know if that isn't working for you.

Thanks,

Nick

@SugarRayLua
Copy link
Author

SugarRayLua commented Apr 28, 2024

Hi, Nick, yes, thanks, hitting return works, perfectly! Being able to go in and out of the lua shell is very handy :-)

Two follow up questions:

  1. Some R-Lua bridges allow referencing variables in the other language without returning them: e.g.
Lua> C = 5
R> test = luajr.c
R> test 
# would hope to print "5"

It doesn't look like one can do that right now in luajr; instead it looks like one always needs to do instead:

Lua> C = 5
R> test = lua("return C")
R> test
# prints "5"

Is that correct or is there another more efficient way to reference Lua values in R session?

  1. I'm not quite sure what the advantage is to creating R object in Lua with luajr versus just creating them in R (or versus creating similar Lua objects and passing them to R via lua() function)? E.g.:

Create R List in R:

R> test = list(pkgName = "luajr", command = "shell")
R> test
# prints $pkgname [1] "luajr" [2] "shell"

Create R List in Lua via a Lua table and pass to R:

Lua> test = {pkgName = "luajr", command = "shell"}
R> test2 = lua("return test")
R> test2
# prints same as in straight R example above

Create list object in lua using luajr module:
I'm actually not sure how one initializes a list with named keys and values but it seems more complicated then the two examples above, and it is not clear to me whether once properly initialized, R can directly reference the R list that was created with luajr module. For simpler array type list objects, I understand to do:

Lua> test = luajr.list
Lua> test[1] = 5

But don't quite understand how to set up a key, value list like "test" in the above examples. The documentation indicates one uses ListName(attr,x), but I can't figure out how to make that work.

Thus, the bottom line question is what would be the advantage of creating R objects inside LuaJit, and if there is an advantage, it would be helpful if you could provide an example of how to create a key, value list in Lua and answer as to whether that list created in Lua could be referenced directly from R.

Thanks again,

Sincerely,

Mike

@nicholasdavies
Copy link
Owner

  1. Yes, that's correct.
  2. I don't know. Whether there is an advantage to doing this either way would depend on what you're doing, I think.
  3. You need to use test = luajr.list() with parentheses, as it's a function. You then get/set values within Lua using e.g. test.x = 1 or test["foo"] = "hello". When this gets passed back to R, it will be an R list with entries named "x" and "foo". The v(attr) or v(attr,x) can be used to get/set other attributes you want the R list to have when it is passed back to R, such as the "class" attribute.

@SugarRayLua
Copy link
Author

SugarRayLua commented Apr 28, 2024

Thanks, Nick, I think I understand better now (part of the problem was leaving off the ()).

It looks like then by defining objects as R objects using the luajr module in lua, one is assured that when they get passed to R via the "lua('return obj')" function that one is getting the specific type of R object that one wants from lua rather than having the lua('return obj') function determine the best R object to convert the lua object it is passed to.

It also still seems that the answer to #1 still holds true: even though one has created a R list in lua as per your example ("test = luajr.list(); test.x = 1") R won't know about the variable "test" until still passed to R via a "test = lua('return test')" function call.

I think I now have enough information I need to use luajr successfully 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants