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

[knitr] Duplicated plotly graph - intermediary plot shoud probably be hidden #1468

Open
cderv opened this issue Aug 30, 2023 · 3 comments
Open

Comments

@cderv
Copy link
Contributor

cderv commented Aug 30, 2023

From quarto-dev/quarto-cli#4507 - idea is to try the examples at https://plotly.com/python/sliders/

---
title: "plotly"
output: html_document
---
  
```{python}
#| echo: false
import plotly.graph_objects as go
import numpy as np

# Create figure
fig = go.Figure()

# Add traces, one for each slider step
for step in np.arange(0, 5, 0.1):
	fig.add_trace(
		go.Scatter(
			visible=False,
			line=dict(color="#00CED1", width=6),
			name="𝜈 = " + str(step),
			x=np.arange(0, 10, 0.01),
			y=np.sin(step * np.arange(0, 10, 0.01))))

# Make 10th trace visible
fig.data[10].visible = True

# Create and add slider
steps = []
for i in range(len(fig.data)):
	step = dict(
		method="update",
		args=[{"visible": [False] * len(fig.data)},
			  {"title": "Slider switched to step: " + str(i)}],  # layout attribute
	)
	step["args"][0]["visible"][i] = True  # Toggle i'th trace to "visible"
	steps.append(step)

sliders = [dict(
	active=10,
	currentvalue={"prefix": "Frequency: "},
	pad={"t": 50},
	steps=steps
)]

fig.update_layout(
	sliders=sliders
)

fig.show()

```

But rendering this in R Markdown documents (or quarto), will end up with more plot than expected.

Is this expected and it should somehow be dealt with in code chunk itself ? Or should there be a way to hide intermediary plots by default ?
image

@t-kalinowski
Copy link
Member

Thanks, I can reproduce.

In principle I agree that we should suppress auto-printing of the intermediate figures here, but changing the auto-printing semantics here is going to be a breaking change which we'll have to do thoughtfully.

For now, users can explicitly disable printing of intermediate plots by enabling jupyter_compat=TRUE mode. In this mode, only the very last expression in a chunk gets auto-printed, and only if it doesn't end with a trailing ;.

E.g., updating the chunk like:

```{python}
#| jupyter_compat: true
#| echo: false
import plotly.graph_objects as go
import numpy as np
.... (same as before)

image

@cderv
Copy link
Contributor Author

cderv commented Jan 31, 2024

Thanks ! I did not know about this jupyter_compat mode.

@t-kalinowski
Copy link
Member

You may want to have a peek at ?reticulate::eng_python. We recently updated the help page to comprehensively list all the knitr options we support.

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

No branches or pull requests

2 participants