Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed Image_sources/PreferenceBreakdown.png
Binary file not shown.
Binary file removed Image_sources/PreferenceBreakdown2.png
Binary file not shown.
Binary file removed Image_sources/PreferenceBreakdown3.png
Binary file not shown.
Binary file removed Image_sources/Rankand Analysis.png
Binary file not shown.
Binary file removed Image_sources/RankandAnalysis2.png
Binary file not shown.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export(get_video_and_rank)
export(get_video_ids)
export(preprare_data)
export(print_videos)
export(tabulate_overallsummary)
export(tabulate_summary)
export(visualize)
export(visualize_channel)
export(visualize_proportion)
Expand All @@ -27,6 +29,7 @@ import(ggtext)
import(gridExtra)
import(httr)
import(jsonlite)
import(kableExtra)
import(lubridate)
import(stringr)
import(tidyr)
Expand Down
95 changes: 85 additions & 10 deletions R/preference_analysis.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library(dplyr)
library(ggplot2)
library(kableExtra)

#' Helps to prepare data for preference breakdown
#'
Expand Down Expand Up @@ -102,6 +103,76 @@ calculate_and_display_summary<-function(merged_info)
c("Category", "Count", "Duration in mins", "Percecntage")))
}

#' Tabulates summary on least and most favorite category
#'
#' @param merged_info
#'
#' @return: gives table
#'
#' @import kableExtra
#' @export
#'
#' @example
#' merged_data <- data.frame(categoryid = c("22", "27"),
#' channelId = c("UCAuUUnT6oDeKwE6v1NGQxug", "UCtYLUTtgS3k1Fg4y5tAhLbw"),
#' categoryTitle = c("Entertainment","Education"),
#' count = c(1, 19),
#' duration_sum = c(21.05, 399.95),
#' percentage_duration = c(5, 95))
#' visualize(merged_data)
#' tabulate_summary(merged_data)
tabulate_summary<-function(merged_info)
{
min_category <- min(merged_info$count)
max_category <- max(merged_info$count)
min_categorylist <- merged_info$categoryTitle[merged_info$count == min_category]
max_categorylist <- merged_info$categoryTitle[merged_info$count == max_category]
min_category_duration <- sum(merged_info$duration_sum[merged_info$count == min_category])
max_category_duration <- sum(merged_info$duration_sum[merged_info$count == max_category])
min_category_percentage <- sum(merged_info$percentage_duration[merged_info$count == min_category])
max_category_percentage <- sum(merged_info$percentage_duration[merged_info$count == max_category])


summary_data <- data.frame(
Likeness = c("Least Favorite", "Most Favorite"),
categories = c(paste(paste(min_categorylist, collapse = ", ")), paste(paste(max_categorylist, collapse = ", "))),
Time_Spend_Minutes = c(paste(round(min_category_duration), "min"),paste(round(max_category_duration), "min")),
Percentage_Time_Spent = c(paste(round(min_category_percentage), "%"), paste(round(max_category_percentage), "%"))
)

top_10_videos_print <- kbl(summary_data, caption = "Summary on Least and Most favourite category") %>%
kable_classic(full_width = F, html_font = "Cambria")

print(top_10_videos_print)
}

#' Tabulates the overall summary on category
#'
#' @param merged_info
#'
#' @return
#' @import kableExtra
#'
#' @export
#' @example
#' merged_data <- data.frame(categoryid = c("22", "27"),
#' channelId = c("UCAuUUnT6oDeKwE6v1NGQxug", "UCtYLUTtgS3k1Fg4y5tAhLbw"),
#' categoryTitle = c("Entertainment","Education"),
#' count = c(1, 19),
#' duration_sum = c(21.05, 399.95),
#' percentage_duration = c(5, 95))
#'
#' tabulate_overallsummary(merged_data)
tabulate_overallsummary<-function(merged_info)
{
splitted_data <- merged_info[, c("categoryTitle", "count", "duration_sum", "percentage_duration")]
colnames(splitted_data) <- c("Category", "Count", "Duration in mins", "Percecntage of watch time")
table_channel_stats <- kbl(splitted_data, caption = "Overall Summary on breakdown") %>%
kable_classic(full_width = F, html_font = "Cambria")

print(table_channel_stats)
}

#' Helps to display category in pi chart
#'@description
#'This method helps to create pi chart
Expand All @@ -123,9 +194,9 @@ visualize<-function(merged_info)
category_plot <- ggplot(merged_info, aes(x = " ", y = count, fill = categoryTitle)) +
geom_bar(stat = "identity") +
coord_polar("y", start = 0) +
labs(title = "Breakdown on your watch category", fill = "categoryTitle") +
labs(title = "Categorical breakdown in your watch history", fill = "categoryTitle") +
theme_minimal()+
labs(x="")
labs(x="", fill = "Categories")
print(category_plot)
}

Expand All @@ -146,15 +217,17 @@ visualize<-function(merged_info)
#' duration_sum = c(21.05, 399.95),
#' percentage_duration = c(5, 95))
#' visualize_channel(merged_data)
visualize_channel<-function(merged_info)
visualize_channel <- function(merged_info)
{
ggplot(merged_info, aes(x = channelName, y = 1, size = count, color = channelName)) +
geom_point(alpha = 0.7,show.legend = FALSE) +
geom_text(aes(label = round(duration_sum)), vjust = -0.5, size=5, color= 'white')+
scale_size_continuous(range = c(20, 70)) +
labs(x = "Channel Name", y = NULL, size = "Duration", title=" Watch time (in minutes) per channel")+
theme(axis.text.y=element_blank())+
labs(y="")
ggplot(merged_info, aes(x = channelName, y = 1, size = count, color = channelName)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
geom_text(aes(label = channelName), vjust = -8, size = 5, color = 'black') + # corrected
geom_text(aes(label = paste("count: ", round(count))), vjust = 8, size = 5, color = 'black') + # corrected
geom_text(aes(label = paste(round(duration_sum), " min")), vjust = 10, size = 5, color = 'black') + # corrected
scale_size_continuous(range = c(20, 70)) +
labs(x = "Channel Name", y = NULL, size = "Duration", title = "Channel View Summary: Watch Time and Video Duration") +
theme(axis.text.y = element_blank(), axis.text.x = element_blank()) +
labs(y = "", x = "") # corrected
}

#' Main function that helps to co-ordinate all the functions
Expand All @@ -181,6 +254,8 @@ get_Preference_Breakdown<-function(video_ids)
}
video_ids <- gsub(" ", "", video_ids)
merged_info <- preprare_data(video_ids)
tabulate_summary(merged_info)
tabulate_overallsummary(merged_info)
calculate_and_display_summary(merged_info)
visualize(merged_info)
visualize_channel(merged_info)
Expand Down
10 changes: 7 additions & 3 deletions R/rank_and_analyse_top_videos.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library(ggplot2)

library(kableExtra)

#' @param channelId
#'
Expand Down Expand Up @@ -27,6 +27,7 @@ get_video_ids <-function(channelId,year)
#' @param channelID
#' @param year
#'
#' @import kableExtra
#' @return videos a dataframe
#' @export
#'
Expand Down Expand Up @@ -60,6 +61,7 @@ get_video_and_rank<-function(channelID, year)
#' @param reverse
#'
#' @export
#' @import kableExtra
#'
#' @examples
#' dummy_data <- data.frame(
Expand All @@ -78,7 +80,9 @@ print_videos<-function(filtered_video_details, reverse = TRUE)
if(reverse){
top_videos<-top_videos[nrow(top_videos):1, ]
}
print(top_videos)
top_10_videos <- kbl(top_videos, caption = "TOP 10 Videos") %>%
kable_classic(full_width = F, html_font = "Cambria")
print(top_10_videos)
}

#' Helps to calculate proportion of views, count,comments, duration
Expand Down Expand Up @@ -141,7 +145,7 @@ visualize_proportion<-function(proportion_data)
{
ggplot(proportion_data, aes(x = Category, y = Proportion,fill = Category)) +
geom_bar(stat = 'identity', width = 0.5) +
labs(x = "", y = "Proportion (%)", title = "Proportions of Top 10 Videos") +
labs(x = "", y = "Proportion (%)", title = "Proportions of Top 10 Videos",fill="Metrics") +
theme_minimal()

}
Expand Down
14 changes: 14 additions & 0 deletions man/tabulate_overallsummary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/tabulate_summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/visualize_channel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.