Reem
2024-12-09
##Introduction In this exercise, we will explore the shiny package, create a basic Shiny app, and extend its functionality. Outputs are explicitly included to ensure visibility during each step.
##Step 1: Package Discovery and Installation ###Installing and Loading the Package
# Install shiny (if not already installed)
if (!requireNamespace("shiny", quietly = TRUE)) {
install.packages("shiny")
}
library(shiny)
# Confirm package loaded
print("Shiny package loaded successfully.")
## [1] "Shiny package loaded successfully."
##Step 2: Analyzing the Package Structure ###Listing Functions and Tools
# List all exported functions
functions <- ls("package:shiny")
print(head(functions, 10)) # Display first 10 functions for clarity
## [1] "a" "absolutePanel" "actionButton" "actionLink"
## [5] "addResourcePath" "animationOptions" "appendTab" "as.shiny.appobj"
## [9] "basicPage" "bindCache"
## No vignettes found by browseVignettes("shiny")
## [1] "Browse the vignettes opened in your browser or RStudio Viewer."
##Step 3: Solving a Practical Problem ###Building a Basic Shiny App Here, we’ll build an app step by step with outputs displayed.
ui <- fluidPage(
titlePanel("MTCars Data Explorer"),
sidebarLayout(
sidebarPanel(
sliderInput("mpgRange", "Select MPG Range:",
min = min(mtcars$mpg), max = max(mtcars$mpg),
value = c(min(mtcars$mpg), max(mtcars$mpg))),
selectInput("cyl", "Select Cylinder:",
choices = unique(mtcars$cyl), selected = unique(mtcars$cyl)[1])
),
mainPanel(
plotOutput("mpgPlot")
)
)
)
print("UI defined successfully.")
## [1] "UI defined successfully."
server <- function(input, output) {
output$mpgPlot <- renderPlot({
filtered_data <- subset(mtcars, mpg >= input$mpgRange[1] & mpg <= input$mpgRange[2] & cyl == input$cyl)
plot(filtered_data$wt, filtered_data$mpg,
main = "Weight vs. MPG",
xlab = "Weight (1000 lbs)",
ylab = "Miles per Gallon",
pch = 19, col = "blue")
})
}
print("Server logic defined successfully.")
## [1] "Server logic defined successfully."
## [1] "Launching the Shiny app. Check your RStudio Viewer or browser."
##Step 4: Extending the Package ###Adding a Custom Utility Function We’ll add a function to standardize the appearance of plots and test it.
# Custom function to add titles and styling
customize_plot <- function(plot_obj, title, subtitle) {
plot_obj +
ggtitle(title, subtitle) +
theme(
plot.title = element_text(face = "bold", hjust = 0.5, size = 14),
plot.subtitle = element_text(face = "italic", hjust = 0.5, size = 12)
)
}
print("Custom function defined successfully.")
## [1] "Custom function defined successfully."
###Testing the Function
## Warning: package 'ggplot2' was built under R version 4.3.3
sample_plot <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
customized_plot <- customize_plot(sample_plot, "Weight vs. MPG", "Using a Custom Function")
print(customized_plot) # Display customized plot
##Conclusion In this exercise, we explored the shiny package step by step, ensuring visible outputs along the way. We:
1-Discovered and loaded the shiny package while reviewing its documentation and functions. 2-Built a simple interactive Shiny app for exploring the mtcars dataset. 3-Extended functionality by creating a custom utility function for plot customization and tested it using a ggplot visualization.