![](/img/trans.png)
[英]Shiny and R: load data only once - do not load at each start of app and have progress bar
[英]Load files only once in R shiny server end
如果數據文件沒有變化但模型參數發生了變化,有沒有辦法在閃亮的服務器端只加載一次數據文件(由用戶上傳)? 這就是說,當 R 代碼上傳和讀取文件時,用戶每次通過 Web UI 更改模型參數時,代碼都不會重新加載數據文件。 我對 R 閃亮很陌生。 我找不到任何例子。 謝謝!
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.