简体   繁体   English

在 R 闪亮的服务器端只加载一次文件

[英]Load files only once in R shiny server end

If the data file does not change but the model parameter changes, is there any way to only load the data file (uploaded by user) once in shiny server end?如果数据文件没有变化但模型参数发生了变化,有没有办法在闪亮的服务器端只加载一次数据文件(由用户上传)? This is to say, when the file is uploaded and read by the R code, every time the user change the model parameter through web UI, the code does not reload the data file.这就是说,当 R 代码上传和读取文件时,用户每次通过 Web UI 更改模型参数时,代码都不会重新加载数据文件。 I am very new to R shiny.我对 R 闪亮很陌生。 I could not find any example.我找不到任何例子。 Thanks!谢谢!

Shiny is pretty smart. Shiny 很聪明。

It knows when it needs to do something again and when it doesn't.它知道什么时候需要再次做某事,什么时候不需要。 This is a case when Shiny knows it doesn't need to reload the file, changing the parameter does not prompt a reload.这是 Shiny 知道不需要重新加载文件的情况,更改参数不会提示重新加载。

library(shiny)

options(shiny.maxRequestSize = 9*1024^2)

server <- shinyServer(function(input, output) {


  data <- eventReactive(input$go, {
    validate(
      need(input$file1, "Choose a file!")
    )

    inFile <- input$file1

    read.csv(inFile$datapath, header = input$header,
             sep = input$sep, quote = input$quote)
  })

  output$plot <- renderPlot({
    set <- data()
    plot(set[, 1], set[, 2] * input$param)
  })
})


ui <- shinyUI(fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose file to upload',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  'text/tab-separated-values',
                  'text/plain',
                  '.csv',
                  '.tsv'
                )
      ),
      tags$hr(),
      checkboxInput('header', 'Header', TRUE),
      radioButtons('sep', 'Separator',
                   c(Comma=',',
                     Semicolon=';',
                     Tab='\t'),
                   ','),
      radioButtons('quote', 'Quote',
                   c(None='',
                     'Double Quote'='"',
                     'Single Quote'="'"),
                   '"'),
      tags$hr(),
      p('If you want a sample .csv or .tsv file to upload,',
        'you can first download the sample',
        a(href = 'mtcars.csv', 'mtcars.csv'), 'or',
        a(href = 'pressure.tsv', 'pressure.tsv'),
        'files, and then try uploading them.'
      ),
      actionButton("go", "Load File and plot"),
      sliderInput("param", "Parameter", 0, 1, value = 0.1, step = 0.1, animate = TRUE)

    ),
    mainPanel(
      tableOutput('contents'),
      plotOutput("plot")
    )


  )
))

shinyApp(ui = ui, server = server)

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

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