posit-dev / positron

Positron, a next-generation data science IDE
Other
2.21k stars 66 forks source link

"highcharter" charts are not displayed in notebook with R interpreter #3969

Closed vinaychuri closed 2 weeks ago

vinaychuri commented 1 month ago

-Positron Version: 2024.07.0 (Universal) build 21 -MacOS Ventura 13.6.7 -R 4.4.1

library(highcharter)
hchart(mtcars, "scatter", hcaes(wt, mpg, z = drat, color = hp)) 

No chart is displayed inline in the notebook and the following message is displayed. Error rendering output item using 'positron.r.htmlwidget' "[object Object]" is not valid JSON

vinaychuri commented 1 month ago

The same error when using plotly library

library(plotly)
plot_ly(economics, x = ~date, y = ~pop,type="scatter",mode="markers")
seeM commented 1 month ago

Thanks for the report! I can also repro the "[object Object]" is not valid JSON error in Python notebooks with IPyWidgets.

Note that highcharter widgets don't seem to work in console sessions at the moment so that'll need more work.

However, plotly widgets do work in the console, so we should be able fix them for notebooks too.

Proposed solutions

There are three parts to fixing this:

  1. We should return a JSON notebook cell output item for known widget mimetypes here: https://github.com/posit-dev/positron/blob/9979b182c632ad89904c48bb6b440eba6112a6c6/extensions/positron-notebook-controllers/src/notebookController.ts#L321-L325
  2. We're failing to load the required resources in our htmlwidgets notebook renderer inside notebooks (it works in consoles). We currently set the resource roots via the output message before creating our webview here: https://github.com/posit-dev/positron/blob/9979b182c632ad89904c48bb6b440eba6112a6c6/src/vs/workbench/contrib/positronOutputWebview/browser/notebookOutputWebviewServiceImpl.ts#L160-L165

    But that doesn't affect notebooks. We need to figure out a way to get those resource roots for the notebook back layer webview here: https://github.com/posit-dev/positron/blob/9979b182c632ad89904c48bb6b440eba6112a6c6/src/vs/workbench/contrib/notebook/browser/view/renderers/backLayerWebView.ts#L1207-L1221

    This will at least fix plotly.

  3. Highcharter doesn't work in the console either. There are a bunch of type errors in the console logs. I'm not sure what the issue is, but looks like something isn't being loaded properly.
seeM commented 1 month ago

Some discussion about these highcharter errors in: https://github.com/posit-dev/positron/issues/1742.

testlabauto commented 2 weeks ago

Verified Fixed

Positron Version(s) : 2024.07.0-107
OS Version          : OSX

Test scenario(s)

library(highcharter) hchart(mtcars, "scatter", hcaes(wt, mpg, z = drat, color = hp))

Link(s) to TestRail test cases run or created: N/A

vinaychuri commented 2 weeks ago

Is this released or still under testing? I tried the same on Positron Version: 2024.07.0 (Universal) build 125 and still no output in Notebook

vinaychuri commented 2 weeks ago

I use the default Dark theme in Positron Version: 2024.07.0 (Universal) build 125 and in R Notebook execute the following code. There is no output but the background of the entire notebook changes!

https://github.com/user-attachments/assets/364b1da8-dc1c-4bfa-b588-a7fae40e9129

library(highcharter)
hchart(mtcars, "scatter", hcaes(wt, mpg, z = drat, color = hp))
testlabauto commented 2 weeks ago

Hi @vinaychuri,

Thanks for pointing this out! I am able to reproduce what you are seeing.

I verified this with the 107 release but it no longer appears to work. I will reopen.

testlabauto commented 2 weeks ago

@seeM In light mode I am seeing this for the cell output (build 125):

Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
seeM commented 2 weeks ago

I don't believe any R HTML Widgets are currently supported in ipynb notebooks. I've created https://github.com/posit-dev/positron/issues/4219 to track that. They should work via the console though.