-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgetNumericRangeInput.R
120 lines (100 loc) · 3.32 KB
/
widgetNumericRangeInput.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#'
#' Range numeric input. Because the slider is too unprecise
#'
library(shiny)
#' Creates a control that allows the user to select a range of numbers with precision of numericInput
#'
#' @param id
#' @param label.from
#' @param label.to
#'
#' @return
#' @export
#'
#' @examples
numericRangeInput <- function(id, label.from, label.to) {
if(!is.character(id)) {
stop("Invalid arguments!")
}
ns <- NS(id)
return(tagList(
fixedRow(
column(width = 6, numericInput(ns("from"), label.from, value = 1)),
column(width = 6, numericInput(ns("to"), label.to, value = 1))
),
textOutput(ns("message"))
))
}
#' Returns value of numeric range input
#'
#' @param input
#' @param output
#' @param session
#' @param value.min Reactive returning the min. value
#' @param value.max Reactive returning the max. value
#' @param value.default.min Optional reactive returning the default min value (NULL for value.min)
#' @param value.default.max Optional reactive returning the default max value (NULL for value.max)
#'
#' @return List with range start (from) and range end (to)
#' @export
#'
#' @examples
numericRangeInputValue_ <- function(input, output, session, value.min, value.max, value.default.min = NULL, value.default.max = NULL) {
observe({
validate(need(value.min(), "No minimum defined!"),
need(value.max(), "No maximum defined!"))
default.min <- value.min()
default.max <- value.max()
if(!is.null(value.default.min)) {
default.min <- value.default.min()
}
if(!is.null(value.default.max)) {
default.max <- value.default.max()
}
updateNumericInput(session, "from",value = default.min, min = value.min(), max = value.max())
updateNumericInput(session, "to", value = default.max, min = value.min(), max = value.max())
})
from <- reactive({
return(min(value.max(), max(value.min(), input$from)))
})
to <- reactive({
return(min(value.max(), max(value.min(), input$to)))
})
range <- reactive( {
if(!is.na(from()) && !is.na(to()) && from() < to()) {
return(list("from" = from(), "to" = to()))
}
else {
return(list("from" = value.min(), "to" = value.max()))
}
})
output$message <- renderText({
if(!is.na(from()) && !is.na(to()) && from() < to()) {
return("")
}
else {
return("Invalid range!")
}
})
return(range)
}
#' Returns value of numeric range input
#' This function is supposed to be called by callModule. Use the one without an underscore for easier access.
#'
#' @param value.min Reactive returning the min. value
#' @param value.max Reactive returning the max. value
#' @param value.default.min Optional reactive returning the default min value (NULL for value.min)
#' @param value.default.max Optional reactive returning the default max value (NULL for value.max)
#'
#' @return List with range start (from) and range end (to)
#' @export
#'
#' @examples
numericRangeInputValue <- function(id, value.min, value.max, value.default.min = NULL, value.default.max = NULL) {
return(callModule(numericRangeInputValue_,
id,
value.min = value.min,
value.max = value.max,
value.default.min = value.default.min,
value.default.max = value.default.max))
}