rticulate/import

Similar functionality for R scripts

Closed this issue · 11 comments

Is it possible to have a similar functionality for R scripts e.g. say I had a shiny app with directory structure:

global.R
ui.R
server.R
utils/
  connect.R

and connect.R had the template:

# function to connect to a data base
connect <- function(...) {
  # make some calls to the utility functions defined in connect.R
}

# utility functions
...

would it be possible to have something like (in global.R):

import::from("utils/connect.R", "connect")

so I get connect() without all the noise from the utility functions.

I'm guessing this might be pushing it, but it'd be super neat if possible.

Let me think about that :)

My initial thought is that it will be possible, but would require parsing and evaluating the whole script in an environment, after which pulling it out would be easy. But, wondering if this is a good idea.

Will you try out the latest push? Here's an "example": https://gist.github.com/smbache/1704489003d010fcf610

Added some documentation to the README. Seems to work fine, but some testing IRL would be nice.

Cheers, I'll make some time to test this out.

Sorry for the delay. Will be modifying a shiny app at work to use this. Expect an update on Monday.

FYi, I updated the functionality slightly so that scripts are sourced as if in the directory of the script (ie chdir = TRUE in sys.source). This makes relative paths easier to manage...

Testing it out on the second shiny app today. No bugs found so far!

Perfect!

I've used it quite some the past week. It's very useful! ;)

Did you get the version where relative paths are handled better?

Also not that using import::here in a script/module makes the imports local to that file, as opposed to exposed in eg "imports".

I'll close this now: if you experience any issues, please reopen.