Debian Codegraph @

R/input-file.R

raw
1 #' File Upload Control
2 #'
3 #' Create a file upload control that can be used to upload one or more files.
4 #'
5 #' Whenever a file upload completes, the corresponding input variable is set to
6 #' a dataframe. See the `Server value` section.
7 #'
8 #' Each time files are uploaded, they are written to a new random subdirectory
9 #' inside of R's process-level temporary directory. The Shiny user session keeps
10 #' track of all uploads in the session, and when the session ends, Shiny deletes
11 #' all of the subdirectories where files where uploaded to.
12 #'
13 #' @family input elements
14 #'
15 #' @inheritParams textInput
16 #' @param multiple Whether the user should be allowed to select and upload
17 #' multiple files at once. **Does not work on older browsers, including
18 #' Internet Explorer 9 and earlier.**
19 #' @param accept A character vector of "unique file type specifiers" which gives
20 #' the browser a hint as to the type of file the server expects. Many browsers
21 #' use this prevent the user from selecting an invalid file.
22 #'
23 #' A unique file type specifier can be:
24 #' * A case insensitive extension like `.csv` or `.rds`.
25 #' * A valid MIME type, like `text/plain` or `application/pdf`
26 #' * One of `audio/*`, `video/*`, or `image/*` meaning any audio, video,
27 #' or image type, respectively.
28 #' @param buttonLabel The label used on the button. Can be text or an HTML tag
29 #' object.
30 #' @param placeholder The text to show before a file has been uploaded.
31 #' @param capture What source to use for capturing image, audio or video data.
32 #' This attribute facilitates user access to a device's media capture
33 #' mechanism, such as a camera, or microphone, from within a file upload
34 #' control.
35 #'
36 #' A value of `user` indicates that the user-facing camera and/or microphone
37 #' should be used. A value of `environment` specifies that the outward-facing
38 #' camera and/or microphone should be used.
39 #'
40 #' By default on most phones, this will accept still photos or video. For
41 #' still photos only, also use `accept="image/*"`. For video only, use
42 #' `accept="video/*"`.
43 #' @examples
44 #' ## Only run examples in interactive R sessions
45 #' if (interactive()) {
46 #'
47 #' ui <- fluidPage(
48 #' sidebarLayout(
49 #' sidebarPanel(
50 #' fileInput("file1", "Choose CSV File", accept = ".csv"),
51 #' checkboxInput("header", "Header", TRUE)
52 #' ),
53 #' mainPanel(
54 #' tableOutput("contents")
55 #' )
56 #' )
57 #' )
58 #'
59 #' server <- function(input, output) {
60 #' output$contents <- renderTable({
61 #' file <- input$file1
62 #' ext <- tools::file_ext(file$datapath)
63 #'
64 #' req(file)
65 #' validate(need(ext == "csv", "Please upload a csv file"))
66 #'
67 #' read.csv(file$datapath, header = input$header)
68 #' })
69 #' }
70 #'
71 #' shinyApp(ui, server)
72 #' }
73 #'
74 #' @section Server value:
75 #'
76 #' A `data.frame` that contains one row for each selected file, and following
77 #' columns:
78 #' \describe{
79 #' \item{`name`}{The filename provided by the web browser. This is
80 #' **not** the path to read to get at the actual data that was uploaded
81 #' (see
82 #' `datapath` column).}
83 #' \item{`size`}{The size of the uploaded data, in
84 #' bytes.}
85 #' \item{`type`}{The MIME type reported by the browser (for example,
86 #' `text/plain`), or empty string if the browser didn't know.}
87 #' \item{`datapath`}{The path to a temp file that contains the data that was
88 #' uploaded. This file may be deleted if the user performs another upload
89 #' operation.}
90 #' }
91 #'
92 #' @export
93 fileInput <- function(inputId, label, multiple = FALSE, accept = NULL,
94 width = NULL, buttonLabel = "Browse...", placeholder = "No file selected",
95 capture = NULL) {
96
97 restoredValue <- restoreInput(id = inputId, default = NULL)
98
99 # Catch potential edge case - ensure that it's either NULL or a data frame.
100 if (!is.null(restoredValue) && !is.data.frame(restoredValue)) {
101 warning("Restored value for ", inputId, " has incorrect format.")
102 restoredValue <- NULL
103 }
104
105 if (!is.null(restoredValue)) {
106 restoredValue <- toJSON(restoredValue, strict_atomic = FALSE)
107 }
108
109 inputTag <- tags$input(
110 id = inputId,
111 class = "shiny-input-file",
112 name = inputId,
113 type = "file",
114 # Don't use "display: none;" style, which causes keyboard accessibility issue; instead use the following workaround: https://css-tricks.com/places-its-tempting-to-use-display-none-but-dont/
115 style = "position: absolute !important; top: -99999px !important; left: -99999px !important;",
116 `data-restore` = restoredValue
117 )
118
119 if (multiple)
120 inputTag$attribs$multiple <- "multiple"
121 if (length(accept) > 0)
122 inputTag$attribs$accept <- paste(accept, collapse=',')
123
124 if (!is.null(capture)) {
125 inputTag$attribs$capture <- capture
126 }
127
128 div(class = "form-group shiny-input-container",
129 style = css(width = validateCssUnit(width)),
130 shinyInputLabel(inputId, label),
131
132 div(class = "input-group",
133 # input-group-prepend is for bootstrap 4 compat
134 tags$label(class = "input-group-btn input-group-prepend",
135 span(class = "btn btn-default btn-file",
136 buttonLabel,
137 inputTag
138 )
139 ),
140 tags$input(type = "text", class = "form-control",
141 placeholder = placeholder, readonly = "readonly"
142 )
143 ),
144
145 tags$div(
146 id=paste(inputId, "_progress", sep=""),
147 class="progress active shiny-file-input-progress",
148 tags$div(class="progress-bar")
149 )
150 )
151 }