繁体   English   中英

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

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

我想每天上传更新的csv文件。 CSV文件上传后,上传图标应消失,并且valueBox应显示相关值。 这是下面的代码:

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)

上面的代码每次访问Shinydashboard时都接受csv文件。 当前,每次有人打开URL /仪表板时,它都会显示上传图标。 我希望应该显示上传图标,直到csv文件没有上传到ShinyApp。 上传后,它应该消失并显示'valueBox()' ,其值取决于上传的文件。 有人可以帮我如何编写控制代码吗?

由于您的应用程序将由可以访问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()))

但是,我们每天只需要创建一次此.rds文件。 如果当前日期已经存在文件,我们可以

1.跳过此步骤,直接读取文件
2.在用户界面中隐藏输入字段

所以代码变成

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")
      }

在这里,我们使用shinyjs显示和隐藏字段。 因此,您需要安装该软件包(如果尚未安装)并在代码中调用它。 另外,此代码应在每次初始化应用程序时运行,以便向用户显示数据(如果有保存的文件)或看到输入字段以导入文件。

我已经更新了代码以实现此目的

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)

希望这可以帮助!

暂无
暂无

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

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