Skip to content

Conversation

@suhaani-agarwal
Copy link
Contributor

fixing #219

@suhaani-agarwal
Copy link
Contributor Author

I investigated into this issue, and saw that there are two files - geom.r and z-animint-helpers.r
the write.table function is called with quote=FALSE:

 write.table(some.lines, tmp,
                    col.names=FALSE,
                    quote=FALSE, row.names=FALSE, sep="\t")

With quote=FALSE, any string written to the TSV is unquoted. This becomes a problem because the JS side uses d3.tsv to read per-chunk TSVs. If the TSV contains a newline inside a field, it gets split into multiple lines. That breaks the tabular layout, and D3 mis-parses the rows. As a result, points can disappear
Thats why the geom_point was not being rendered when aes(tooltip = "two\nlines") was given with aes(color)

an example is :

viz<-list(
  p1=ggplot()+
    geom_point(aes(
      x, x, tooltip=x, color=x),
      size=5,
      data.frame(x=c("one line", "two\nlines", "three\nlines\nhere")))
)

the generated tsv for this is as follows :
image

Here the "two\nlines" point is split across rows, which causes it to disappear.

When I changed those write.table calls to use quote=TRUE, the TSV instead looks like this:

image

This way, the newline stays inside the quoted string, and D3 reads the whole field correctly. The "two\nlines" point then renders properly, with the tooltip showing \n as a space as follows:

Screen.Recording.2025-09-01.at.7.26.43.PM.mov

@suhaani-agarwal
Copy link
Contributor Author

Now the newline survives serialization, but the browser’s default rendering (and/or the way the tooltip is inserted into the DOM) collapses it into a single space. This happens not because the code explicitly replaces \n with a space, but because HTML collapses newlines by default.

We have a few options:

  • Keep quote=TRUE (as I have now) and accept that \n renders as a space in tooltips.
  • Keep quote=TRUE and implement multi-line tooltips by converting \n to <br> for proper HTML rendering.
  • Revert to quote=FALSE and remove or warn about \n in tooltips like ('aes(tooltip) does not support line breaks; please remove \n from aes(tooltip)') on the R side to avoid TSV corruption.

Could you advise which approach we should go with?

@tdhock
Copy link
Collaborator

tdhock commented Sep 2, 2025

Thanks for investigating!
rendering with a space instead of a newline is preferable to not rendering anything at all.
but even in that case, it would be good to add a warning("newline detected in geom_text label, which will be converted to a space; please use multiple text elements at different y positions instead") or similar.
Can you please try changing all three fwrite/write.table calls to fwrite()? The default quote="auto" should work (only adding quotes where they are necessary).

@tdhock
Copy link
Collaborator

tdhock commented Sep 2, 2025

You mentioned that converting \n to <br/> may be a possibility... if that works that would be preferable, so can you try that please? (I'm not sure if it would work though)

@codecov
Copy link

codecov bot commented Sep 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.70%. Comparing base (92d6971) to head (05d086e).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #220   +/-   ##
=======================================
  Coverage   77.70%   77.70%           
=======================================
  Files         164      164           
  Lines        8777     8778    +1     
  Branches      553      564   +11     
=======================================
+ Hits         6820     6821    +1     
  Misses       1957     1957           
Flag Coverage Δ
javascript 95.39% <100.00%> (+<0.01%) ⬆️
r 69.61% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@suhaani-agarwal
Copy link
Contributor Author

the tooltips now support \n

for example the following code:

viz<-list(
  p1=ggplot()+
    geom_point(aes(
      x, x, tooltip=x, color=x),
      size=5,
      data.frame(x=c("one line", "two\nlines", "three\nlines\nhere")))
)
animint2dir(viz, "label-aligned")

produces

Screen.Recording.2025-09-03.at.2.05.23.PM.mov

@tdhock
Copy link
Collaborator

tdhock commented Sep 4, 2025

great thanks! please increase version number and add an item to NEWS and then I will merge.

mouseX = d3.event.pageX;
mouseY = d3.event.pageY;
}
var safeHtml = String(content).replace(/\n/g, '<br/>');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we do this on R instead of in JS, I think we shold be able to get line breaks in legend entries too, see test below.
image

maybe also desirable in

  • axes titles
  • axes tick marks
  • facet titles
  • geom_text label

but all of these features are too much for this PR, so I will create a new issue.

@tdhock
Copy link
Collaborator

tdhock commented Sep 6, 2025

great thanks!

@tdhock tdhock merged commit 4475749 into master Sep 6, 2025
5 checks passed
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 this pull request may close these issues.

3 participants