Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download to CSV function does not work if empty table values #434

Open
nick-youngblut opened this issue Dec 26, 2023 · 4 comments · May be fixed by #437
Open

Download to CSV function does not work if empty table values #434

nick-youngblut opened this issue Dec 26, 2023 · 4 comments · May be fixed by #437

Comments

@nick-youngblut
Copy link

From the Customizing section of the docs:

customOpts = list(
      csv = list(
        name = "Download to CSV",
        callback = htmlwidgets::JS(
          "function (key, options) {
            var csv = csvString(this, sep=',', dec='.');

            var link = document.createElement('a');
            link.setAttribute('href', 'data:text/plain;charset=utf-8,' +
              encodeURIComponent(csv));
            link.setAttribute('download', 'data.csv');

            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
          }"
        )
      )
    )

This code does not work if any of the values in the rendered table are empty. The "Download to CSV" option is present in the menu, but clicking on it does nothing. If the user then completely fills all cells in the table with non-empty values, then the user can download the table as a csv.

This repo has not been updated in 2 years, so the docs likely won't be updated, but maybe a user has a suggestion on how to make the "Download to CSV" option more flexible?

@nick-youngblut
Copy link
Author

Maybe the issue is how empty table values are handled in the csvString function:

function csvString(instance, sep, dec) {

  var headers = instance.getColHeader();

  var csv = headers.join(sep) + "\n";

  for (var i = 0; i < instance.countRows(); i++) {
      var row = [];
      for (var h in headers) {
          var col = instance.propToCol(h);
          var value = instance.getDataAtRowProp(i, col);
          if ( !isNaN(value) ) {
            value = value.toString().replace(".", dec);
          }
          row.push(value);
      }

      csv += row.join(sep);
      csv += "\n";
  }

  return csv;
}

@nick-youngblut
Copy link
Author

A simple reprex:

MAT = as.data.frame(matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10],
                                                   letters[1:5])))
MAT$f = NA  

rhandsontable(MAT, width = 550, height = 300) %>%
  hot_context_menu(
    customOpts = list(
      csv = list(name = "Download to CSV",
                 callback = htmlwidgets::JS(
                   "function (key, options) {
                         var csv = csvString(this, sep=',', dec='.');

                         var link = document.createElement('a');
                         link.setAttribute('href', 'data:text/plain;charset=utf-8,' +
                           encodeURIComponent(csv));
                         link.setAttribute('download', 'data.csv');

                         document.body.appendChild(link);
                         link.click();
                         document.body.removeChild(link);
                       }"))))

The reprex is the same code as Export to CSV in the docs, but includes a column that contains all NA values.

@nick-youngblut
Copy link
Author

@jrowen & @DillonHammill I would work on fixing this issue, but I don't want my PR to just sit in the queue forever.

Would you accept a PR, or is this repo essential a public archive?

@nick-youngblut
Copy link
Author

I forked the repo, and the fix is really just one line:

function csvString(instance, sep, dec) {

  var headers = instance.getColHeader();

  var csv = headers.join(sep) + "\n";

  for (var i = 0; i < instance.countRows(); i++) {
      var row = [];
      for (var h in headers) {
          var col = instance.propToCol(h);
          var value = instance.getDataAtRowProp(i, col);
          if (value == null) value = "";  // Handle null or undefined values  // handle
          if ( !isNaN(value) ) {
            value = value.toString().replace(".", dec);
          }
          row.push(value);
      }

      csv += row.join(sep);
      csv += "\n";
  }

  return csv;
}

Specifically: if (value == null) value = "";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant