Debian Codegraph @

inst/examples-shiny/09_upload/app.R

raw
1 library(shiny)
2 library(bslib)
3
4 # Define UI for slider demo app ----
5 ui <- page_sidebar(
6
7 # App title ----
8 title = "Uploading Files",
9
10 # Sidebar panel for inputs ----
11 sidebar = sidebar(
12
13 # Input: Select a file ----
14 fileInput(
15 "file1",
16 "Choose CSV File",
17 multiple = TRUE,
18 accept = c(
19 "text/csv",
20 "text/comma-separated-values,text/plain",
21 ".csv"
22 )
23 ),
24
25 # Horizontal line ----
26 tags$hr(),
27
28 # Input: Checkbox if file has header ----
29 checkboxInput("header", "Header", TRUE),
30
31 # Input: Select separator ----
32 radioButtons(
33 "sep",
34 "Separator",
35 choices = c(
36 Comma = ",",
37 Semicolon = ";",
38 Tab = "\t"
39 ),
40 selected = ","
41 ),
42
43 # Input: Select quotes ----
44 radioButtons(
45 "quote",
46 "Quote",
47 choices = c(
48 None = "",
49 "Double Quote" = '"',
50 "Single Quote" = "'"
51 ),
52 selected = '"'
53 ),
54
55 # Horizontal line ----
56 tags$hr(),
57
58 # Input: Select number of rows to display ----
59 radioButtons(
60 "disp",
61 "Display",
62 choices = c(
63 Head = "head",
64 All = "all"
65 ),
66 selected = "head"
67 )
68 ),
69
70 # Output: Data file ----
71 tableOutput("contents")
72 )
73
74 # Define server logic to read selected file ----
75 server <- function(input, output) {
76 output$contents <- renderTable({
77 # input$file1 will be NULL initially. After the user selects
78 # and uploads a file, head of that data file by default,
79 # or all rows if selected, will be shown.
80
81 req(input$file1)
82
83 df <- read.csv(
84 input$file1$datapath,
85 header = input$header,
86 sep = input$sep,
87 quote = input$quote
88 )
89
90 if (input$disp == "head") {
91 return(head(df))
92 } else {
93 return(df)
94 }
95 })
96 }
97
98 # Create Shiny app ----
99 shinyApp(ui, server)