ramnathv / htmlwidgets

HTML Widgets for R
http://htmlwidgets.org
Other
790 stars 207 forks source link

Sum of value showing NaN in summarywidget #486

Open lyamlim97 opened 6 months ago

lyamlim97 commented 6 months ago

Using a crosstalk::SharedData dataframe, the result after calling

r htmlWidget = htmlwidgets::createWidget( name = 'summarywidget', x, width = width, height = height, package = 'summarywidget', elementId = elementId, dependencies = crosstalk::crosstalkLibs() ), the generated HTML shows NaN.

CC: @Thepan97

gadenbuie commented 6 months ago

Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for us to recreate your problem so that we can fix it: please help us help you! If you've never heard of a reprex before, start by reading about the reprex package or the guidance from the Shiny team on creating a reproducible example.

lyamlim97 commented 6 months ago

sharedData <- function(df) {
  SharedData$new(df)
}

df <- read.csv("sample.csv")

df_shared <- sharedData(df)

key <- df_shared$key()
group <- df_shared$groupName()
data <- df_shared$origData()

selection = ~favourableTop10 == "Unfavourable Top 10"
selection = eval(selection[[2]], data, environment(selection))

data = data[selection, ]
key = key[selection]

column = 'Revenue'
data = data[[column]]

statistic <- 'sum'

digits = 2

x = list(
  data = data,
  settings = list(
    statistic = statistic,
    digits = digits,
    crosstalk_key = key,
    crosstalk_group = group
  )
)

width = NULL
height = NULL
elementId = NULL

htmlwidgets::createWidget(
  name = 'summarywidget',
  x,
  width = width,
  height = height,
  package = 'summarywidget',
  elementId = elementId,
  dependencies = crosstalk::crosstalkLibs()
)

Above code uses sample.csv file consisting of data that causes NaN issue. Filtering by favourableTop10 == "Unfavourable Top 10" causes the NaN but favourableTop10 == "Favourable Top 10" works.

I have also attached screenshots of the RStudio Viewer output for both filter cases. Unfavourable Top 10 Favourable Top 10