-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathinput-knob.R
215 lines (206 loc) · 6.9 KB
/
input-knob.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#' Knob Input
#'
#' @param inputId The \code{input} slot that will be used to access the value.
#' @param label Display label for the control, or NULL for no label.
#' @param value Initial value.
#' @param min Minimum allowed value, default to \code{0}.
#' @param max Maximum allowed value, default to \code{100}.
#' @param step Specifies the interval between each selectable value, default to \code{1}.
#' @param angleOffset Starting angle in degrees, default to \code{0}.
#' @param angleArc Arc size in degrees, default to \code{360}.
#' @param cursor Display mode "cursor", don't work properly if \code{width} is not set in pixel, (\code{TRUE} or \code{FALSE}).
#' @param thickness Gauge thickness, numeric value.
#' @param lineCap Gauge stroke endings, 'default' or 'round'.
#' @param displayInput Hide input in the middle of the knob (\code{TRUE} or \code{FALSE}).
#' @param displayPrevious Display the previous value with transparency (\code{TRUE} or \code{FALSE}).
#' @param rotation Direction of progression, 'clockwise' or 'anticlockwise'.
#' @param fgColor Foreground color.
#' @param inputColor Input value (number) color.
#' @param bgColor Background color.
#' @param pre A prefix string to put in front of the value.
#' @param post A suffix string to put after the value.
#' @param fontSize Font size, must be a valid CSS unit.
#' @param readOnly Disable knob (\code{TRUE} or \code{FALSE}).
#' @param skin Change Knob skin, only one option available : 'tron'.
#' @param width,height The width and height of the input, e.g. `400px`, or `100%`.
#' A value a pixel is recommended, otherwise the knob won't be able to initialize itself in some case
#' (if hidden at start for example).
#' @param immediate If \code{TRUE} (default), server-side value is updated each time value change,
#' if \code{FALSE} value is updated when user release the widget.
#'
#' @return Numeric value server-side.
#' @export
#'
#' @seealso \code{\link{updateKnobInput}} for updating the value server-side.
#'
#' @importFrom shiny restoreInput
#' @importFrom htmltools tags validateCssUnit
#'
#' @examples
#' if (interactive()) {
#'
#' library("shiny")
#' library("shinyWidgets")
#'
#' ui <- fluidPage(
#' knobInput(
#' inputId = "myKnob",
#' label = "Display previous:",
#' value = 50,
#' min = -100,
#' displayPrevious = TRUE,
#' fgColor = "#428BCA",
#' inputColor = "#428BCA"
#' ),
#' verbatimTextOutput(outputId = "res")
#' )
#'
#' server <- function(input, output, session) {
#'
#' output$res <- renderPrint(input$myKnob)
#'
#' }
#'
#' shinyApp(ui = ui, server = server)
#'
#' }
knobInput <- function(inputId, label, value,
min = 0, max = 100, step = 1,
angleOffset = 0, angleArc = 360,
cursor = FALSE,
thickness = NULL,
lineCap = c("default", "round"),
displayInput = TRUE,
displayPrevious = FALSE,
rotation = c("clockwise", "anticlockwise"),
fgColor = NULL,
inputColor = NULL,
bgColor = NULL,
pre = NULL, post = NULL,
fontSize = NULL,
readOnly = FALSE,
skin = NULL,
width = NULL,
height = NULL,
immediate = TRUE) {
value <- shiny::restoreInput(id = inputId, default = value)
lineCap <- match.arg(lineCap)
rotation <- match.arg(rotation)
# if (!is.null(skin))
# warning("Argument skin is deprecated")
knobParams <- dropNulls(list(
id = inputId, type = "text", class = "knob-input",
`data-value` = value, `data-skin` = skin,
`data-min` = min, `data-max` = max, `data-step` = step,
`data-angleoffset` = angleOffset, `data-anglearc` = angleArc,
`data-displayprevious` = displayPrevious, `data-thickness` = thickness,
`data-displayinput` = displayInput, `data-linecap` = lineCap,
`data-fgcolor` = fgColor, `data-inputcolor` = inputColor,
`data-bgcolor` = bgColor, `data-cursor` = cursor,
`data-rotation` = rotation, `data-readonly` = readOnly,
`data-pre` = pre, `data-post` = post,
`data-width` = width, `data-height` = height,
`data-immediate` = immediate
))
knobParams <- lapply(knobParams, function(x) {
if (identical(x, TRUE))
"true"
else if (identical(x, FALSE))
"false"
else x
})
inputTag <- do.call(tags$input, knobParams)
knobInputTag <- tags$div(
class = "form-group shiny-input-container",
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
if (!is.null(fontSize)) {
tags$style(sprintf(
"#%s {font-size: %s !important;}",
inputId, validateCssUnit(fontSize)
))
},
if (!is.null(label)) tags$label(`for` = inputId, label),
if (!is.null(label)) tags$br(),
inputTag
)
attachShinyWidgetsDep(knobInputTag, "jquery-knob")
}
#' Change the value of a knob input on the client
#'
#' @param session Standard shiny \code{session}.
#' @param inputId The id of the input object.
#' @param label The label to set for the input object.
#' @param value The value to set for the input object.
#' @param options List of additional parameters to update, use \code{knobInput}'s arguments.
#'
#' @export
#'
#' @examples
#' if (interactive()) {
#'
#' library("shiny")
#' library("shinyWidgets")
#'
#' ui <- fluidPage(
#' tags$h1("knob update examples"),
#' br(),
#'
#' fluidRow(
#'
#' column(
#' width = 6,
#' knobInput(
#' inputId = "knob1", label = "Update value:",
#' value = 75, angleOffset = 90, lineCap = "round"
#' ),
#' verbatimTextOutput(outputId = "res1"),
#' sliderInput(
#' inputId = "upknob1", label = "Update knob:",
#' min = 0, max = 100, value = 75
#' )
#' ),
#'
#' column(
#' width = 6,
#' knobInput(
#' inputId = "knob2", label = "Update label:",
#' value = 50, angleOffset = -125, angleArc = 250
#' ),
#' verbatimTextOutput(outputId = "res2"),
#' textInput(inputId = "upknob2", label = "Update label:")
#' )
#'
#' )
#' )
#'
#' server <- function(input, output, session) {
#'
#' output$res1 <- renderPrint(input$knob1)
#'
#' observeEvent(input$upknob1, {
#' updateKnobInput(
#' session = session,
#' inputId = "knob1",
#' value = input$upknob1
#' )
#' }, ignoreInit = TRUE)
#'
#'
#' output$res2 <- renderPrint(input$knob2)
#' observeEvent(input$upknob2, {
#' updateKnobInput(
#' session = session,
#' inputId = "knob2",
#' label = input$upknob2
#' )
#' }, ignoreInit = TRUE)
#'
#' }
#'
#' shinyApp(ui = ui, server = server)
#'
#' }
updateKnobInput <- function(session, inputId, label = NULL, value = NULL, options = NULL) {
message <- dropNulls(list(label = label, value = value, options = options))
session$sendInputMessage(inputId, message)
}