Skip to content

Commit

Permalink
sliderTextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Oct 11, 2017
1 parent d9d3126 commit f524a7a
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shinyWidgets
Title: Custom Inputs Widgets for Shiny
Version: 0.3.5
Version: 0.3.5.900
Authors@R: c(
person("Victor", "Perrier", email = "victor.perrier@dreamrs.fr", role = c("aut", "cre")),
person("Fanny", "Meyer", email = "fanny.meyer@dreamrs.fr", role = c("aut")),
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export(radioGroupButtons)
export(searchInput)
export(sendSweetAlert)
export(shinyWidgetsGallery)
export(sliderTextInput)
export(switchInput)
export(textInputAddon)
export(tooltipOptions)
Expand All @@ -33,6 +34,7 @@ export(updateMaterialSwitch)
export(updatePickerInput)
export(updateProgressBar)
export(updateRadioGroupButtons)
export(updateSliderTextInput)
export(updateSwitchInput)
export(useSweetAlert)
importFrom(htmltools,HTML)
Expand All @@ -47,10 +49,12 @@ importFrom(htmltools,tags)
importFrom(htmltools,validateCssUnit)
importFrom(jsonlite,toJSON)
importFrom(shiny,addResourcePath)
importFrom(shiny,animationOptions)
importFrom(shiny,getDefaultReactiveDomain)
importFrom(shiny,icon)
importFrom(shiny,restoreInput)
importFrom(shiny,runApp)
importFrom(shiny,shinyAppFile)
importFrom(shiny,sliderInput)
importFrom(utils,capture.output)
importFrom(utils,packageVersion)
20 changes: 13 additions & 7 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
shinyWidgets 0.3.5.900
==================

* New widget : `sliderTextInput` : a slider for character vector.


shinyWidgets 0.3.5
==================

Support for bookmarking state.
Support for tooltip from `bsplus`.
Upgrade `pickerInput` to bootstrap-select 1.12.4.
Upgrade `switchInput` to bootstrap-switch 3.3.4.
Remove `receiveSweetAlert` for simpler use with `useSweetAlert`.
Add inline argument to `materialSwitch` ([#17](https://github.com/dreamRs/shinyWidgets/issues/17)).
Display code for dropdowns in gallery.
* Support for bookmarking state.
* Support for tooltip from `bsplus`.
* Upgrade `pickerInput` to bootstrap-select 1.12.4.
* Upgrade `switchInput` to bootstrap-switch 3.3.4.
* Remove `receiveSweetAlert` for simpler use with `useSweetAlert`.
* Add inline argument to `materialSwitch` ([#17](https://github.com/dreamRs/shinyWidgets/issues/17)).
* Display code for dropdowns in gallery.



Expand Down
226 changes: 226 additions & 0 deletions R/input-sliderText.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
#' @title Slider Text Input Widget
#'
#' @description Constructs a slider widget with characters instead of numeric values.
#'
#' @param inputId The \code{input} slot that will be used to access the value.
#' @param label Display label for the control, or \code{NULL} for no label.
#' @param choices Character vector to select a value from.
#' @param selected The initially selected value, if length > 1, create a range slider.
#' @param animate TRUE to show simple animation controls with default settings, for more details see \code{\link[shiny]{sliderInput}}.
#' @param grid Logical, show or hide ticks marks.
#' @param hide_min_max Hides min and max labels.
#' @param from_fixed Fix position of left (or single) handle.
#' @param to_fixed Fix position of right handle.
#' @param from_min Set minimum limit for left handle.
#' @param from_max Set the maximum limit for left handle.
#' @param to_min Set minimum limit for right handle.
#' @param to_max Set the maximum limit for right handle.
#' @param width The width of the input, e.g. \code{400px}, or \code{100\%}.
#' @param pre A prefix string to put in front of the value.
#' @param post A suffix string to put after the value.
#' @param dragRange See the same argument in \code{\link[shiny]{sliderInput}}.
#'
#' @return The value retrieved server-side is a character vector.
#' @export
#'
#' @importFrom htmltools validateCssUnit tagAppendChild findDependencies tags
#' @importFrom shiny icon sliderInput animationOptions
#' @importFrom jsonlite toJSON
#'
#' @examples
#' \dontrun{
#'
#' if (interactive()) {
#'
#' library("shiny")
#' library("shinyWidgets")
#'
#' ui <- fluidPage(
#' br(),
#' sliderTextInput(
#' inputId = "mySliderText",
#' label = "Month range slider:",
#' choices = month.name,
#' selected = month.name[c(4, 7)]
#' ),
#' verbatimTextOutput(outputId = "result")
#' )
#'
#' server <- function(input, output, session) {
#' output$result <- renderPrint(str(input$mySliderText))
#' }
#'
#' shinyApp(ui = ui, server = server)
#'
#' }
#'
#' }
sliderTextInput <- function (inputId, label, choices, selected = NULL,
animate = FALSE, grid = FALSE,
hide_min_max = FALSE,
from_fixed = FALSE, to_fixed = FALSE,
from_min = NULL, from_max = NULL,
to_min = NULL, to_max = NULL,
width = NULL, pre = NULL, post = NULL, dragRange = TRUE)
{

if (!is.character(choices)) {
choices <- as.character(choices)
}

if (is.null(selected)) {
selected <- choices[1]
} else {
selected <- as.character(selected)
}

from <- match(x = selected[1], table = choices) - 1
if (is.na(from))
stop("Invalid 'selected' arguments, probably not an element of 'choices'.")

if (length(selected) > 1) {
to <- match(x = selected[2], table = choices) - 1
if (is.na(to))
stop("Invalid 'selected' arguments, probably not an element of 'choices'.")
} else {
to <- NULL
}

if (!is.null(from_min))
from_min <- match(x = from_min, table = choices) - 1
if (!is.null(from_max))
from_max <- match(x = from_max, table = choices) - 1
if (!is.null(to_min))
to_min <- match(x = to_min, table = choices) - 1
if (!is.null(to_max))
to_max <- match(x = to_max, table = choices) - 1

sliderProps <- dropNulls(list(
class = "js-range-slider", class = "sw-slider-text",
id = inputId, `data-type` = if (length(selected) > 1) "double",
`data-from` = from,
`data-to` = if (length(selected) > 1) to,
`data-grid` = grid,
`data-hide-min-max` = hide_min_max,
`data-prefix` = pre, `data-postfix` = post,
`data-from-fixed` = from_fixed, `data-to-fixed` = to_fixed,
`data-from-min` = from_min, `data-from-max` = from_max,
`data-to-min` = to_min, `data-to-max` = to_max,
`data-from-shadow` = !is.null(from_min) | !is.null(from_max),
`data-to-shadow` = !is.null(to_min) | !is.null(to_max),
`data-swvalues` = jsonlite::toJSON(x = choices),
`data-keyboard` = TRUE,
`data-drag-interval` = if (length(selected) > 1) dragRange,
`data-data-type` = "text"
))
sliderProps <- lapply(sliderProps, function(x) {
if (identical(x, TRUE))
"true"
else if (identical(x, FALSE))
"false"
else x
})
sliderTag <- htmltools::tags$div(class = "form-group shiny-input-container",
style = if (!is.null(width))
paste0("width: ", htmltools::validateCssUnit(width), ";"),
if (!is.null(label))
htmltools::tags$label(class = "control-label", `for` = inputId,
label), do.call(htmltools::tags$input, sliderProps))
if (identical(animate, TRUE))
animate <- animationOptions()
if (!is.null(animate) && !identical(animate, FALSE)) {
if (is.null(animate$playButton))
animate$playButton <- shiny::icon("play", lib = "glyphicon")
if (is.null(animate$pauseButton))
animate$pauseButton <- shiny::icon("pause", lib = "glyphicon")
sliderTag <- htmltools::tagAppendChild(
sliderTag,
htmltools::tags$div(
class = "slider-animate-container",
htmltools::tags$a(
href = "#", class = "slider-animate-button",
`data-target-id` = inputId, `data-interval` = animate$interval,
`data-loop` = animate$loop, htmltools::tags$span(class = "play",
animate$playButton), htmltools::tags$span(class = "pause",
animate$pauseButton)
)
))
}

dep <- htmltools::findDependencies(shiny::sliderInput(inputId = "id", label = "label", min = 1, max = 10, value = 5))
attachShinyWidgetsDep(
htmltools::attachDependencies(sliderTag, dep)
)
}




#' @title Change the value of a slider text input on the client
#'
#' @description
#' Change the value of a slider text input on the client
#'
#' @param session The session object passed to function given to shinyServer.
#' @param inputId The id of the input object.
#' @param label The label to set.
#' @param selected The values selected.
#' @param choices The new choices for the input.
#' @param from_fixed Fix the left handle (or single handle).
#' @param to_fixed Fix the right handle.
#'
#' @export
#'
#' @seealso \code{\link{sliderTextInput}}
#'
#' @examples
#' \dontrun{
#'
#' if (interactive()) {
#' library("shiny")
#' library("shinyWidgets")
#'
#' ui <- fluidPage(
#' br(),
#' sliderTextInput(
#' inputId = "mySlider",
#' label = "Pick a month :",
#' choices = month.abb,
#' selected = "Jan"
#' ),
#' verbatimTextOutput(outputId = "res"),
#' radioButtons(
#' inputId = "up",
#' label = "Update choices:",
#' choices = c("Abbreviations", "Full names")
#' )
#' )
#'
#' server <- function(input, output, session) {
#' output$res <- renderPrint(str(input$mySlider))
#'
#' observeEvent(input$up, {
#' choices <- switch(
#' input$up,
#' "Abbreviations" = month.abb,
#' "Full names" = month.name
#' )
#' updateSliderTextInput(
#' session = session,
#' inputId = "mySlider",
#' choices = choices
#' )
#' }, ignoreInit = TRUE)
#' }
#'
#' shinyApp(ui = ui, server = server)
#' }
#'
#' }
updateSliderTextInput <- function (session, inputId, label = NULL, selected = NULL, choices = NULL, from_fixed = NULL, to_fixed = NULL) {
message <- dropNulls(list(
label = label, selected = selected, choices = choices,
from_fixed = from_fixed, to_fixed = to_fixed
))
session$sendInputMessage(inputId, message)
}
Loading

0 comments on commit f524a7a

Please sign in to comment.