【发布时间】:2017-10-13 22:29:36
【问题描述】:
我只是想知道如何只在 server.R 中运行 read.csv 一次?在下面的代码中,每次我更改 ui.R 中的输入时,代码运行 read.csv 需要几秒钟,因此我期待在开始时只加载一次文件然后使用相同的ui.R 中输入时的数据发生变化。谢谢 这是我的代码:
ui.R
shinyUI(fluidPage(
headerPanel("myApp"),
fluidRow( sidebarPanel(
selectInput("myOptions", "myOptions:",
list("1" = "1",
"2" = "2",
"3" = "3"))
)),
hr(),
plotlyOutput("prescription"),
hr()
))
服务器.R
library(utils)
library(plotly)
library(plyr)
shinyServer(function(input, output) {
diseases<-eventReactive({
diseases=read.csv("diseases_94.csv",header=F)
})
output$prescription <- renderPlotly( {
myDiseases=diseases
freq=count(myDiseases,"DISEASES")
p<-plot_ly(freq, labels = ~DISEASES, values = ~freq, type = 'pie',
textposition = 'inside',
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
hoverinfo = 'text',
text = ~paste(DISEASES),
marker = list(line = list(color = '#FFFFFF', width = 1)))
p
})
})
【问题讨论】:
-
如果你只想加载一次,为什么只能在服务器函数之外加载呢?也许您可以告诉我们更多您希望何时以及如何读取数据,...
-
@BigDataScientist 感谢您的回复。当我运行闪亮的应用程序时,我只需要在开始时加载。然后我应该将它作为输入参数传递给 shinyServer 函数吗?
-
你可以像普通变量一样使用它,..
-
@BigDataScientist 明白了!它现在正在工作。非常感谢