Giter VIP home page Giter VIP logo

Comments (7)

eddelbuettel avatar eddelbuettel commented on June 3, 2024

Absolutely.

Look at install2.r to get a feel for docopt. You need to pass options for

  • what package (mandatory, I say)
  • what dir (optional, default scripts/)
  • what file to run (mandatory, file.r above)

plus args. The it becomes

scriptRunner -p mypkg file.r arg1 args2 arg3

I am close to a new release. We could squeeze this in, or you could just start with a local version.

from littler.

1beb avatar 1beb commented on June 3, 2024

Ok, I think I have this. The only thing I'm not sure about is that if the system.file() returns nothing, I think it should be an error for the user but I'm not sure how to raise the error. Is it just stop? I'm also not sure how I should invoke the script itself, I'm just using RScript below.

Let me know what you think and I'll update appropriately.

#!/usr/bin/env r
#
# A simple example to run a script, with arguments contained in an
# installed pacakge 
#
# Copyright (C) 2014 - 2015  Carl Boettiger and Dirk Eddelbuettel
#
# Released under GPL (>= 2)

## load docopt and devtools from CRAN
suppressMessages(library(docopt))       # we need docopt (>= 0.3) as on CRAN

## configuration for docopt
doc <- "Usage: scriptRunner [-h] [-p PKG] [-d DIR] [-f FILE] [ARGS...]

-p --package PKG      The name of the installed package that contains the script [default: example]
-d --dir DIR          The directory of the script. R package convention is inst/scripts so [default: scripts/]
-f --file FILE        The filename of the R script to run
-h --help             Show this help text
where ARGS... is one or more arguments to the target script repositories.
Examples:
  scriptRunner -p yourpackage -f yourfile.r arg1 arg2 arg3
  scriptRunner -p yourpackage -f yourfile.r -d directory arg1 arg2 arg3 
scriptRunner is part of littler which brings 'r' to the command-line.
See http://dirk.eddelbuettel.com/code/littler.html for more information.
"
## docopt parsing
opt <- docopt(doc)

script_path <- system.file(opt$dir,opt$file,package = opt$package)
if(script_path = "") {
  stop("No script found with that name or directory")
}

system(paste(
  "RScript",
  script_path,
  paste(opt$args,collapse = " ")
))

from littler.

eddelbuettel avatar eddelbuettel commented on June 3, 2024

Looks good! Regarding your questions:

The only thing I'm not sure about is that if the system.file() returns nothing, I think it should be an error for the user but I'm not sure how to raise the error. Is it just stop?

Pretty much. When system.file() cannot find the file it returns `""`` which we can (and should) test for.

I'm also not sure how I should invoke the script itself, I'm just using RScript below.

Standard Unix 'shebang' (Google it!) style -- #!/usr/bin/e, or if you like the indirection #!/usr/bin/env r. If you're on Windoze, none of that works.

from littler.

eddelbuettel avatar eddelbuettel commented on June 3, 2024

Oh, I didn't see that at first but

system(paste(
  "RScript",
  script_path,
  paste(opt$args,collapse = " ")
))

That makes no sense, really. If what you have in subdir of a package is an executable script then ... you really only to have the one call to system.file() to compute the location.

from littler.

1beb avatar 1beb commented on June 3, 2024

Based on your comments, I'm not sure what changes I need to make. If I don't call the package subdirectory, I just get "", where if I pass system.file("scripts", "file.r", pacage = "pkg"), I get the full path to the script file in the package library. Also, besides including the shebang at the head of the file (it is) I'm not sure how to implement it. Was there something more that needed to be done?

#!/usr/bin/env r

RE:

That makes no sense, really. If what you have in subdir of a package is an executable script then ... you really only to have the one call to system.file() to compute the location.

I wasn't using system.file a second time, I was using system() to run the line:

Rscript path arg1 arg2 arg3

I'm a bit out of my bandwidth here, please be explicit about the changes you need.

from littler.

eddelbuettel avatar eddelbuettel commented on June 3, 2024

Your original question was about

R -e 'system.find("scripts","file.r",package="mypackage")'

You can do, approximately, this:

Compute path

R -e 'system.find("scripts","file.r",package="mypackage")'

yielding

edd@max:~$ Rscript -e 'cat(system.file("examples", package="littler"),"\n")'
/usr/local/lib/R/site-library/littler/examples 
edd@max:~$ 

Use it

Here we use the above to fill in the path -- ignore that install2.r may be in $PATH too:

Rscript $(Rscript -e 'cat(system.file("examples", package="littler"))')/install2.r --help

Putting it together:

edd@max:~$ Rscript $(Rscript -e 'cat(system.file("examples", package="littler"))')/install2.r --help
Usage: install2.r [-r REPO...] [-l LIBLOC] [-h] [-d DEPS] [--error] [--] [PACKAGES ...]

-r --repos REPO     repository to use, or NULL for file [default: getOption]
-l --libloc LIBLOC  location in which to install [default: /usr/local/lib/R/site-library]
-d --deps DEPS      install suggested dependencies as well [default: NA]
-e --error          throw error and halt instead of a warning [default: FALSE]
-h --help           show this help text

where PACKAGES... can be one or more CRAN package names, or local (binary or source)
package files (where extensions .tar.gz, .tgz and .zip are recognised). Optional
arguments understood by R CMD INSTALL can be passed interspersed in the PACKAGES, though
this requires use of '--'.

Examples:
  install2.r -l /tmp/lib Rcpp BH                         # install into given library
  install2.r -- --with-keep.source drat                  # keep the source
  install2.r -- --data-compress=bzip2 stringdist         # prefer bz2 compression

install2.r is part of littler which brings 'r' to the command-line.
See http://dirk.eddelbuettel.com/code/littler.html for more information. 
edd@max:~$ 

So nothing to do here. If you want to work at the shell you already can.

Enjoy.

from littler.

1beb avatar 1beb commented on June 3, 2024

Ah, I see. No change required because this is already possible without anything special nor extra. Just a slightly bit of shell syntax.

Thanks!

from littler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.