autoInput: Automatic Input Widget

Description Usage Arguments Details Examples

View source: R/autoInput.R

Description

Constructs an input based on the type and values of provided data.

Usage

1
2
3
4
5
6
7
8
9
autoInput(
  x,
  inputId = NA,
  label = inputId,
  selected_fraction = 0.5,
  range = TRUE,
  inline = TRUE,
  ...
)

Arguments

x

Input data, which can be a vector, a one-column data.frame, or a one-element list. NB: to extract a one-column data.frame or a single element list from a larger one, provide a single index to `[`, i.e. use df[1] instead of df[,1] and l[1] instead of l[[1]], the later forms extract the corresponding vector/content; see the examples.

inputId

The input slot that will be used to access the value. With the default value of NA, if x has a names attribute of length one (i.e. in the case of a one-column data.frame, or a one-element list), it will be used as inputId. Specifying an inputId overrides this behaviour.

label

Display label for the control, by default set to the value of inputId. Set this explicitly to NULL for no label.

selected_fraction

Fraction of the data (between 0 and 1) to select upon creation of the widget.

range

Whether to create a widget that allows selecting a range rather than a single value.

inline

If TRUE, render the choices inline (i.e. horizontally) in checkboxGroupInput.

...

passed to the input widget creation functions.

Details

Widgets are chosen according to the class of x:

In this last case, a simple heuristic is used to create good looking bounds for the slider, although the range selected by default is based on the actual data range. For example, if x varies between 2.16 and 3.98 and selected_fraction=1, the slider goes from 2 to 4 but the range initially selected would be [2.16, 3.98].

Examples

 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
## Only run examples in interactive R sessions
if (interactive()) {
# simple example
set.seed(1)
autoInput(runif(10), "foo", "My automatic slider")

# create a data.frame with various types of data
n <- 40
d <- data.frame(
  boolean=rep(c(TRUE, FALSE), times=n/2),
  few_levels=letters[1:5],
  many_levels=rep(letters,2)[1:n],
  int=1:n,
  num=runif(n, 0, 5),
  large_num=runif(n, 0, 10^6),
  small_num=runif(n, 0, 0.001),
  date=Sys.Date()+1:n,
  date_time=Sys.time()+1:n
)
str(d)

ui <- fluidPage(
  fluidRow(
    column(
      6,
      h2("Gallery"),
      autoInput(d["boolean"]),
      autoInput(d["few_levels"]),
      autoInput(d["many_levels"]),
      autoInput(d["int"]),
      autoInput(d["num"]),
      autoInput(d["large_num"]),
      autoInput(d["small_num"]),
      autoInput(d["date"]),
      autoInput(d["date"], range=FALSE),
      autoInput(d["date_time"]),
      autoInput(d["date_time"], range=FALSE)
    ),
    column(
      6,
      h2("Options"),
      p("width=100%:"), autoInput(d["int"], width="100%", inline=TRUE),
      p("selected_fraction=1:"), autoInput(d["int"], selected_fraction=1),
      p("selected_fraction=0:"), autoInput(d["int"], selected_fraction=0),
      p("selected_fraction=0.0001; when the variable is categorical,
      the number of elements is  rounded up to select at least one:"),
      autoInput(d["few_levels"], selected_fraction=0.0001),
      p("to select nothing, set selected_fraction to exactly 0:"),
      autoInput(d["few_levels"], selected_fraction=0),
      p("inline=FALSE:"), autoInput(d["few_levels"], inline=FALSE),
      p("Change label and ticks aspect:"),
      autoInput(d["small_num"], label=NULL),
      autoInput(d["small_num"], sep=""),
      autoInput(d["small_num"], ticks=FALSE),
      p("etc.")
    )
  )
)
server <- function(input, output) {}
shinyApp(ui, server)
}

jiho/qualitr documentation built on July 9, 2020, 12:49 a.m.