Reputation: 2012
Edited to reflect ongoing discussion.
From my server section I want to use a different formula, depending on a radioButton selection that's called input$beamSupport
and furthermore change the formula, depending on the value of x
. I'm stuck on the syntax, however.
I also know that I could trim some of the code by simply stating "else...", but I want to have the possibility to expand.
Right now I get the error message "Error in if: argument is of length zero"
library(shiny)
ui <- fluidPage(
# Style tags for changing the slider
tags$style(HTML(".irs-bar {background: none}")),
tags$style(HTML(".irs-bar {border-top: none}")),
tags$style(HTML(".irs-bar {border-bottom: none}")),
tags$style(HTML(".irs-bar-edge {border: none}")),
tags$style(HTML(".irs-bar-edge {background: none}")),
mainPanel(
titlePanel("Beam Deflection Calculator"),
radioButtons("beamSupport",
label = ("Beam support type"),
choices = list("Simply supported" = 1,
"Cantilever" = 2),
selected = 1),
numericInput("num_W", # Load force, W
label = "Enter load force in N.",
value = 1),
numericInput("num_l", # Length of beam, l
label = "Enter beam length in m.",
value = 10),
numericInput("num_I", # Intertial moment, I (caps i)
label = "Enter moment of inertia in m^4.",
value = 0.001),
numericInput("num_E",
label = "Enter Young's modulus in GPa.",
value = 200),
uiOutput("slider"), # Sliders for a and x
textOutput("text_calc")
)
)
server <- function(input, output, session) {
output$slider <- renderUI({
tagList( # Need this for multiple reactive sliders
sliderInput("slider_a",
label = "Choose position, where to apply force, starting from left, in m.",
min = 0,
max = input$num_l,
value = 5,
step = 0.1),
sliderInput("slider_x",
label = "Calculate the deflection, at position starting from left, in m.",
min = 0,
max = input$num_l,
value = 5,
step = 0.1)
)
})
output$text_calc <- renderText({
W <- input$num_W
l <- input$num_l
I <- input$num_I
E <- input$num_E * 10^9
a <- input$slider_a
x <- input$slider_x
cond <- x < a
if (input$beamSupport == 1){
if (cond){
return(paste("The deflection is =",
((W*(l-a)*x)/(6*E*I*l))*(l**2-x**2-(l-a)**2)
))
}
else{
return(paste("The deflection is =",
((W*a*(l-x))/(6*E*I*l))*(l**2-(l-x)**2-a**2)
))
}
}
if (input$beamSupport == 2){
if (cond){
return(paste("The deflection is =",
((W*x**2)/(6*E*I))*(3*a-x)
))
}
else{
return(paste("The deflection is =",
((W*a**2)/(6*E*I))*(3*x-a)
))
}
}
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1
Views: 1130
Reputation: 29387
Try this, also note that when you use renderUI
that will be will get evaluated after the initial parameters are loaded so your slider_a
and slider_x
will be null until then.
#rm(list = ls())
library(shiny)
ui <- fluidPage(
# Style tags for changing the slider
tags$style(HTML(".irs-bar {background: none}")),
tags$style(HTML(".irs-bar {border-top: none}")),
tags$style(HTML(".irs-bar {border-bottom: none}")),
tags$style(HTML(".irs-bar-edge {border: none}")),
tags$style(HTML(".irs-bar-edge {background: none}")),
mainPanel(
titlePanel("Beam Deflection Calculator"),
radioButtons("beamSupport",
label = ("Beam support type"),
choices = list("Simply supported" = 1,
"Cantilever" = 2),
selected = 1),
numericInput("num_W", # Load force, W
label = "Enter load force in N.",
value = 1),
numericInput("num_l", # Length of beam, l
label = "Enter beam length in m.",
value = 10),
numericInput("num_I", # Intertial moment, I (caps i)
label = "Enter moment of inertia in m^4.",
value = 0.001),
numericInput("num_E",
label = "Enter Young's modulus in GPa.",
value = 200),
uiOutput("slider"), # Sliders for a and x
textOutput("text_calc")
)
)
server <- function(input, output, session) {
output$slider <- renderUI({
tagList( # Need this for multiple reactive sliders
sliderInput("slider_a",
label = "Choose position, where to apply force, starting from left, in m.",
min = 0,
max = input$num_l,
value = 5,
step = 0.1),
sliderInput("slider_x",
label = "Calculate the deflection, at position starting from left, in m.",
min = 0,
max = input$num_l,
value = 5,
step = 0.1)
)
})
output$text_calc <- renderText({
if(is.null(input$slider_a) || is.null(input$slider_x)){
return()
}
W <- input$num_W
l <- input$num_l
I <- input$num_I
E <- input$num_E * 10^9
a <- input$slider_a
x <- input$slider_x
cond <- x < a
if (input$beamSupport == 1){
if (cond){
return(paste("The deflection is =",
((W*(l-a)*x)/(6*E*I*l))*(l**2-x**2-(l-a)**2)
))
}
else{
return(paste("The deflection is =",
((W*a*(l-x))/(6*E*I*l))*(l**2-(l-x)**2-a**2)
))
}
}
if (input$beamSupport == 2){
if (cond){
return(paste("The deflection is =",
((W*x**2)/(6*E*I))*(3*a-x)
))
}
else{
return(paste("The deflection is =",
((W*a**2)/(6*E*I))*(3*x-a)
))
}
}
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1