0

I created a Shiny app. Basically, inside the R code, the data is retrieved from the URL link (so App is using only it), which downloads data in excel format. The data in the URL is refreshed every hour. As a result, ideally, the Shiny app gets every time the latest data.

Question: Is it possible to do in this way that the latest data is depicted on the web page (of Shiny app) every time users 1) open the web page with Shiny app and 2) press Refresh button on the web page with Shiny app?

Data is always refreshing at H+30. I.E. information for the 10 am is published at 11:30 and etc. So, I want kind of refresh button, which helps run the script in the app from the beginning.

The table is following:

library(shiny)
#library(httr)
#library(XML)
library(dplyr)
library(ggplot2)
#library(data.table)
library(formattable)
library(reshape2)
library(ggthemes)
library(shinyjs)
library(V8)



df <- read.csv2(paste("path"))[-2]
names(df) <- c("Region","Date","Imbalance")

df$Date  <- as.POSIXct(df$Date , format="%Y-%m-%d %H")
df$Imbalance<-as.numeric(df$Imbalance)

df<- as.data.frame(na.omit(dcast(df, Date ~ Region, value.var = "Imbalance")))
df<-df %>% arrange(desc(Date))
df[2:5]<-lapply(df[2:5], as.numeric)
df[2:5]<-round(df[2:5],0)



# Define UI for application
ui <- fluidPage(
  
  # Application title
  titlePanel("Imbalance volumes"),
  
  # Sidebar with slider input to select date range
  sidebarLayout(
    sidebarPanel(
      
      
      
      # Add a Slider Input to select date range
      #sliderInput
      dateRangeInput("Date_range_selector", "Select Date Range",
                     start = as.Date(paste(Sys.Date()-2,"00:00:00",sep = " "),"%Y-%m-%d %H"),
                     end = as.Date(paste(Sys.Date(),"00:00:00",sep = " "),"%Y-%m-%d %H"),
                     
                     min = as.Date("2015-01-01 00:00:00","%Y-%m-%d %H"),
                     max = as.Date("2020-12-01 00:00:00","%Y-%m-%d"),
                     #value=c(as.Date(Sys.Date()-14),as.Date(Sys.Date())),
                     #format ="%Y-%m-%d"
      )
    ),
    
    # plot graphs
    mainPanel(tabsetPanel(
      tabPanel("Stat",
               h3(helpText("Imbalance volumes")),
               formattableOutput("formattableexample"))
    )
    )
  ))





server <- function(input, output) {
  

  
  ############Plot Prices and histogram###############
  

  
  test2<-reactive({
    
    
    df %>%
      filter(between(as.Date(Date), input$Date_range_selector[1], input$Date_range_selector[2]))
    
    
    
    
  })
  
  output$formattableexample <- renderFormattable({
    formattable(test2(),
                align =c("l","c","c","c","c", "c", "c", "c", "r"), 
                list("Date" = formatter("span", style = ~ style(color = "grey",font.weight = "bold"))
                ))
  })
  

  
}



shinyApp(ui = ui, server = server)




Epo Luusk
  • 37
  • 5
  • If you're talking about raw data shown in a table, you can just sort the data using a time variable indicating when the data was entered. – Phil Oct 20 '20 at 14:27
  • but, can I pot some button/or smth else?, so that the user can manually refresh the data in the app? – Epo Luusk Oct 20 '20 at 14:35
  • 1
    What have u tried so far? – Pork Chop Oct 20 '20 at 15:08
  • See here: https://stackoverflow.com/questions/30852356/add-a-page-refresh-button-by-using-r-shiny or look up using the `refresh()` function in shinyjs. – Phil Oct 20 '20 at 15:20
  • 1
    U dont need to use `js` as `session` has `reload()` already https://shiny.rstudio.com/reference/shiny/0.14/session.html – Pork Chop Oct 20 '20 at 15:29
  • I also edited the question and added my R code and the needing to the bottom of the question. Something similar to my question I found here, but it did not help me to figure out. https://stackoverflow.com/questions/42889993/restart-shiny-app-from-within-app-reloading-data – Epo Luusk Oct 21 '20 at 09:29
  • I found a solution. There should be added this line in server `observe({ if (input$refreshdata) { session$reload() } }) ` and `server <- function(input, output, session)` – Epo Luusk Oct 21 '20 at 10:44

0 Answers0