Giter Club home page Giter Club logo

add-command-line-args-r-'s Introduction

Add command line arguments (R)

Copyright: Tiffany Timbers, 2023

Introduction to this repository

This is a template repository to act as a practice playground to get experience writing scripts with command line arguments.

Currently the src/count_classes.R script only works on the cancer_train.csv data set in the data directory. And it only writes the results as a csv file to the results/cancer directory. It must also be run from the root of the project to work.

You can test that the script works by running the following from the root of the project's directory:

Rscript src/count_classes.R

Exercise

Your task is to use the R package docopt to add command line arguments so that other data files can be passed to it, and so that the results can be written to other directories.

To help get you started, take a look at the docopt docs for arguments: https://github.com/docopt/docopt.R#usage.

Below, we also provide you an example of an R script that uses docopt so that it can be run at the command line to print the sum of two numbers.

"This script calculates the sum of two numbers.

Usage: sum.R <num1> <num2>
" -> doc


library(tidyverse)
library(docopt)

opt <- docopt(doc)

main <- function(num1, num2) {
  
  sum <- as.numeric(num1) + as.numeric(num2)
  paste0("The sum of ", num1, " and ", num2, " is ", sum) |> 
    print()
}

main(opt$num1, opt$num2)

To run this script from the terminal, you would navigate to the directory containing sum.R and run:

Rscript sum.R 5 7

This will lead to the script running using 5 as the value for the num1 argument, and 7 as the value for the num2 argument.
And the result being printed to the terminal:

The sum of 5 and 7 is 12

Optional/advanced exercise:

When you get the src/count_classes.R script working using positional command line arguments (as in the example above), try to improve it further by naming the arguments (we call this adding command line "options"). The docopt docs for this are here: https://github.com/docopt/docopt.R#usage

License

Add command line arguments (R) exercise contained herein are licensed under the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) License. See the license file for more information. If re-using/re-mixing please provide attribution and link to this webpage. The software code contained within this repository is licensed under the MIT license. See the license file for more information.

add-command-line-args-r-'s People

Contributors

lichunubc avatar

Watchers

 avatar

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.