Package 'mtb'

Title: My Toolbox for Assisting Document Editing and Data Presenting
Description: The purpose of this package is to share a collection of functions the author wrote during weekends for managing kitchen and garden tasks, e.g. making plant growth charts or Thanksgiving kitchen schedule charts, etc. Functions might include but not limited to: (1) aiding summarizing time related data; (2) generating axis transformation from data; and (3) aiding Markdown (with html output) and Shiny file editing.
Authors: Y Hsu [aut, cre]
Maintainer: Y Hsu <[email protected]>
License: AGPL (>= 3)
Version: 0.1.8
Built: 2024-11-18 05:09:39 UTC
Source: https://github.com/yh202109/mtb

Help Index


Add a box with specified color in an R Markdown file.

Description

add_colored_box returns a box component generated by htmltools with specified color and styles.

Usage

add_colored_box(
  type = "blue-default",
  label = "",
  info = "place details here using info option",
  bgcolor = NULL,
  width = 0.5,
  halign = "c",
  top = FALSE
)

Arguments

type

One of:

  • NULL for no default color or label

  • 'blue-default' for a steel-blue box

  • 'gray-info' for a gray box

  • 'blue-info' for a blue box

  • 'green-remainder' for a green box

  • 'yellow-warning' for a yellow box

  • 'red-stop' for a red box

label

One of:

  • NULL for no label if type is NULL or using label set by type

  • A string shown on the top of box

info

A string including the main message of the box

bgcolor

NA or a length 3 vector with integer elements between 0 to 255

width

NA or a number between 0.25 to 0.95

halign

One of:

  • NA for center aligned

  • 'c' for center aligned

  • 'r' for right aligned

top

One of:

  • NA

  • FALSE for inline

  • TRUE for top-of-page

Examples

add_colored_box( type='blue-default', info='the document include information regarding...')

Add a string with specified color or background color.

Description

add_colored_str returns a string component generated by htmltools with specified color and styles.

Usage

add_colored_str(
  text = "",
  color = c(51, 122, 183),
  alpha = 255,
  bgcolor = NULL,
  bgalpha = 51,
  fontsize = 1,
  bold = FALSE,
  it = FALSE
)

Arguments

text

A string. default="".

color

One of

  • a color name, e.g. 'red'.

  • a HEX color string, e.g. '#000000' or '#000000FF'.

  • an RGB vector for the color of text

alpha

An integer between 1 and 255 for text alpha. default=255.

bgcolor

One of

  • a color name, e.g. 'red'.

  • a HEX color string, e.g. '#000000' or '#000000FF'.

  • an RGB vector for the color of text

bgalpha

An integer between 1 and 255 for background alpha. default=51.

fontsize

A real number between 0.5 and 5.0 for font size. default=1.

bold

A logical value for bold fonts. default=FALSE.

it

A Boolean value for italic fonts. default=FALSE.

Value

a formatted string

Examples

add_colored_str("warning: read this message carefully.", color = c(255, 0, 0))

Check two tables with unique matching ids and generate reports on duplicated ids and repeated columns when applicable..

Description

bill_cross_check returns a merged data.table showing information regarding ids and repeated columns.

Usage

bill_cross_check(dt1 = NULL, dt2 = NULL, id = NULL, chk = NULL)

Arguments

dt1

A table.

dt2

A table.

id

A column name or a vector of column names

chk

A column name

Value

a data.table

Examples

bill_cross_check(data.frame(col1=c(1,2,3,3), col2=c('a','b','c','c'),
col3=c('-','=','+','-')),data.frame(col1=c(1,2,3), col2=c('a','b','c'),
col3=c('-','=','+')), id=c('col1','col2'), chk='col3')

Summarize times being purchased for individual items' from multiple grocery shopping lists.

Description

bill_cross_count returns a data.table showing how many times each items being listed on individual bills.

Usage

bill_cross_count(
  ldt = list(),
  id = NULL,
  gp = NULL,
  type = "count",
  condstr = ""
)

Arguments

ldt

A list of grocery tables in data.frame format.

id

A column name

gp

A column name or a vector of column names

type

A string in one of the following:

  • count for number of rows in each bill

  • cond for conditional counting

  • condwt for conditional counting with total in parenthesis

condstr

A string for conditional counting

Value

a data.table

Examples

bill_cross_count(list(cbind(col1=c('a','b','c'),col2=c(1,2,3)),
cbind(col1=c('d','c','d'),col2=c(4,5,6))), id='col1')

Generate a color vector

Description

Create a list of colors for a data vector by a list major colors.

Usage

color_set_palette(
  vect = c(),
  vectn = c(),
  cols = c("blue", "cyan", "darkorange"),
  black = "",
  gray9 = ""
)

Arguments

vect

A vector for groups.

vectn

An integer vector with length 0 or with the same length of vect for order of elements in vect. default=c()

cols

One of

  • A color names vector

  • An RGB triplet vector

  • A HEX vector

black

A level in vect that should be assigned to black color. default=""

gray9

A level in vect that should be assigned to gray9 color. default=""

Value

a named vector

Examples

color_set_palette( c('apple', 'orange', 'lime', 'apple'), c(2,1,3,2), 'red', 'blue')

Test a color vector

Description

Create a figure using the assigned color vector

Usage

color_test_palette(colvect = c(), type = "line")

Arguments

colvect

A vector returned by color_set_palette()

type

One of

  • 'line' for using the color vector on a line plot (Default)

  • 'box' for using the color vector on a box plot.

Value

A plot

Examples

color_test_palette( setNames(c(1,2,3,4), c('apple','orange','avocado','lime') ))

Example dataset

Description

Example dataset

Author(s)

package author


Plot labeled events

Description

Create a plot for events with labels

Usage

time_plot_event(dt, xlab = "Time", anchor = TRUE, compact = FALSE)

Arguments

dt

a data.frame with the following columns

  • idfor ID of each group

  • idnfor order of ID

  • startfor starting time

  • endfor ending time with arrow head

  • labelfor labeling the starting time

  • labelendfor labeling the ending time of a interval

  • typefor event type as one of p (point), i (interval), b (box)

  • colora string for event color

xlab

A string for the x-axis title

anchor

A Boolean value for the vertical lines linking start to the x-axis

compact

A Boolean value for reducing the vertical spacing when applicable

Value

a plot

Examples

library(ggplot2)
dt = data.frame( id=paste('member',c(rep(c(1,2,3),each=3),3),sep=""),
idn=c(rep(1,3),rep(-1,3), rep(2,4)),
start=1800*c(0,1,2, 0.5, 1.2, 3, 1,2,3,4),
end=1800*c(2,NA,3, 2, 6, NA, 2,2.5,3, 3.5),
label=c(paste('event-',seq(1,10),sep='')),
labelend=c('','','?',')','','','','','>','X'),
type=c('b', 'p', 'i','i','p','p','p','b','i','i' ),
color=c('stove', 'oven', 'oven','oven','stove','oven','oven','other','stove','oven' )
 )
time_plot_event( dt )

Plot periods of events

Description

Create a plot for event periods by ID

Usage

time_plot_interval(
  dt,
  xlab = "DateTime",
  ylab = "ID",
  legend_title = "Group",
  arrow_wt = 1,
  arrow_color = "black"
)

Arguments

dt

a data.frame with the following columns

  • idfor ID of each interval

  • idnfor order of ID

  • startfor starting time

  • endfor ending time with arrow head

  • labelfor labeling the starting time

xlab

A string for the label of X-axis

ylab

A string for the label of Y-axis

legend_title

A string for the title of legend

arrow_wt

An integer for the weight of arrow

arrow_color

A string for the color of arrow

Value

a plot

Examples

library(ggplot2)
dt = data.frame( id=c('ID01','ID12','ID3'), idn=c(1,3,2), start=1800*c(0,1,2), end=1800*c(2,-1,3),
label=c('A','B','C') )
time_plot_interval( dt, xlab='Time', ylab='ID', legend_title='Group', arrow_wt=3,
arrow_color='gray')

Transformation for continuous data with a finite number of distinct values

Description

trans_composition() derives a transformation from a numerical vector with a smaller number (ideally < 30) of distinct values. The return can be used with function ggplot::scale_x_continuous() or ggplot::scale_y_continuous() to create a desired axis.

Usage

trans_composition(x = NULL, nb = 30, brk = NA, dab = NA, dgrd = NA, dgrd2 = NA)

Arguments

x

A numerical vector used in a plot as (typically) x

nb

An integer for the maximum number of breaks. Default=30

brk

One of

  • A numerical value within range(x). All values after the value will be spaced equally

  • NA or a numerical value that is greater than or equal to max(x). All values will be plotted in the original scale

  • A numerical value that is smaller than or equal to max(x). All values will be plotted in equal space

dab

One of

  • NA for a value calculated automatically

  • A number for the distance after brk

dgrd

One of

  • NA for a value calculated automatically

  • A number for the minimum space between major grids

dgrd2

One of

  • NA for a value calculated automatically

  • A number for the minimum space between major grids

Value

A transformation function

Examples

library(ggplot2)
pdt=data.frame(x=rep(c(0.5, 1, 10,11,12, 100, 1000), each=5))
pdt$y=pdt$x+rnorm(length(pdt$x))
t=trans_composition(pdt$x,brk=50, dab=3)
ggplot(pdt, aes(x=x, y=y))+geom_point()+scale_x_continuous(trans=t)

Transformation for continuous data with a finite number of distinct values

Description

trans_loglinear() derives a log transformation from a numerical vector with a smaller number (ideally < 30) of distinct values.. The return can be used with function ggplot::scale_x_continuous() or ggplot::scale_y_continuous() to create a desired axis.

Usage

trans_loglinear(x = NULL, nb = 30, int = NA, scale = NA, mindist = 0.03)

Arguments

x

A numerical vector used in a plot as (typically) x

nb

An integer for the maximum number of breaks. Default=30

int

One of

  • NA for a value calculated automatically

  • A real number (>=0) for the shift before log transform

scale

One of

  • NA for a value calculated automatically

  • A real number (>0) for the scale before log transform

mindist

One of

  • NA for a default value set to 0.03

  • A real number between 0 and 0.2 for the minimum distance ratio between major ticks

Value

A transformation function

Examples

library(ggplot2)
pdt=data.frame(x=rep(c(0.5, 1, 10,11,12, 100, 1000), each=5))
pdt$y=pdt$x+rnorm(length(pdt$x))
t=trans_loglinear(pdt$x)
ggplot(pdt, aes(x=x, y=y))+geom_point()+scale_x_continuous(trans=t)