Cloud Pak for Data

 View Only
Expand all | Collapse all

Rshiny Force Data Reload/Disconnect Users

  • 1.  Rshiny Force Data Reload/Disconnect Users

    Posted Tue January 17, 2023 07:57 AM
    Background: My team currently has an Rshiny dashboard deployed in Cloud Pak, and data is read into the dashboard from a storage volume outside of the server function (at the beginning of the app.R file). Various functions (outside and inside the server function) use this data, and the data is updated daily in associated Cloud Pak storage volumes.

    Issue: Occasionally (and this seems to happen when someone has the dashboard open while the data is being refreshed), the underlying data is updated, although the dashboard does not pull in the new data.

    Question: Is there a way in Cloud Pak to forcibly remove all users from a deployed Rshiny dashboard at a specific time (around when underlying data is being updated)?

    Workaround: Redeploying the dashboard or performing the "replace asset" function in CPD with a different asset and switching back to the current one forces the data to be refreshed.

    Other potential solutions: I have looked into reactiveFileReader (which checks a file's last modified timestamp at a specified interval and reloads that file if a more recent version is available), although reactive expressions have to be embedded in the server piece of the application (which, to my understanding, only applies to a specific session). The current dashboard is coded to load in data outside of the server code block (so that it is loaded in once for all user sessions of the application), so reactiveFileReader may not be a solution in this case. In order to move towards session-level data, a large portion of the dashboard would need to be reworked (since it was developed based on the assumption that these datasets would be available at the application-level).

    ------------------------------
    Sam Jasper
    ------------------------------

    #CloudPakforDataGroup


  • 2.  RE: Rshiny Force Data Reload/Disconnect Users

    Posted Thu March 09, 2023 02:28 PM

    Solution:

    1. Load data in once at the beginning of the file (outside of the server and UI pieces), so that any functions that use this data can run successfully (and so data can be shared across user sessions).

    2. Within the server code, assign the reactiveFileReader function to a variable. Next, embed that new variable within an observe event assigning the result to the same data frame that was initialized originally (specifically using the <<- operator to update data at the cross-session scope). For more information on scopes in Rshiny, see Shiny - Scoping rules for Shiny apps (rstudio.com).

    Example:

    library(arrow)
    my_data <- read_parquet('my_data.parquet')
    
    ui <- shinyUI(
      <Insert UI Code here>
    )
    
    server <- function(input, output, session) {
       load_my_data <- reactiveFileReader(intervalMillis = 10000, session = NULL, filePath = "my_data.parquet", readFunc = read_parquet)
    
        observe({
           my_data<<- load_my_data()
         })
    
       <Insert more server code here>
    }

    Additional Details: The above solution combines two different concepts that  are worth discussing in more detail:

    1. reactiveFileReader- Given a file path and read function, this function returns a reactive data source for the contents of the file (by checking a file's last modified time). When a file has been updated, its last modified timestamp changes, and this function reloads the file.
      1. Note that the session parameter is set to NULL so that all sessions share the same reader
      2. This function is stored to a variable to be called in an observe event, so that Rshiny can monitor it (at the specified interval) for updates
    2. The <<- operator updates a variable at a scope one level higher than the current scope. In this example, the variable "my_data" will be updated at the cross-session scope (rather than just within the individual user's session).

    In Practice:

    When the dashboard is initially loaded, data is loaded in twice (once for all sessions using the load function before the UI piece, and once for an individual session from the load function in the server code). When underlying data is updated, data is pulled in automatically (based on the poll frequency and load time) and shared across sessions (no longer requiring for the dashboard to be redeployed). Refreshing one's browser may be required to pull in the new data.



    ------------------------------
    Sam Jasper
    ------------------------------