Package: pytask-r
pytask-rRun R scripts with pytask.Installationpytask-r is available onPyPIandAnaconda.org. Install it with$pipinstallpytask-r#or$condainstall-cconda-forgepytask-rYou also need to have R installed andRscripton your command line. Test it by typing
the following on the command line$Rscript--helpIf an error is shown instead of a help page, you can install R withcondaby choosing
either R or Microsoft R Open (MRO). Choose one of the two following commands. (Seeherefor
further explanation on Anaconda, R, and MRO.)$condainstall-crr-base# For normal R.$condainstall-crmro-base# For MRO.Or install install R from the officialR Project.UsageTo create a task which runs a R script, define a task function with the@pytask.mark.rdecorator. Thescriptkeyword provides an absolute path or path relative to the task
module to the R script.importpytask@pytask.mark.r(script="script.r")@pytask.mark.produces("out.rds")deftask_run_r_script():passIf you are wondering why the function body is empty, know that pytask-r replaces the
body with a predefined internal function. See the section on implementation details for
more information.Dependencies and ProductsDependencies and products can be added as with a normal pytask task using the@pytask.mark.depends_onand@pytask.mark.producesdecorators. which is explained in
thistutorial.Accessing dependencies and products in the scriptTo access the paths of dependencies and products in the script, pytask-r stores the
information by default in a.jsonfile. The path to this file is passed as a
positional argument to the script. Inside the script, you can read the information.library(jsonlite)args<-commandArgs(trailingOnly=TRUE)path_to_json<-args[length(args)]config<-read_json(path_to_json)config$produces# Is the path to the output file "../out.csv".The.jsonfile is stored in the same folder as the task in a.pytaskdirectory.To parse the JSON file, you need to installjsonlite.You can also pass any other information to your script by using the@taskdecorator.@task(kwargs={"number":1})@pytask.mark.r(script="script.r")@pytask.mark.produces("out.rds")deftask_run_r_script():passand inside the script useconfig$number# Is 1.DebuggingIn case a task throws an error, you might want to execute the script independently from
pytask. After a failed execution, you see the command which executed the R script in the
report of the task. It looks roughly like this$Rscript<options>script.r<path-to>/.pytask/task_py_task_example.jsonCommand Line ArgumentsThe decorator can be used to pass command line arguments toRscript. See the following
example.@pytask.mark.r(script="script.r",options="--vanilla")@pytask.mark.produces("out.rds")deftask_run_r_script():passRepeating tasks with different scripts or inputsYou can also repeat the execution of tasks, meaning executing multiple R scripts or
passing different command line arguments to the same R script.The following task executes two R scripts,script_1.randscript_2.r, which produce
different outputs.foriinrange(2):@task@pytask.mark.r(script=f"script_{i}.r")@pytask.mark.produces(f"out_{i}.csv")deftask_execute_r_script():passIf you want to pass different inputs to the same R script, pass these arguments with thekwargskeyword of the@taskdecorator.foriinrange(2):@task(kwargs={"i":i})@pytask.mark.r(script="script.r")@pytask.mark.produces(f"output_{i}.csv")deftask_execute_r_script():passand inside the task access the argumentiwithlibrary(jsonlite)args<-commandArgs(trailingOnly=TRUE)path_to_json<-args[length(args)]config<-read_json(path_to_json)config$produces# Is the path to the output file "../output_{i}.csv".config$i# Is the number.SerializersYou can also serialize your data with any other tool you like. By default, pytask-r also
supports YAML (if PyYaml is installed).Use theserializerkeyword arguments of the@pytask.mark.rdecorator with@pytask.mark.r(script="script.r",serializer="yaml")deftask_example():...And in your R script uselibrary(yaml)args<-commandArgs(trailingOnly=TRUE)config<-read_yaml(args[length(args)])Note that theYAMLpackage needs to be installed.If you need a custom serializer, you can also provide any callable toserializerwhich
transforms data to a string. Usesuffixto set the correct file ending.Here is a replication of the JSON example.importjson@pytask.mark.r(script="script.r",serializer=json.dumps,suffix=".json")deftask_example():...ConfigurationYou can influence the default behavior of pytask-r with some configuration values.r_serializerUse this option to change the default serializer.[tool.pytask.ini_options]r_serializer="json"r_suffixUse this option to set the default suffix of the file which contains serialized paths to
dependencies and products and more.[tool.pytask.ini_options]r_suffix=".json"r_optionsUse this option to set default options for each task which are separated by whitespace.[tool.pytask.ini_options]r_options=["--vanilla"]ChangesConsult therelease notesto find out about what is new.