Automatically upload file in Shiny - Sample Data for Upload UI

0

Is there a way to automatically upload the excel file. Right now, the user has to manually upload the excel file(file.xlsx) that is kept under project folder.

Now the expected output is the moment the user clicks on "Automatically Upload the exceil file", the file should get uploaded.

Is there a way to achieve this? Let me know if this makes sense

library(tidyverse)
library(shiny)
library(shinydashboard)
library(DT)

ui <-
  dashboardPage(
    skin = "green",
    dashboardHeader(
      title      = "Test",
      titleWidth = 280
    ),
    dashboardSidebar(
      width = 280,
      sidebarMenu(
        menuItem(text = "File(s) Upload", tabName = "Files",     icon = icon("file-upload")),
        menuItem(text = "Output", tabName = "Out1",     icon = icon("file-upload"))
      )
    ),
    dashboardBody(
      tabItems(
        tabItem(
          tabName = "Files",
          fluidRow(
            column(
              width = 4,
              inputPanel(
                fileInput(inputId = "File1", label = "File", multiple = TRUE, accept = c(".xlsx")),
                selectInput(inputId = "Sheet1", label = "Select sheet", choices = NULL, selected = NULL),
                actionButton("sub", "Automatically Upload the exceil file")
              )
            )
          )
        ),
        tabItem(
          tabName = "Out1",
          fluidRow(column(width = 10, strong("Data")), align = "center"),
          br(),
          fluidRow(dataTableOutput("Data1"))
        )
      )
    )
  )


server <- function(input, output){
  
  
  # Populate the drop down menu with the names of the different Excel Sheets, but
  # only after a new file is supplied
  observe({
    
    sheet_names <- readxl::excel_sheets(input$File1$datapath)
    
    shiny::updateSelectInput(
      inputId = "Sheet1",
      choices = sheet_names,
      selected = sheet_names[[1]]
    )
    
  }) %>%
    bindEvent(input$File1)
  
  
  # When the drop down meny is populated, read the selected sheet from the Excel
  # file
  thedata <- reactive({
    
    req(input$Sheet1)
    
    readxl::read_xlsx(input$File1$datapath, sheet = input$Sheet1)
    
  })
  
  
  output$Data1 <-
    renderDataTable(
      thedata()
      , extensions = "Buttons"
      , options = list(
        dom = "Bfrtip"
        , buttons = c("copy", "csv", "excel", "pdf", "print")
      )
    )
  
  # observe({
  #   print(reactiveValuesToList(input, all.names = FALSE))
  # })
}

runApp(
  list(ui = ui, server = server)
  , launch.browser = TRUE
)

Just a note that this was also cross-posted here,

Is there a way to automatically upload the excel file.

Shiny apps that need the user to upload some kind of file are pretty common. One approach I frequently see is to offer your users an sample file to get started with instead of uploading their own file.

  1. You can do this by giving a UI option, "Use sample data", which then tells your app to load an excel it already possesses.

  2. Slightly less elegantly, I've also seen some apps that just offer a link to an example file, which the end-users download and re-upload. Though this adds a few steps to get started with the sample file, one advantage is that the example file offers users a nice template, helping them understand the kind of data and format it needs to be in.

Here's an example of the first option Jiddu Alexander created last year. When you enter the app and select "Upload SVG", note the "Or select sample SVG" option.

For reference, here's a nice chapter from Mastering Shiny on uploading and downloading data, Chapter 9 Uploads and downloads | Mastering Shiny. I am struggling to find example so

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.