Best method of storing/retreiving dataframe from dccStore?

I’m toying with converting a python dash app into R and kind of stuck on the what the best/fastest method of storing and retreiving a dataframe in a dccStore? The method used in the documentation hardcodes the column names and I don’t want to do that.

Hi @aboyher

Which example in the documentation are you referring to?

The example of “share data between callbacks” on this page: Dash for R User Guide and Documentation | R & RStats | Plotly shows how to store and retrieve a dataframe from dccStore. (no hard coding of column names)

That’s the doc I was referring to. The second example hardcodes in order to turn it back into a dataframe.

  output = list(id="memory-graph", property = 'figure'),
  params = list(input(id = "memory-output", property = 'data'),
                input(id = "memory-field", property = 'value')),
  function(data, field){
    data = data.frame(matrix(unlist(data), nrow=length(data), byrow=T))
    colnames(data)[1:ncol(data)] = c('country', 'year','pop','continent','lifeExp', 'gdpPercap')
    if(is.null(data) == TRUE){
    aggregation = list()
    data <- split(data, f = data$country)
    for (row in 1:length(data)) {
      aggregation[[row]] <- list(
        x = unlist(data[[row]][[field]]),
        y = unlist(data[[row]]['year']),
        text = data[[row]]['country'],
        mode = 'lines+markers',
        name = as.character(unique(data[[row]]['country'])$country)

      'data' = aggregation))

I answered my own question. Use toJSON and fromJSON from the jsonlite library instead of df_to_list().

1 Like