0

I have a shinydashboard which displays the plots and boxes based on the data present in a single CSV file. I have scheduled the CSV to get updated for every 1 hour.But the shinydashboard isnt getting updated unless i refresh it manually or run it again(which is not ideal at all).

I have tried invalidatelater() but it doesnt seem to work.

Here is a small snippet.

server<-function(input,output,session){


`observe({

  invalidateLater(60000,session)

  isolate(Day<-read.csv("C:/Users/user1/Desktop/dashboardstuff/querycode.csv"))

  })

querycode is the name of the CSV. This csv gets updated every 60 min and i want my dashboard to get updated as well. Not sure if this is the right way of doing it. Anyhelp would be appreciated.

Just to put up my code,

ui<-dashboardPage(
dashboardHeader(title = "METRIC DASHBOARD"),
dashboardSidebar(
 useShinyjs(),
 sidebarMenu(
  id='tabs',
  menuItem("NA DAY COUNT", tabName = "tab1") ,
  menuItem("EU DAY COUNT", tabName = "tab2"),
  menuItem("JA DAY COUNT", tabName = "tab3"),
  menuItem("CUMULATIVE DAY COUNT", tabName = "tab4") ,
  menuItem("EU DEFECTS SUBMITTED", tabName = "tab5"),
  menuItem("NA DEFECTS SUBMITTED", tabName = "tab6") ,
  menuItem("JA DEFECTS SUBMITTED", tabName = "tab7") ,
  menuItem("Total DEFECTS SUBMITTED", tabName = "tab8"),
  menuItem("HOURLY PRODUCTIVITY", tabName = "tab9") ,
  menuItem("HOURLY DEFECTS SUBMITTED", tabName = "tab10")
 )),
dashboardBody(
tabItems(
  tabItem(
    tabName = 'tab1',
      highchartOutput("h1",height = 700)
    ),
  tabItem(
    tabName = 'tab2',
    highchartOutput("h2",height = 700)
  ),
  tabItem(
    tabName = 'tab3',
    highchartOutput("h3",height = 700)
  ),
  tabItem(
    tabName = 'tab4',
    highchartOutput("h4",height = 700)
  ),
  tabItem(
    tabName = 'tab5',
    highchartOutput("h5",height = 700)
  ),
  tabItem(
    tabName = 'tab6',
    highchartOutput("h6",height = 700)
  ),
  tabItem(
    tabName = 'tab7',
    highchartOutput("h7",height = 700)
  ),
  tabItem(
    tabName = 'tab8',
    highchartOutput("h8",height = 700)
  ))
)
)

tabnames = c('tab1', 'tab2','tab3','tab4','tab5','tab6','tab7','tab8')

And here is the server part,

server<-function(input,output,session){

observe({

  invalidateLater(60000,session)

  isolate(Day<-read.csv("C:/Users/user1/Desktop/dashboardstuff/querycode.csv"))

  })

# Day <- reactiveFileReader(60000*5,session,'C:/Users/pchintap/Desktop/dashboardstuff/querycode.csv',read.csv,header=TRUE)


# Day <- read.csv("C:/Users/user1/Desktop/dashboardstuff/querycode.csv")

  # })

strptime(Day$review_started_timestamp,"%Y-%m-%d %H:%M:%S")+5*3600+30*60->Day$review_started_timestamp
substr(Day$review_started_timestamp,6,7)->Day$Month
substr(Day$review_started_timestamp,1,10)->Day$date
strftime(Day$date,format = "%V")->Day$Week
Day$Week<-as.factor(Day$Week)
substr(Day$review_started_timestamp,12,13)->Day$starthour 

merge(Day,Associate.data%>%select("username","Q4.Manager","Project","Level","Q4.Shift","Region"),by.x = "username",all.x = TRUE)->Day
Day[complete.cases(Day),]->Day


output$h1<-renderHighchart({

  hourNA<-Day

The rest of the code is just the data manipulation in the way i want it. But this is how i am trying to read the csv "Day". I have tried the methods mentioned in the comments. But it doesnt seem to work.

1 Answers1

0

It would really help if you posted a full code sample of a working (yet minimal) app, so that we can take your exact code, see what's not working, and apply the correct modifications.

Going just by what I see in your short piece of code, I can 100% see why what you're doing doesn't work: Day is getting assigned to the new CSV file inside the observer, but it does not retain that value outside of the observe scope. This is due to basic scoping rules in R, just like if you define a variable inside one function, another unrelated function cannot access that variable. But this piece of code does get run every 60 seconds (you can confirm this by putting a print statement inside and see that it gets printed every minute, or you can even do something like print the number of rows in the dataset and see that it does indeed read the new dataset every minute).

Without seeing your actual app it's hard for me to tell how Day is initially defined and how it should be used, but there are three common ways people achieve what you're trying to do here:

  1. It looks like the entire reason for this observer is only to read the file and assign it to a variable, so I would wager that using a reactive() is more appropriate. This means something like this:

    Day <- reactive({
      invalidateLater(60000, session)
      read.csv("C:/Users/user1/Desktop/dashboardstuff/querycode.csv")
    })
    

    And then when you want to use this variable, call Day()

  2. Similarly, you can use a reactiveVal or reactiveValues instead of a reactive() function. I won't go over the difference between using reactive() and reactiveValues (that's out of scope and a whole separate topic, here's a question+answer for that), but here is what that would look like as an example:

    rv <- reactiveValues()
    observe({
      invalidateLater(60000, session)
      rv$Day <- read.csv("C:/Users/user1/Desktop/dashboardstuff/querycode.csv")
    })
    

    And use rv$Day to access this variable

  3. Since this is a common issue a lot of people do, there is a special function for it: reactiveFileReader(). You can use it like this:

    Day <- reactiveFileReader(60000, session, "C:/Users/user1/Desktop/dashboardstuff/querycode.csv", read.csv)
    

    And use Day() to access it

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • Firstly, thank you for the piece of info and i have added a bit of info about the code and the logic i am trying to implement. @Dean Attali – Pradeep Chintapalli Jan 13 '19 at 19:08
  • I have also replace Day with Day() , but to no avail. The data isnt getting refreshed automatically. – Pradeep Chintapalli Jan 13 '19 at 19:34
  • Your sample code is long and hard to parse through, please post only relevant code (hence I mentioned **minimal**). Try to post the smallest possible piece of code that fully runs and shows your problem. Looking at your post now, after the edit, it still looks like you're trying to assign `Day` inside an observer, which as I said in my post, will not work. You need to follow one of the three solutions I proposed. – DeanAttali Jan 14 '19 at 00:43