Skip to content

Commit

Permalink
fixed updateNumericRangeInput, added example
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Nov 16, 2021
1 parent 7550090 commit 31e2755
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 15 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ shinyWidgets 0.6.2.9000
======================

* `checkboxGroupButtons()`/`radioGroupButtons()` fixed `justified = TRUE` argument compatibility with Bootstrap 4 (fix [#423](https://github.com/dreamRs/shinyWidgets/issues/423))
* `checkboxGroupButtons()`/`radioGroupButtons()`: `status` argument now accept a vector, thanks to [@jassler](https://github.com/jassler) ([#440](https://github.com/dreamRs/shinyWidgets/pull/440))

### Bug fixes
* `updateNumericRangeInput()` failed to correctly update `label` ([#441](https://github.com/dreamRs/shinyWidgets/issues/441))



Expand Down
22 changes: 13 additions & 9 deletions R/input-numericRange.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#' @importFrom shiny sliderInput restoreInput
#' @importFrom utils packageVersion
#'
#' @seealso [updateNumericRangeInput()]
#'
#' @export
#'
#' @examples
Expand All @@ -37,7 +39,7 @@
#' tags$br(),
#'
#' numericRangeInput(
#' inputId = "noui1", label = "Numeric Range Input:",
#' inputId = "my_id", label = "Numeric Range Input:",
#' value = c(100, 400)
#' ),
#' verbatimTextOutput(outputId = "res1")
Expand All @@ -46,7 +48,7 @@
#'
#' server <- function(input, output, session) {
#'
#' output$res1 <- renderPrint(input$noui1)
#' output$res1 <- renderPrint(input$my_id)
#'
#' }
#'
Expand Down Expand Up @@ -117,20 +119,22 @@ numericRangeInput <- function(inputId,
#'
#' @param session The session object passed to function given to shinyServer.
#' @inheritParams numericRangeInput
#'
#' @seealso [numericRangeInput()]
#'
#' @export
#'
#' @example examples/updateNumericRangeInput.R
updateNumericRangeInput <- function(session = getDefaultReactiveDomain(),
inputId,
label = NULL,
value = NULL) {

value <- c(min(value), max(value))

message <- dropNulls(list(
if (!is.null(value))
value <- c(min(value), max(value))
message <- list(
label = label,
value = value
))

session$sendInputMessage(inputId, message)

)
session$sendInputMessage(inputId, dropNulls(message))
}
42 changes: 42 additions & 0 deletions examples/updateNumericRangeInput.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
library(shiny)
library(shinyWidgets)

ui <- fluidPage(

tags$br(),

numericRangeInput(
inputId = "my_id",
label = "Numeric Range Input:",
value = c(100, 400)
),
verbatimTextOutput(outputId = "res1"),
textInput("label", "Update label:"),
numericInput("val1", "Update value 1:", 100),
numericInput("val2", "Update value 2:", 400)

)

server <- function(input, output, session) {

output$res1 <- renderPrint(input$my_id)

observeEvent(input$label, {
updateNumericRangeInput(
session = session,
inputId = "my_id",
label = input$label
)
}, ignoreInit = TRUE)

observe({
updateNumericRangeInput(
session = session,
inputId = "my_id",
value = c(input$val1, input$val2)
)
})
}

if (interactive())
shinyApp(ui, server)
9 changes: 6 additions & 3 deletions inst/assets/numericRange/js/numericRange-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ $.extend(numericRangeInputBinding, {
receiveMessage: function(el, data) {
var $el = $(el);

//if (data.hasOwnProperty('label')) $el.find('label[for="' + $escape(el.id) + '"]').text(data.label);

if (data.hasOwnProperty("value")) this.setValue($el, data.value);
if (data.hasOwnProperty("label")) {
$el.find('label[for="' + Shiny.$escape(el.id) + '"]').text(data.label);
}

if (data.hasOwnProperty("value")) {
this.setValue($el, data.value);
}
$(el).trigger("change");
},
unsubscribe: function(el) {
Expand Down
2 changes: 1 addition & 1 deletion inst/assets/shinyWidgets-bindings.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions man/numericRangeInput.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions man/updateNumericRangeInput.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 31e2755

Please sign in to comment.