Debian Codegraph @

inst/examples/09_upload/app.R

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