Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update data in shiny application without refreshing the whole application

I have an application for real time data visualization build with R shiny library. I do periodic data reload from file using reactivePoll function. What I do not like about this is that whenever data reloads the whole application refreshes.

So for example if I have DT table output with selection and I use this selection input$table_rows_selected it resets to NULL whenever data reloads which is not user-friendly at all.

Is it overall possible to change data output without interrupting user?

UPDATE.

Can this be achieved with any other package for displaying tables - googleVis or other?

Working example.

library(shiny)
library(DT)

runApp(shinyApp(
  ui = fluidPage(dataTableOutput('table')),
  server = function(input, output, session) {
    pollData <- reactivePoll(4000, session,
                             checkFunc = function(){ Sys.time() },
                             valueFunc = function(){ data.frame(id = sample(letters[1:3]), a = runif(3), b = runif(3), c = runif(3)) })
    output$table <- renderDataTable({pollData()})
    proxy <- dataTableProxy('table')
    observeEvent(pollData(), {
      selectRows(proxy, input$table_rows_selected)
    })}
))

I have taken this example from @NicE answer and added id column. The point is that @NicE answer is OK if one needs certain row to be selected when that row is identified by the row number.

Now suppose I need a row to be selected when that row is identified by some id value. That is if I select a row with id equal b, then the next time data reloads I want the row to be selected with the same id value.

like image 465
danas.zuokas Avatar asked Mar 10 '16 08:03

danas.zuokas


People also ask

How to store data in Shiny?

The most trivial way to save data from Shiny is to simply save each response as its own file on the current server. To load the data, we simply load all the files in the output directory. In our specific example, we also want to concatenate all of the data files together into one data. frame.

Is shiny easy to learn?

Along with Shiny elements, you can use HTML elements to stylize your content in your application. In my opinion, R Shiny is very easy to learn despite how powerful the tool is.

What are shiny applications?

Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. You can also extend your Shiny apps with CSS themes, htmlwidgets, and JavaScript actions.


1 Answers

You could use a dataTableProxy to select rows when the datable is created after a pollData update. Here's an example, the dataframe is refreshed every 4 seconds:

library(shiny)
library(DT)

ui <- fluidPage(dataTableOutput("table"))

server <- function(input,output,session){
        values <- reactiveValues()
        pollData <- reactivePoll(4000, session,
                                 checkFunc=function(){
                                         Sys.time()
                                 },
                                 valueFunc=function(){                             
                                         data.frame(a=sample(c("a","b","c"),3),b=runif(3),c=runif(3),stringsAsFactors = F)
                                 })

        output$table <- renderDataTable({ pollData()})

        observe({
                values$selected <- pollData()$a[input$table_rows_selected]
        })

        proxy = dataTableProxy('table')
        observeEvent(pollData(),{
                selectRows(proxy, which(pollData()$a %in% values$selected))
        })
}

shinyApp(ui,server)

Update: on the above code, when the data changes, the selected rows are the ones that have the same first column as previously.

like image 176
NicE Avatar answered Sep 23 '22 21:09

NicE