Math function as input variable (update)

Hello,
Using the example in Chapter 11 Bookmarking | Mastering Shiny (mastering-shiny.org)

ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("omega", "omega", value = 1, min = -2, max = 2, step = 0.01),
sliderInput("delta", "delta", value = 1, min = 0, max = 2, step = 0.01),
sliderInput("damping", "damping", value = 1, min = 0.9, max = 1, step = 0.001),
numericInput("length", "length", value = 100)
),
mainPanel(
plotOutput("fig")
)
)
)
server <- function(input, output, session) {
t <- reactive(seq(0, input$length, length.out = input$length * 100))
x <- reactive(sin(input$omega * t() + input$delta) * input$damping ^ t())
y <- reactive(sin(t()) * input$damping ^ t())

output$fig <- renderPlot({
plot(x(), y(), axes = FALSE, xlab = "", ylab = "", type = "l", lwd = 2)
}, res = 96)
}
I want the user to choice the function(sin in the above code) himself as dynamical input without changing the code in the server function.
Thank you in advance

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      shiny::radioButtons("func_chooser",label="pick a function",
                          choices = c("sin","cos","sin cos product"),
                          selected = "sin"),
      sliderInput("omega", "omega", value = 1, min = -2, max = 2, step = 0.01),
      sliderInput("delta", "delta", value = 1, min = 0, max = 2, step = 0.01),
      sliderInput("damping", "damping", value = 1, min = 0.9, max = 1, step = 0.001),
      numericInput("length", "length", value = 100)
    ),
    mainPanel(
      plotOutput("fig")
    )
  )
)
server <- function(input, output, session) {
  
  dofunc <- function(x,funchoice){
  switch (funchoice,
          "sin" = sin(x),
          "cos" = cos(x),
          "sin cos product" = sin(x)*cos(x)
  )
  }
  t <- reactive(seq(0, input$length, length.out = input$length * 100))
  x <- reactive(dofunc(input$omega * t() + input$delta,input$func_chooser) * input$damping ^ t())
  y <- reactive(dofunc(t(),input$func_chooser) * input$damping ^ t())
  
  output$fig <- renderPlot({
    plot(x(), y(), axes = FALSE, xlab = "", ylab = "", type = "l", lwd = 2)
  }, res = 96)
}


shinyApp(ui, server)

Thank you and it works!!.
Is it possible that the user himself writes the function and enters it into an input field (eg. TextInput, ...) ?

Thanks in advance for your help !

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
     textInput("func_chooser","type a known function name (takes a single param (x)",value="sin"),
      sliderInput("omega", "omega", value = 1, min = -2, max = 2, step = 0.01),
      sliderInput("delta", "delta", value = 1, min = 0, max = 2, step = 0.01),
      sliderInput("damping", "damping", value = 1, min = 0.9, max = 1, step = 0.001),
      numericInput("length", "length", value = 100)
    ),
    mainPanel(
      plotOutput("fig")
    )
  )
)
server <- function(input, output, session) {
  
  dofunc <- function(x,funchoice){
    command <- paste0(funchoice,"(x)")
    print(command)
     res <- eval(parse(text=command))
     res
  }
  t <- reactive(seq(0, input$length, length.out = input$length * 100))
  x <- reactive(dofunc(input$omega * t() + input$delta,input$func_chooser) * input$damping ^ t())
  y <- reactive(dofunc(t(),input$func_chooser) * input$damping ^ t())
  
  output$fig <- renderPlot({
    plot(x(), y(), axes = FALSE, xlab = "", ylab = "", type = "l", lwd = 2)
  }, res = 96)
}


shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.