Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to get the difference of range sliderInput in R Shiny

I’m working through an R Shiny tutorial, and decided to veer off the path, but I’m lost at the moment. A previous exercise had me create a range slider, and I checked the docs to fancy it up with arguments. Now what I’m trying to do is create a textOutput below it to show the value of the difference between the min and max.

library(shiny)

ui <- fluidPage(
  sliderInput("rang", "Range", value = c(100, 2000), min = 0, max = 15000, 
              sep = ",", pre = "$", ticks = FALSE, dragRange = TRUE,
              width = "600px"),
  textOutput(costDiff())
)

server <- function(input, output, session) {
  costDiff <- reactive({
    costDiff <- input$rang[2] - input$rang[1]
  }) 
  
  output$costDiff <- renderText("$",{
    costDiff()
  })
}

shinyApp(ui, server)

I’m receiving:

Error in costDiff() : could not find function "costDiff"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance for your help!

>Solution :

Try this:

library(shiny)

ui <- fluidPage(
    sliderInput("rang", "Range", value = c(100, 2000), min = 0, max = 15000, 
                sep = ",", pre = "$", ticks = FALSE, dragRange = TRUE,
                width = "600px"),
    textOutput('costDiff')
)

server <- function(input, output, session) {
    costDiff <- reactive({
        input$rang[2] - input$rang[1]
    }) 
    
    output$costDiff <- renderText({
        paste('$', costDiff())
    })
}

shinyApp(ui, server)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading