简体   繁体   English

如何限制在R Shiny中每天一次将文件导入ShinyApp?

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

I want to upload updated csv file daily basis. 我想每天上传更新的csv文件。 Once the csv file get uploaded, the upload icon should disappear and valueBox should display with relevant value. CSV文件上传后,上传图标应消失,并且valueBox应显示相关值。 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. 上面的代码每次访问Shinydashboard时都接受csv文件。 Currently it showing the upload icon each time someone opens the URL/dashboard. 当前,每次有人打开URL /仪表板时,它都会显示上传图标。 I want the upload icon should shown till csv file not uploaded into shinyApp. 我希望应该显示上传图标,直到csv文件没有上传到ShinyApp。 Once uploaded, it should disappear and should display 'valueBox()' with values depend on the uploaded file. 上传后,它应该消失并显示'valueBox()' ,其值取决于上传的文件。 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. 由于您的应用程序将由可以访问URL的多个人使用,因此简单的方法是创建一个全局.rds文件,只要导入.csv文件,所有用户都可以访问。

 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. 但是,我们每天只需要创建一次此.rds文件。 If a file already exists for the current date, we can 如果当前日期已经存在文件,我们可以

1. Skip this step and read the file directly 1.跳过此步骤,直接读取文件
2. Hide the input field from the UI 2.在用户界面中隐藏输入字段

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. 在这里,我们使用shinyjs显示和隐藏字段。 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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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