试图在 R Shiny 中获取范围 sliderInput 的差异

Trying to get the difference of range sliderInput in R Shiny

我正在处理一个 R Shiny tutorial,并决定改变方向,但我现在迷路了。之前的练习让我创建了一个范围滑块,我检查了文档以使用参数来想象它。现在我要做的是在其下方创建一个 textOutput 以显示最小值和最大值之间的差值。

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)

我收到:

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

在此先感谢您的帮助!

试试这个:

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)