Weekly R-Tips: Importing Packages and User Inputs

Number 1: Importing Multiple Packages

Anyone who has used R for some time has written code that required the use of multiple packages. In most cases, this will be done by using the library or require function to bring in the appropriate extensions.


That’s nice and gets the desired result, but can’t we just import all the packages we need in one or two lines. Yes we can, and here is the one line of code to do that.

libs <- c("forecast", "ggplot2", "stringr", "lubridateee", "rockchalk")
sapply(libs, library, character.only=TRUE, logical.return=TRUE)

libs <- c("forecast", "ggplot2", "stringr", "lubridateee", "rockchalk")
lapply(libs, require, character.only=TRUE)

Number 2: User Input

One side project that I hope to start on is a process whereby I can interact with R and select options that will result in particular outcomes. For example, let’s say you’re trying to put together a script that manages a weekly list. A good first step would be a list of options that the user would see and be prompted to select an option. Here is how R can be used to get user input in such circumstances.

lopts <- cat("
1. Add an item
2. Delete an item
3. Print the list
4. Quit

action <- readline("Choose an option: ")

2 thoughts on “Weekly R-Tips: Importing Packages and User Inputs”

  1. Pingback: Atajo para importar mĂșltiples paquetes en R | To the mean!

  2. Pingback: Weekly R-Tips: Importing Packages and User Inputs | Mubashir Qasim

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top