Module # 13 Shiny Web App

 

Creating My Own Package in R via RStudio

1) This week I learn about Shiny, a product from R that allows us to create web applications in R itself. This is a very powerful tool that uses client-server communication, HTML, layout and JavaScript programming to make web applications. This however is not what we will be using today complete our assignment. This week I will be creating my own R package that will be available via my GitHub. 

2) I will be creating an R package that will allow users to create their own tier list in RStudio. This will help visualize what variables or objects are more important than others in rank. Users will be able to (hopefully) rank variables or other information in their data to help explain how things are ranked on a arbitrary scale according to the audience in mind. Such as creating a tier list of books based off of how much they sold, their reviews, or what genres are better. It can use objectively to reorganize the audiences focus or used to state opinions of the user. I have not seen another R package tackle this so I will try to do it myself. Here we go


3) Here is the following code I am using to run my package:


#' Create a Tier List
#'
#' @param tiers A character vector of tier names.
#' @param colors A character vector of tier colors (optional).
#' @param items A character vector of items to rank (optional).
#'
#' @return A list object representing the tier list.
#' @examples
#' tier_list <- create_tier_list(tiers = c("S", "A"), items = c("Item1", "Item2"))
#' @export
create_tier_list <- function(tiers = c("S", "A", "B"),
                             colors = NULL,
                             items = NULL) {
  if (is.null(colors)) {
    colors <- grDevices::rainbow(length(tiers))  # Default to distinct colors for tiers
  }

  if (length(tiers) != length(colors)) {
    stop("The number of tiers and colors must match.")
  }

  if (is.null(items)) {
    items <- character()  # Initialize with an empty vector if no items are provided.
  }

  list(
    tiers = tiers,
    colors = colors,
    items = items,
    assignments = setNames(rep(NA, length(items)), items)  # Initialize assignments to NA
  )
}


#' Assign an Item to a Tier
#'
#' @param tier_list A tier list object created by `create_tier_list`.
#' @param item The item to assign.
#' @param tier The tier to assign the item to.
#'
#' @return Updated tier list object.
#' @examples
#' tier_list <- create_tier_list(items = c("Item1", "Item2"), tiers = c("S", "A"))
#' tier_list <- assign_to_tier(tier_list, "Item1", "S")
#' @export
assign_to_tier <- function(tier_list, item, tier) {
  if (!item %in% tier_list$items) {
    stop("Item is not in the tier list.")
  }
  if (!tier %in% tier_list$tiers) {
    stop("Tier does not exist.")
  }
  tier_list$assignments[item] <- tier
  tier_list
}


#' Add an Item to the Tier List
#'
#' @param tier_list A tier list object created by `create_tier_list`.
#' @param item The item to add.
#'
#' @return Updated tier list object.
#' @examples
#' tier_list <- add_item_to_tier_list(tier_list, "Item3")
#' @export
add_item_to_tier_list <- function(tier_list, item) {
  if (item %in% tier_list$items) {
    stop("Item already exists in the tier list.")
  }

  tier_list$items <- c(tier_list$items, item)
  tier_list$assignments[item] <- NA  # Initialize the item's tier assignment to NA
  tier_list
}


#' Move an Item Between Tiers
#'
#' @param tier_list A tier list object created by `create_tier_list`.
#' @param item The item to move.
#' @param new_tier The new tier to move the item to.
#'
#' @return Updated tier list object.
#' @examples
#' tier_list <- move_item_between_tiers(tier_list, "Item1", "A")
#' @export
move_item_between_tiers <- function(tier_list, item, new_tier) {
  if (!item %in% tier_list$items) {
    stop("Item is not in the tier list.")
  }
  if (!new_tier %in% tier_list$tiers) {
    stop("Tier does not exist.")
  }

  tier_list$assignments[item] <- new_tier
  tier_list
}


#' Plot tier list with advanced features
#'
#' This function creates an advanced plot for the tier list.
#' @name plot_tier_list_advanced
#' @param tier_list The tier list object to be plotted
#' @export

library(ggplot2)
library(dplyr)

# Plot a Tier List
plot_tier_list_advanced <- function(tier_list) {
  # Prepare data for tiers
  tier_data <- data.frame(
    tier = rev(tier_list$tiers),
    y_pos = seq_along(tier_list$tiers),   # Reversed so "S" is at the top
    colors = rev(tier_list$colors)
  )

  # Assign each item to its corresponding tier
  item_data <- data.frame(
    items = names(tier_list$assignments),
    tier = tier_list$assignments,
    stringsAsFactors = FALSE
  )

  # Assign y positions based on tier
  item_data$y_pos <- as.numeric(factor(item_data$tier, levels = rev(tier_list$tiers)))

  # Adjust x positions for each tier
  item_data <- item_data %>%
    group_by(y_pos) %>%
    mutate(x_pos = seq(1, length.out = n(), by = 1)) %>% # Ensure even spacing
    ungroup()

  # Debugging: Print item_data to verify correctness
  print("Item Data:")
  print(item_data)

  # Maximum x_pos for scaling plot width
  max_x_pos <- max(item_data$x_pos, na.rm = TRUE)

  # Create the plot
  ggplot() +
    # Draw colored rectangles for each tier
    geom_rect(data = tier_data,
              aes(xmin = 0, xmax = max_x_pos + 2,
                  ymin = y_pos - 0.5, ymax = y_pos + 0.5, fill = colors),
              inherit.aes = FALSE, alpha = 0.8) +

    # Add the items to the plot
    geom_text(data = item_data,
              aes(x = x_pos, y = y_pos, label = items),
              size = 5, color = "black", hjust = 0.5, vjust = 0.5) + # Center items

    # Adjust x-axis to hide labels and ticks
    scale_x_continuous(limits = c(0, max_x_pos + 2), labels = NULL) +

    # Adjust y-axis for tier labels
    scale_y_continuous(breaks = seq_along(tier_list$tiers), labels = rev(tier_list$tiers)) +

    # Set fill for each tier's rectangle
    scale_fill_identity() +

    # Theme adjustments
    theme_minimal() +
    theme(
      axis.title = element_blank(),
      axis.text.x = element_blank(),
      axis.ticks.x = element_blank(),
      axis.text.y = element_text(size = 14),
      axis.ticks.y = element_blank(),
      panel.grid = element_blank(),
      legend.position = "none",
      plot.background = element_rect(fill = "#D3D3D3"),
      plot.margin = margin(10, 10, 10, 10)
    )
}



4) This code defines a set of functions to create and manipulate a tier list, which organizes items into ranked groups (tiers) for visual representation. The create_tier_list function initializes a tier list, allowing the user to specify tier names, optional colors, and items. If no colors are provided, it assigns distinct default colors, ensuring the number of colors matches the tiers. Items are initially unassigned (NA). The assign_to_tier function updates the tier list by assigning a specific item to a specified tier, ensuring both exist in the list. The add_item_to_tier_list function allows adding a new item while initializing it with no tier assignment. The move_item_between_tiers function reassigns an item to a different tier, verifying its presence in the list and the validity of the new tier.

5) The plot_tier_list_advanced function visualizes the tier list using ggplot2. It organizes tier data into rows with distinct colors and positions items within their tiers, starting from the left and spacing them evenly. This prevents clustering in the middle of the plot. The plot uses reversed tiers to display higher ranks (like "S") at the top, hides x-axis details for simplicity, and uses custom y-axis labels for the tier names. A clean, minimalistic theme and gray background enhance the presentation, making the tier list visually intuitive and professional.




5) Here is an example of how this package can be used:

> # Load necessary libraries

> library(ggplot2)

> library(dplyr)

>library(Khan)

> # Create tier list with valid colors

> tier_list <- create_tier_list(

+     tiers = c("S", "A", "B"),

+     colors = c("gold", "pink", "#cd7f32"),  # Replace "bronze" with its hex code

+     items = c("Item1", "Item2", "Item3", "Item4", "Item5")

+ )

> # Assign items to tiers

> tier_list <- assign_to_tier(tier_list, "Item1", "S")

> tier_list <- assign_to_tier(tier_list, "Item2", "A")

> tier_list <- assign_to_tier(tier_list, "Item3", "B")

> tier_list <- assign_to_tier(tier_list, "Item4", "S")

> tier_list <- assign_to_tier(tier_list, "Item5", "A")

> # Plot the tier list

> plot_tier_list_advanced(tier_list)







 6) Shown above is a quick example of what you can do with this package. You can rename the "Item"s and the names of the tiers on the side, as well as the color. 


7) If you want to checkout this package it is available via my GitHub here. Feel free to reach out about any comments questions or concerns about this package, there is quite a few more improvements about it that I think needs testing. Overall however, its not bad for my first R package ever. 


Comments

Popular posts from this blog

Module # 7 R Object: S3 vs. S4 assignment

Module # 4 Programming Structure