♻️ refactor app.Rmd
This commit is contained in:
248
src/app.Rmd
248
src/app.Rmd
@@ -12,6 +12,7 @@ library(httr)
|
||||
library(jsonlite)
|
||||
library(trajr)
|
||||
|
||||
|
||||
# UI Definition
|
||||
ui <- fluidPage(
|
||||
titlePanel("Flight Trajectory Analysis - GUI"),
|
||||
@@ -214,20 +215,7 @@ server <- function(input, output, session) {
|
||||
rv$current_route <- route_df
|
||||
|
||||
# Create trajectory object
|
||||
lat_ref <- route_df$lat[1]
|
||||
lon_ref <- route_df$lon[1]
|
||||
meters_per_deg_lat <- 111320
|
||||
meters_per_deg_lon <- 111320 * cos(lat_ref * pi / 180)
|
||||
|
||||
x_meters <- (route_df$lon - lon_ref) * meters_per_deg_lon
|
||||
y_meters <- (route_df$lat - lat_ref) * meters_per_deg_lat
|
||||
time_seconds <- route_df$time - route_df$time[1]
|
||||
|
||||
rv$current_trj <- TrajFromCoords(
|
||||
data.frame(x = x_meters, y = y_meters, time = time_seconds),
|
||||
xCol = "x", yCol = "y", timeCol = "time"
|
||||
)
|
||||
|
||||
rv$current_trj <- createTrajFromRoute(route_df)
|
||||
status(paste("Successfully analyzed", icao24, "with", nrow(route_df), "points"))
|
||||
|
||||
# Switch to analysis tab
|
||||
@@ -266,41 +254,8 @@ server <- function(input, output, session) {
|
||||
|
||||
trj <- rv$current_trj
|
||||
|
||||
duration <- TrajDuration(trj)
|
||||
path_length <- TrajLength(trj)
|
||||
diffusion_distance <- TrajDistance(trj)
|
||||
straightness <- TrajStraightness(trj)
|
||||
mean_velocity <- path_length / duration
|
||||
|
||||
fractal_dim <- tryCatch({
|
||||
min_step <- path_length / 100
|
||||
max_step <- path_length / 2
|
||||
if (min_step > 0 && max_step > min_step) {
|
||||
step_sizes <- exp(seq(log(min_step), log(max_step), length.out = 10))
|
||||
TrajFractalDimension(trj, stepSizes = step_sizes)
|
||||
} else {
|
||||
NA
|
||||
}
|
||||
}, error = function(e) NA)
|
||||
|
||||
data.frame(
|
||||
Parameter = c(
|
||||
"Duration (s)", "Duration (min)",
|
||||
"Path Length (m)", "Path Length (km)",
|
||||
"Diffusion Distance (m)", "Diffusion Distance (km)",
|
||||
"Straightness Index",
|
||||
"Mean Velocity (m/s)", "Mean Velocity (km/h)",
|
||||
"Fractal Dimension"
|
||||
),
|
||||
Value = c(
|
||||
round(duration, 2), round(duration / 60, 2),
|
||||
round(path_length, 2), round(path_length / 1000, 2),
|
||||
round(diffusion_distance, 2), round(diffusion_distance / 1000, 2),
|
||||
round(straightness, 4),
|
||||
round(mean_velocity, 2), round(mean_velocity * 3.6, 2),
|
||||
round(fractal_dim, 4)
|
||||
)
|
||||
)
|
||||
data.frame <- calculateRouteCharacteristics(trj)
|
||||
data.frame
|
||||
})
|
||||
|
||||
# Batch analysis
|
||||
@@ -337,19 +292,7 @@ server <- function(input, output, session) {
|
||||
route_df <- as.data.frame(track_data$path)
|
||||
colnames(route_df) <- c("time", "lat", "lon", "alt", "heading", "on_ground")
|
||||
|
||||
lat_ref <- route_df$lat[1]
|
||||
lon_ref <- route_df$lon[1]
|
||||
meters_per_deg_lat <- 111320
|
||||
meters_per_deg_lon <- 111320 * cos(lat_ref * pi / 180)
|
||||
|
||||
x_meters <- (route_df$lon - lon_ref) * meters_per_deg_lon
|
||||
y_meters <- (route_df$lat - lat_ref) * meters_per_deg_lat
|
||||
time_seconds <- route_df$time - route_df$time[1]
|
||||
|
||||
trj <- TrajFromCoords(
|
||||
data.frame(x = x_meters, y = y_meters, time = time_seconds),
|
||||
xCol = "x", yCol = "y", timeCol = "time"
|
||||
)
|
||||
trj <- createTrajFromRoute(route_df)
|
||||
|
||||
duration <- TrajDuration(trj)
|
||||
path_length <- TrajLength(trj)
|
||||
@@ -402,19 +345,55 @@ server <- function(input, output, session) {
|
||||
# Statistics summary table
|
||||
output$stats_summary_table <- renderTable({
|
||||
req(rv$trajectory_stats_df)
|
||||
calculateStatsSummary(rv$trajectory_stats_df)
|
||||
})
|
||||
|
||||
params <- c("diffusion_distance_km", "straightness", "duration_min",
|
||||
"mean_velocity_kmh", "fractal_dimension")
|
||||
labels <- c("Diffusion Distance (km)", "Straightness", "Duration (min)",
|
||||
# Boxplots
|
||||
output$boxplots <- renderPlot({
|
||||
req(rv$trajectory_stats_df)
|
||||
createBoxplots(rv$trajectory_stats_df)
|
||||
})
|
||||
|
||||
# Density plots
|
||||
output$density_plots <- renderPlot({
|
||||
req(rv$trajectory_stats_df)
|
||||
createDensityPlots(rv$trajectory_stats_df)
|
||||
})
|
||||
|
||||
# Histograms
|
||||
output$histograms <- renderPlot({
|
||||
req(rv$trajectory_stats_df)
|
||||
createHistograms(rv$trajectory_stats_df)
|
||||
})
|
||||
|
||||
# Interpretation text
|
||||
output$interpretation_text <- renderText({
|
||||
req(rv$trajectory_stats_df)
|
||||
generateInterpretation(rv$trajectory_stats_df)
|
||||
})
|
||||
}
|
||||
|
||||
# Helper function to get parameter names and labels
|
||||
getTrajectoryParams <- function() {
|
||||
list(
|
||||
params = c("diffusion_distance_km", "straightness", "duration_min",
|
||||
"mean_velocity_kmh", "fractal_dimension"),
|
||||
labels = c("Diffusion Distance (km)", "Straightness", "Duration (min)",
|
||||
"Mean Velocity (km/h)", "Fractal Dimension")
|
||||
)
|
||||
}
|
||||
|
||||
stats_list <- lapply(seq_along(params), function(i) {
|
||||
x <- rv$trajectory_stats_df[[params[i]]]
|
||||
# Calculate statistics summary table
|
||||
calculateStatsSummary <- function(trajectory_stats_df) {
|
||||
p <- getTrajectoryParams()
|
||||
|
||||
stats_list <- lapply(seq_along(p$params), function(i) {
|
||||
x <- trajectory_stats_df[[p$params[i]]]
|
||||
x <- x[!is.na(x)]
|
||||
if (length(x) < 2) return(NULL)
|
||||
|
||||
data.frame(
|
||||
Parameter = labels[i],
|
||||
Parameter = p$labels[i],
|
||||
N = length(x),
|
||||
Mean = round(mean(x), 4),
|
||||
Variance = round(var(x), 4),
|
||||
@@ -426,77 +405,60 @@ server <- function(input, output, session) {
|
||||
})
|
||||
|
||||
do.call(rbind, stats_list[!sapply(stats_list, is.null)])
|
||||
})
|
||||
}
|
||||
|
||||
# Boxplots
|
||||
output$boxplots <- renderPlot({
|
||||
req(rv$trajectory_stats_df)
|
||||
|
||||
params <- c("diffusion_distance_km", "straightness", "duration_min",
|
||||
"mean_velocity_kmh", "fractal_dimension")
|
||||
labels <- c("Diffusion Distance (km)", "Straightness", "Duration (min)",
|
||||
"Mean Velocity (km/h)", "Fractal Dimension")
|
||||
# Create boxplots for trajectory statistics
|
||||
createBoxplots <- function(trajectory_stats_df) {
|
||||
p <- getTrajectoryParams()
|
||||
|
||||
par(mfrow = c(2, 3))
|
||||
for (i in seq_along(params)) {
|
||||
data <- rv$trajectory_stats_df[[params[i]]][!is.na(rv$trajectory_stats_df[[params[i]]])]
|
||||
for (i in seq_along(p$params)) {
|
||||
data <- trajectory_stats_df[[p$params[i]]][!is.na(trajectory_stats_df[[p$params[i]]])]
|
||||
if (length(data) >= 2) {
|
||||
boxplot(data, main = labels[i], ylab = labels[i], col = "lightblue", border = "darkblue")
|
||||
boxplot(data, main = p$labels[i], ylab = p$labels[i], col = "lightblue", border = "darkblue")
|
||||
points(1, mean(data), pch = 18, col = "red", cex = 1.5)
|
||||
}
|
||||
}
|
||||
par(mfrow = c(1, 1))
|
||||
})
|
||||
}
|
||||
|
||||
# Density plots
|
||||
output$density_plots <- renderPlot({
|
||||
req(rv$trajectory_stats_df)
|
||||
|
||||
params <- c("diffusion_distance_km", "straightness", "duration_min",
|
||||
"mean_velocity_kmh", "fractal_dimension")
|
||||
labels <- c("Diffusion Distance (km)", "Straightness", "Duration (min)",
|
||||
"Mean Velocity (km/h)", "Fractal Dimension")
|
||||
# Create density plots for trajectory statistics
|
||||
createDensityPlots <- function(trajectory_stats_df) {
|
||||
p <- getTrajectoryParams()
|
||||
|
||||
par(mfrow = c(2, 3))
|
||||
for (i in seq_along(params)) {
|
||||
data <- rv$trajectory_stats_df[[params[i]]][!is.na(rv$trajectory_stats_df[[params[i]]])]
|
||||
for (i in seq_along(p$params)) {
|
||||
data <- trajectory_stats_df[[p$params[i]]][!is.na(trajectory_stats_df[[p$params[i]]])]
|
||||
if (length(data) >= 3) {
|
||||
dens <- density(data)
|
||||
plot(dens, main = paste("Density:", labels[i]), xlab = labels[i], col = "darkblue", lwd = 2)
|
||||
plot(dens, main = paste("Density:", p$labels[i]), xlab = p$labels[i], col = "darkblue", lwd = 2)
|
||||
polygon(dens, col = rgb(0, 0, 1, 0.3), border = "darkblue")
|
||||
abline(v = mean(data), col = "red", lwd = 2, lty = 2)
|
||||
abline(v = median(data), col = "green", lwd = 2, lty = 3)
|
||||
}
|
||||
}
|
||||
par(mfrow = c(1, 1))
|
||||
})
|
||||
}
|
||||
|
||||
# Histograms
|
||||
output$histograms <- renderPlot({
|
||||
req(rv$trajectory_stats_df)
|
||||
|
||||
params <- c("diffusion_distance_km", "straightness", "duration_min",
|
||||
"mean_velocity_kmh", "fractal_dimension")
|
||||
labels <- c("Diffusion Distance (km)", "Straightness", "Duration (min)",
|
||||
"Mean Velocity (km/h)", "Fractal Dimension")
|
||||
# Create histograms for trajectory statistics
|
||||
createHistograms <- function(trajectory_stats_df) {
|
||||
p <- getTrajectoryParams()
|
||||
|
||||
par(mfrow = c(2, 3))
|
||||
for (i in seq_along(params)) {
|
||||
data <- rv$trajectory_stats_df[[params[i]]][!is.na(rv$trajectory_stats_df[[params[i]]])]
|
||||
for (i in seq_along(p$params)) {
|
||||
data <- trajectory_stats_df[[p$params[i]]][!is.na(trajectory_stats_df[[p$params[i]]])]
|
||||
if (length(data) >= 3) {
|
||||
hist(data, probability = TRUE, main = paste("Histogram:", labels[i]),
|
||||
xlab = labels[i], col = "lightgray", border = "darkgray")
|
||||
hist(data, probability = TRUE, main = paste("Histogram:", p$labels[i]),
|
||||
xlab = p$labels[i], col = "lightgray", border = "darkgray")
|
||||
lines(density(data), col = "red", lwd = 2)
|
||||
}
|
||||
}
|
||||
par(mfrow = c(1, 1))
|
||||
})
|
||||
}
|
||||
|
||||
# Interpretation text
|
||||
output$interpretation_text <- renderText({
|
||||
req(rv$trajectory_stats_df)
|
||||
|
||||
df <- rv$trajectory_stats_df
|
||||
# Generate interpretation text for trajectory statistics
|
||||
generateInterpretation <- function(trajectory_stats_df) {
|
||||
df <- trajectory_stats_df
|
||||
|
||||
text <- "========== INTERPRETATION OF TRAJECTORY PARAMETERS ==========\n\n"
|
||||
|
||||
@@ -549,9 +511,73 @@ server <- function(input, output, session) {
|
||||
text <- paste0(text, "========== END OF ANALYSIS ==========")
|
||||
|
||||
text
|
||||
}
|
||||
|
||||
createTrajFromRoute <- function(route_df) {
|
||||
tryCatch({
|
||||
lat_ref <- route_df$lat[1]
|
||||
lon_ref <- route_df$lon[1]
|
||||
meters_per_deg_lat <- 111320
|
||||
meters_per_deg_lon <- 111320 * cos(lat_ref * pi / 180)
|
||||
|
||||
x_meters <- (route_df$lon - lon_ref) * meters_per_deg_lon
|
||||
y_meters <- (route_df$lat - lat_ref) * meters_per_deg_lat
|
||||
time_seconds <- route_df$time - route_df$time[1]
|
||||
|
||||
trj <- TrajFromCoords(
|
||||
data.frame(x = x_meters, y = y_meters, time = time_seconds),
|
||||
xCol = "x", yCol = "y", timeCol = "time"
|
||||
)
|
||||
|
||||
return(trj)
|
||||
|
||||
}, error = function(e) {
|
||||
status(paste("Error creating trajectory object:", e$message))
|
||||
})
|
||||
}
|
||||
|
||||
calculateRouteCharacteristics <- function(trj) {
|
||||
duration <- TrajDuration(trj)
|
||||
path_length <- TrajLength(trj)
|
||||
diffusion_distance <- TrajDistance(trj)
|
||||
straightness <- TrajStraightness(trj)
|
||||
mean_velocity <- path_length / duration
|
||||
|
||||
fractal_dim <- tryCatch({
|
||||
min_step <- path_length / 100
|
||||
max_step <- path_length / 2
|
||||
if (min_step > 0 && max_step > min_step) {
|
||||
step_sizes <- exp(seq(log(min_step), log(max_step), length.out = 10))
|
||||
TrajFractalDimension(trj, stepSizes = step_sizes)
|
||||
} else {
|
||||
NA
|
||||
}
|
||||
}, error = function(e) NA)
|
||||
|
||||
return (data.frame(
|
||||
Parameter = c(
|
||||
"Duration (s)", "Duration (min)",
|
||||
"Path Length (km)",
|
||||
"Duffusion Distance (m)",
|
||||
"Diffusion Distance (km)",
|
||||
"Straightness Index",
|
||||
"Mean Velocity (km/h)",
|
||||
"Fractal Dimension"
|
||||
),
|
||||
Value = c(
|
||||
duration_s = round(duration, 2),
|
||||
duration_min = round(duration / 60, 2),
|
||||
path_length_km = round(path_length / 1000, 2),
|
||||
diffusion_distance_m = round(diffusion_distance, 2),
|
||||
diffusion_distance_km = round(diffusion_distance / 1000, 2),
|
||||
straightness_index = round(straightness, 4),
|
||||
mean_velocity_kmh = round(mean_velocity *3.6, 2),
|
||||
fractal_dimension = round(fractal_dim, 4)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
# Run the application
|
||||
shinyApp(ui = ui, server = server)
|
||||
```
|
||||
Reference in New Issue
Block a user