library(viridis) # Import the viridis color palette library
library(ggplot2)
set.seed(123) # Set a seed for reproducibility
<- 50000
num_flips <- sample(c("Heads", "Tails"), num_flips, replace = TRUE)
flips
# Image aspect ratio
<- 1 # You can customize the aspect ratio here
aspect_ratio <- round(sqrt(num_flips) * aspect_ratio)
n_col <- ceiling(num_flips / n_col)
n_row
# Create a color matrix to represent coin flips
<- ifelse(flips == "Heads", "red", "blue")
colors
# Create matrices for Heads and Tails
<- matrix(0, nrow = n_row, ncol = n_col)
heads_matrix <- matrix(0, nrow = n_row, ncol = n_col)
tails_matrix
for (i in 1:num_flips) {
if (flips[i] == "Heads") {
- 1) %/% n_col + 1, (i - 1) %% n_col + 1] <- 1
heads_matrix[(i else {
} - 1) %/% n_col + 1, (i - 1) %% n_col + 1] <- 1
tails_matrix[(i
}
}
# Function to calculate the number of consecutive sequences
<- function(matrix) {
calculate_sequences <- matrix(0, nrow = nrow(matrix), ncol = ncol(matrix))
sequences for (i in 1:nrow(matrix)) {
<- 0
count for (j in 1:ncol(matrix)) {
if (matrix[i, j] == 1) {
<- count + 1
count <- count
sequences[i, j] else {
} <- 0
count
}
}
}return(sequences)
}
# Calculate sequences for Heads and Tails matrices
<- calculate_sequences(heads_matrix)
sequences_heads <- calculate_sequences(tails_matrix)
sequences_tails
# Find the longest sequence for Heads and Tails
<- max(sequences_heads)
longest_sequence_heads <- max(sequences_tails)
longest_sequence_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
<- calculate_sequences(heads_matrix)
sequences_heads <- calculate_sequences(tails_matrix)
sequences_tails
# Calculate sequence lengths for Heads and Tails
<- table(sequences_heads)
sequence_lengths_heads <- table(sequences_tails)
sequence_lengths_tails
# Calculate the percentage of sequence lengths
<- prop.table(sequence_lengths_heads) * 100
percentages_heads <- prop.table(sequence_lengths_tails) * 100
percentages_tails
# Create data frames with lengths, absolute numbers, and percentages
<- data.frame(
dataframe_heads Length = names(sequence_lengths_heads),
Absolute_Numbers = as.numeric(sequence_lengths_heads),
Percentage = percentages_heads
)<- data.frame(
dataframe_tails 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)
<- function(id) {
my_module <- NS(id)
ns tagList(
textInput(inputId = ns("text_input"), label = "Enter text:"),
actionButton(inputId = ns("action_button"), label = "Click me")
)
}
<- fluidPage(
ui 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.