Mohammad
Mohammad

Reputation: 1108

How to load a file in Shiny only once?

I'm just wondering how to run read.csv in server.R only once? In the following code, each time that I change the input in ui.R, it takes a few seconds for the code to run read.csv, therefore I'm looking forward to load the file only once at the beginning then use the same data when input in ui.R changes. Thanks Here is my code:

ui.R

shinyUI(fluidPage(
     headerPanel("myApp"),
     fluidRow(   sidebarPanel(
    selectInput("myOptions", "myOptions:",
                list("1" = "1", 
                     "2" = "2",
                     "3" = "3"))
     )),
     hr(),
     plotlyOutput("prescription"),   
     hr()   
      ))

server.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


 })


 })

Upvotes: 1

Views: 1388

Answers (1)

Edith Reyes Loya
Edith Reyes Loya

Reputation: 21

Use the variable outside of shinyServer. This should load the file only in the beginning.

Upvotes: 2

Related Questions