【问题标题】:Load files only once in R shiny server end在 R 闪亮的服务器端只加载一次文件
【发布时间】:2016-05-10 20:54:08
【问题描述】:

如果数据文件没有改变但是模型参数改变了,有没有办法在闪亮的服务器端只加载一次数据文件(由用户上传)?也就是说,当R代码上传和读取文件时,用户每次通过web UI更改模型参数时,代码都不会重新加载数据文件。我对 R 闪亮很陌生。我找不到任何例子。谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    Shiny 非常聪明。

    它知道什么时候需要再次做某事,什么时候不需要。这是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)
    

    【讨论】:

    • 谢谢。我想我明白了。但是您的代码似乎没有 eventReactive 调用。
    猜你喜欢
    • 2019-11-18
    • 2014-05-06
    • 2017-11-03
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 2017-07-04
    相关资源
    最近更新 更多