-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
198 lines (171 loc) · 6.48 KB
/
app.R
File metadata and controls
198 lines (171 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
library(shiny)
library(ggplot2)
library(plotly)
# Define UI for application that draws a Load-velocity plot
ui <- fluidPage(
# Header
tags$header(
tags$h1("Load-velocity Model App"),
tags$p("This app allows you to visualize the Load-velocity model by adding speed and charge values.")
),
# Sidebar with input fields for speed and charge
sidebarLayout(
sidebarPanel(
numericInput("speed", "Enter Speed (m/s):", value = 0, min = 0),
numericInput("charge", "Enter Charge (kg):", value = 0, min = 0),
actionButton("add", "Add to Plot"),
actionButton("clear", "Clear Plot")
),
# Main panel with tabs
mainPanel(
tabsetPanel(
tabPanel(
"Summary",
plotlyOutput("combinedPlot")
),
tabPanel(
"Plots",
plotlyOutput("lvPlot"),
plotlyOutput("fsPlot"),
plotlyOutput("powerPlot")
),
tabPanel(
"Values",
h4("Maximal Values"),
verbatimTextOutput("maxForce"),
verbatimTextOutput("maxSpeed"),
verbatimTextOutput("maxPower")
)
)
)
),
# Footer
tags$footer(
tags$p(
"For more information, visit the ",
tags$a(href = "https://github.com/Boudry-Felix/Load-velocity", "GitHub repository"), " or report issues ",
tags$a(href = "https://github.com/Boudry-Felix/Load-velocity/issues", "here"), "."
)
)
)
# Define server logic required to draw the plot
server <- function(input, output) {
# Data storage
data <- reactiveVal(data.frame(speed = numeric(0), charge = numeric(0)))
observeEvent(input$add, {
new_data <- data()
new_data <- rbind(new_data, data.frame(speed = input$speed, charge = input$charge))
data(new_data)
})
observeEvent(input$clear, {
data(data.frame(speed = numeric(0), charge = numeric(0)))
})
output$lvPlot <- renderPlotly({
plot_data <- data()
p <- ggplot(plot_data, aes(x = speed, y = charge)) +
geom_point() +
labs(x = "Speed (m/s)", y = "Charge (kg)", title = "Load-velocity Model") +
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
if (nrow(plot_data) > 1) {
fit <- lm(charge ~ speed, data = plot_data)
p <- p + geom_smooth(method = "lm", col = "blue", se = FALSE, formula = "y ~ x")
ggplotly(p)
}
})
output$fsPlot <- renderPlotly({
plot_data <- data()
plot_data$force <- plot_data$charge * 9.81 # Assuming charge is mass in kg and force is in Newtons
p <- ggplot(plot_data, aes(x = speed, y = force)) +
geom_point() +
labs(x = "Speed (m/s)", y = "Force (N)", title = "Force-Speed Plot") +
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
if (nrow(plot_data) > 1) {
fit <- lm(force ~ speed, data = plot_data)
p <- p + geom_smooth(method = "lm", col = "blue", se = FALSE, formula = "y ~ x")
ggplotly(p)
}
})
output$powerPlot <- renderPlotly({
plot_data <- data()
plot_data$power <- plot_data$speed * plot_data$charge
p <- ggplot(plot_data, aes(x = speed, y = power)) +
geom_point() +
labs(x = "Speed (m/s)", y = "Power (W)", title = "Power Evolution by Speed") +
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
if (nrow(plot_data) > 2) {
fit <- lm(power ~ poly(speed, 2), data = plot_data)
p <- p + geom_smooth(method = "lm", formula = y ~ poly(x, 2), col = "red", se = FALSE)
ggplotly(p)
}
})
output$combinedPlot <- renderPlotly({
plot_data <- data()
if (nrow(plot_data) > 0) {
plot_data$force <- plot_data$charge * 9.81 # Assuming charge is mass in kg and force is in Newtons
plot_data$power <- plot_data$speed * plot_data$charge
# Combined plot
p <- ggplot(plot_data) +
geom_point(aes(x = speed, y = charge, color = "Charge")) +
geom_point(aes(x = speed, y = force, color = "Force")) +
geom_point(aes(x = speed, y = power, color = "Power")) +
labs(x = "Speed (m/s)", y = "Value", title = "Combined Plot") +
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
if (nrow(plot_data) > 1) {
fit1 <- lm(charge ~ speed, data = plot_data)
fit2 <- lm(force ~ speed, data = plot_data)
fit3 <- lm(power ~ poly(speed, 2), data = plot_data)
p <- p + geom_smooth(aes(x = speed, y = charge, color = "Charge"), method = "lm", se = FALSE, formula = "y ~ x") +
geom_smooth(aes(x = speed, y = force, color = "Force"), method = "lm", se = FALSE, formula = "y ~ x") +
geom_smooth(aes(x = speed, y = power, color = "Power"), method = "lm", formula = y ~ poly(x, 2), se = FALSE)
# Add regression equations to the legend
eq1 <- paste0("Charge: y = ", round(coef(fit1)[1], 4), " + ", round(coef(fit1)[2], 4), "x")
eq2 <- paste0("Force: y = ", round(coef(fit2)[1], 4), " + ", round(coef(fit2)[2], 4), "x")
eq3 <- paste0("Power: y = ", round(coef(fit3)[1], 4), " + ", round(coef(fit3)[2], 4), "x + ", round(coef(fit3)[3], 4), "x^2")
p <- p + scale_color_manual(
name = "Legend",
breaks = c("Charge", "Force", "Power"),
values = c("Charge" = "blue", "Force" = "green", "Power" = "red"),
labels = c(paste("Charge", eq1), paste("Force", eq2), paste("Power", eq3))
)
}
ggplotly(p)
} else {
NULL
}
})
output$maxForce <- renderText({
plot_data <- data()
if (nrow(plot_data) > 0) {
plot_data$force <- plot_data$charge * 9.81 # Ensure force column exists
fit <- lm(force ~ speed, data = plot_data)
max_force <- max(fitted(fit))
paste("Max Force: ", round(max_force, 4), " N")
} else {
"Max Force: N/A"
}
})
output$maxSpeed <- renderText({
plot_data <- data()
if (nrow(plot_data) > 0) {
plot_data$force <- plot_data$charge * 9.81 # Ensure force column exists
fit <- lm(speed ~ force, data = plot_data)
max_speed <- max(fitted(fit))
paste("Max Speed: ", round(max_speed, 4), " m/s")
} else {
"Max Speed: N/A"
}
})
output$maxPower <- renderText({
plot_data <- data()
if (nrow(plot_data) > 0) {
plot_data$power <- plot_data$speed * plot_data$charge # Ensure power column exists
fit <- lm(power ~ poly(speed, 2), data = plot_data)
max_power <- max(fitted(fit))
paste("Max Power: ", round(max_power, 4), " W")
} else {
"Max Power: N/A"
}
})
}
# Run the application
shinyApp(ui = ui, server = server)