简体   繁体   中英

How to restrict, importing file to shinyApp once per day in R shiny?

I want to upload updated csv file daily basis. Once the csv file get uploaded, the upload icon should disappear and valueBox should display with relevant value. Here is the below code:

library(shiny)
library(shinydashboard)
# Define UI for application that draws a histogram
ui <- dashboardPage(
    dashboardHeader(title = "Upload Stats"),
    dashboardSidebar(),
    dashboardBody(
        box(
            title = "UPTIME:", width = 12, 
    div(column(width = 4, fileInput(inputId = "file", label = "import", accept = ".csv")),
        column(width = 8, valueBoxOutput("stats"))
            )
        ) 
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$stats <- renderValueBox({
        req(input$file)
        data <- read.csv(input$file$datapath)
        valueBox("scr1", sum(data[,2]), width = 12)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

The above code accepting csv file each time visiting the shinydashboard. Currently it showing the upload icon each time someone opens the URL/dashboard. I want the upload icon should shown till csv file not uploaded into shinyApp. Once uploaded, it should disappear and should display 'valueBox()' with values depend on the uploaded file. Can someone help me how to write that control code?

Since your application is going to be used by multiple people who can access the URL, the simple way would be to create a global .rds file accessible by all the users whenever the .csv file is imported.

 data <- read.csv(input$file$datapath)

 # Create a folder named srcdata under www folder in your app directory
 # Save the data object as a .rds file with system date appended to the file name
 saveRDS(data,paste0("www/srcdata/data_",Sys.Date()))

However, we would need to create this .rds file only once per day. If a file already exists for the current date, we can

1. Skip this step and read the file directly
2. Hide the input field from the UI

So the code becomes

filePresent <- list.files("www/srcdata/", pattern = paste0("data_",Sys.Date()))

# if file is present, disable the input field and read from the saved .rds
# if file is not present, show the input field

 if(length(filePresent)==1){
        data <- readRDS(paste0("www/srcdata/data_",Sys.Date()))
        filedata$notPresent <- FALSE
      }else{
        shinyjs::show("file")
      }

Here, we are using shinyjs to show and hide the fields. So you would need to install that package (if not already) and call it in your code. Also, this code should run every time the app gets initialized so that the users either get presented with data (if there is a saved file) or sees a input field to import the file.

I have updated the code to implement this

library(shiny)
library(shinydashboard)
library(shinyjs)
# Define UI for application that draws a histogram
ui <- dashboardPage(
  dashboardHeader(title = "Upload Stats"),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    box(
      title = "UPTIME:", width = 12, 
      div(column(width = 4, hidden(fileInput(inputId = "file", label = "import", accept = ".csv"))),
          column(width = 8, valueBoxOutput("stats"))
      )
    ) 
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  filedata <- reactiveValues(notPresent = TRUE)

  observeEvent(filedata$notPresent,{
    if(filedata$notPresent){

      filePresent <- list.files("www/srcdata/", pattern = paste0("data_",Sys.Date()))

      if(length(filePresent)==1){
        data <- readRDS(paste0("www/srcdata/data_",Sys.Date()))
        filedata$notPresent <- FALSE
      }else{
        shinyjs::show("file")
      }

    }

  })
  output$stats <- renderValueBox({
    req(input$file)
    data <- read.csv(input$file$datapath)
    saveRDS(data,paste0("www/srcdata/data_",Sys.Date()))
    valueBox("scr1", sum(data[,2]), width = 12)
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM