0

I have a requirement to refresh bar chart in UI based on an data in a file.

Sample file:

CCS_DIAGNOSIS_DESCRIPTION,DISCHARGE_YEAR,AGE_GROUP,Number Of Cases,Year_Age_Y
ABDOMINAL HERNIA,2009,0 to 17,425,2009
ABDOMINAL HERNIA,2009,,329,
ABDOMINAL PAIN,2009,0 to 17,966,2009
ABDOMINAL PAIN,2009,,1459,

ui.r:

library(shiny)

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.'
      )
    ),
    mainPanel(
      plotOutput('distPlot')
    )


  )
)
)

Server.R

library(shiny)


shinyServer(function(input, output) {


  output$distPlot <- renderPlot({

    inputfile <- input$file1

    if (is.null(inputfile))
      return(NULL)

    ds <-read.csv(inputfile$datapath, header = input$header,
                  sep = input$sep, quote = input$quote)
    #print(ds)
    age_grp <- c("0 to 17", "18 to 29", "30 to 49", "50 to 69", "70 or Older")
    colours <- c("red", "orange", "blue", "yellow", "green")
    unq= unique(ds$CCS_DIAGNOSIS_DESCRIPTION,incomparables = FALSE)

    for(val in unq[1:2]){    
      print(val)

      df= ds[ds$CCS_DIAGNOSIS_DESCRIPTION == val ,c("Year_Age_Y","Number.Of.Cases")]

      barplot(df$`Number.Of.Cases`,names.arg = df$Year_Age_Y,ylab=val, col=colours,beside=TRUE,legend = age_grp)
      Sys.sleep(5)
    }
  })


})

When I execute this code, the graph gets plotted only for the last value read from the input. It does go in the the loop and prints the values as desirec ut the plotted chart does not change. It just shows a blank screen for initial iterations and then a graph with late one.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
tej246
  • 11
  • 2
  • I think what you are looking for is in this [link](http://stackoverflow.com/questions/18302579/update-graph-plot-with-fixed-interval-of-time) – SBista Mar 02 '17 at 10:42

1 Answers1

0

From the link I mentioned in the comment above, I was able to come with a solution by modifying your server code. This might not be the most elegant way to do it, but works in your case.

shinyServer(function(input, output, session) {
  autoInvalidate <- reactiveTimer(10000, session)
  inside = FALSE

  observeEvent(input$file1, {
    inputfile <- input$file1

    if (is.null(inputfile))
      return(NULL)

    ds <-read.csv(inputfile$datapath, header = input$header,
                  sep = input$sep, quote = input$quote)
    print(ds)
    age_grp <- c("0 to 17", "18 to 29", "30 to 49", "50 to 69", "70 or Older")
    colours <- c("red", "orange", "blue", "yellow", "green")
    unq= unique(ds$CCS_DIAGNOSIS_DESCRIPTION,incomparables = FALSE)
    val<-unq[1]
    output$distPlot <- renderPlot({
      # browser()
      autoInvalidate()
      print(val)
      df= ds[ds$CCS_DIAGNOSIS_DESCRIPTION == val ,c("Year_Age_Y","Number.Of.Cases")]
      if(inside == FALSE)
      {
        inside <<- TRUE
      }else{
        val<<-unq[2]
      }
      barplot(df$`Number.Of.Cases`,names.arg = df$Year_Age_Y,ylab=val, col=colours,beside=TRUE,legend = age_grp)

    })

  })

})

[EDIT]:

I have edited the code further to handle if there are more than two cases. Here based on the different unique cases the plot keeps refreshing and returns to the first plot when all the cases has been plotted.

shinyServer(function(input, output, session) {
  autoInvalidate <- reactiveTimer(10000, session)
  plot_num <- 0

  observeEvent(input$file1, {
    inputfile <- input$file1

    if (is.null(inputfile))
      return(NULL)

    ds <-read.csv(inputfile$datapath, header = input$header,
                  sep = input$sep, quote = input$quote)

    age_grp <- c("0 to 17", "18 to 29", "30 to 49", "50 to 69", "70 or Older")
    colours <- c("red", "orange", "blue", "yellow", "green")
    unq= unique(ds$CCS_DIAGNOSIS_DESCRIPTION,incomparables = FALSE)
    output$distPlot <- renderPlot({
      if((plot_num+1)> length(unq))
      {
        plot_num <<- 0
      }
      autoInvalidate()
      val <-unq[plot_num+1]
      df= ds[ds$CCS_DIAGNOSIS_DESCRIPTION == val ,c("Year_Age_Y","Number.Of.Cases")]
      plot_num <<- plot_num+1
      barplot(df$`Number.Of.Cases`,names.arg = df$Year_Age_Y,ylab=val, col=colours,beside=TRUE,legend = age_grp)

    })
  })

})

Hope it helps!

SBista
  • 7,479
  • 1
  • 27
  • 58