library(viridis) # Import the viridis color palette library
library(ggplot2)
set.seed(123) # Set a seed for reproducibility
num_flips <- 50000
flips <- sample(c("Heads", "Tails"), num_flips, replace = TRUE)
# Image aspect ratio
aspect_ratio <- 1 # You can customize the aspect ratio here
n_col <- round(sqrt(num_flips) * aspect_ratio)
n_row <- ceiling(num_flips / n_col)
# Create a color matrix to represent coin flips
colors <- ifelse(flips == "Heads", "red", "blue")
# Create matrices for Heads and Tails
heads_matrix <- matrix(0, nrow = n_row, ncol = n_col)
tails_matrix <- matrix(0, nrow = n_row, ncol = n_col)
for (i in 1:num_flips) {
if (flips[i] == "Heads") {
heads_matrix[(i - 1) %/% n_col + 1, (i - 1) %% n_col + 1] <- 1
} else {
tails_matrix[(i - 1) %/% n_col + 1, (i - 1) %% n_col + 1] <- 1
}
}
# Function to calculate the number of consecutive sequences
calculate_sequences <- function(matrix) {
sequences <- matrix(0, nrow = nrow(matrix), ncol = ncol(matrix))
for (i in 1:nrow(matrix)) {
count <- 0
for (j in 1:ncol(matrix)) {
if (matrix[i, j] == 1) {
count <- count + 1
sequences[i, j] <- count
} else {
count <- 0
}
}
}
return(sequences)
}
# Calculate sequences for Heads and Tails matrices
sequences_heads <- calculate_sequences(heads_matrix)
sequences_tails <- calculate_sequences(tails_matrix)
# Find the longest sequence for Heads and Tails
longest_sequence_heads <- max(sequences_heads)
longest_sequence_tails <- max(sequences_tails)
# Create images with sequences and titles
par(mfrow = c(1, 2)) # Display the two images side by side
image(t(sequences_heads), col = viridis(100), main = paste("Heads Sequences (Max:", longest_sequence_heads, ")"), xaxt = "n", yaxt = "n")
image(t(sequences_tails), col = inferno(100), main = paste("Tails Sequences (Max:", longest_sequence_tails, ")"), xaxt = "n", yaxt = "n")
library(knitr)
# Calculate sequences for Heads and Tails matrices
sequences_heads <- calculate_sequences(heads_matrix)
sequences_tails <- calculate_sequences(tails_matrix)
# Calculate sequence lengths for Heads and Tails
sequence_lengths_heads <- table(sequences_heads)
sequence_lengths_tails <- table(sequences_tails)
# Calculate the percentage of sequence lengths
percentages_heads <- prop.table(sequence_lengths_heads) * 100
percentages_tails <- prop.table(sequence_lengths_tails) * 100
# Create data frames with lengths, absolute numbers, and percentages
dataframe_heads <- data.frame(
Length = names(sequence_lengths_heads),
Absolute_Numbers = as.numeric(sequence_lengths_heads),
Percentage = percentages_heads
)
dataframe_tails <- data.frame(
Length = names(sequence_lengths_tails),
Absolute_Numbers = as.numeric(sequence_lengths_tails),
Percentage = percentages_tails
)
# Create formatted tables
kable(dataframe_heads, caption = "Table of Heads Sequence Lengths")
kable(dataframe_tails, caption = "Table of Tails Sequence Lengths")In this example, both module_1 and module_2 share the same IDs for input elements (text_input and action_button). If we interact with one module, it will affect the other module as well, leading to unexpected behavior.
Using Namespaces (Recommended)
Now, let’s use namespaces to create unique IDs for each module instance:
library(shiny)
my_module <- function(id) {
ns <- NS(id)
tagList(
textInput(inputId = ns("text_input"), label = "Enter text:"),
actionButton(inputId = ns("action_button"), label = "Click me")
)
}
ui <- fluidPage(
my_module("module_1"),
my_module("module_2")
)In this example, we use NS to generate unique namespaces for each module instance (module_1 and module_2). As a result, the input element IDs are unique between instances, ensuring that interactions within one module do not affect the other module.
Conclusion
When creating Shiny modules, it’s highly recommended to use namespaces (NS) to prevent ID conflicts between module instances. This practice ensures that each module operates independently and avoids unexpected behavior when working with multiple modules in your Shiny app.