dashR icon indicating copy to clipboard operation
dashR copied to clipboard

Bar plot not showing when there is a vector with a length of 1

Open strboul opened this issue 4 years ago • 1 comments

As there is no scalar data type in R, this is probably caused by the direct translation of the JS code to the R code.

A workaround is to add as.list around the vectors to make them look like JS arrays.

Dash example:

library(dash)
library(dashHtmlComponents)
library(dashCoreComponents)

app <- Dash$new()

# globals are fine on local:
cat_data <- aggregate(iris["Petal.Length"], iris["Species"], length)

app$layout(
  htmlDiv(
    list(
      htmlP("Choose a number of category:"),
      dccDropdown(
        id = "category_num",
        options = lapply(seq(nrow(cat_data)), function(x) list(label = x, value = x)),
        value = 1,
        clearable = FALSE,
        searchable = FALSE
      ),
      dccGraph(id = "graph1"),
      dccGraph(id = "graph2")
    )
  )
)

app$callback(
  output = list(id = "graph1", property = "figure"),
  params = list(input(id = "category_num", property = "value")),
  function(category_num) {
    cat_data_subset <- cat_data[seq(1, category_num), ]
    list(
      data = list(
        list(
          type = "bar",
          x = cat_data_subset$Species,
          y = cat_data_subset$Petal.Length
        )
      )
    )
  }
)

app$callback(
  output = list(id = "graph2", property = "figure"),
  params = list(input(id = "category_num", property = "value")),
  function(category_num) {
    cat_data_subset <- cat_data[seq(1, category_num), ]
    list(
      data = list(
        list(
          type = "bar",
          x = as.list(cat_data_subset$Species),
          y = as.list(cat_data_subset$Petal.Length)
        )
      )
    )
  }
)

app$run_server(debug = TRUE)

Thanks a lot for porting Dash to the R. You do a great job there!

strboul avatar Jun 26 '20 07:06 strboul

Thanks @strboul for reporting this issue, for the excellent reproducible example, and for your kind words.

I suspect that you're correct about this being related to the way the resulting JSON is handled when rendering the figure whenever x and y are vectors rather than lists.

I'm inclined to modify the Dash for R docs to suggest wrapping the data within as.list if using the figure syntax as above (users of plot_ly and ggplotly shouldn't run into this issue, at least).

rpkyle avatar Jun 26 '20 13:06 rpkyle