vincentarelbundock / tinytable

Simple and Customizable Tables in `R`
https://vincentarelbundock.github.io/tinytable
GNU General Public License v3.0
196 stars 16 forks source link

Table previews don't appear in Positron #298

Closed andrewheiss closed 2 months ago

andrewheiss commented 2 months ago

The first Positron-related issue!

In Positron (but not VS Code, weirdly), {tinytable} tables don't appear in the preview window and instead cause an error:

Error:
! Function viewer not found in RStudio
Hide Traceback
    ▆
 1. ├─methods (local) `<stndrdGn>`(`<tinytabl[,5]>`)
 2. └─tinytable (local) `<stndrdGn>`(`<tinytabl[,5]>`)
 3.   └─tinytable:::print.tinytable(...)
 4.     └─rstudioapi::viewer(htmlFile)
 5.       └─rstudioapi::callFun("viewer", url, height = height)

Here's a reprex:

This works

```{r}
library(gt)

mtcars[1:5, 1:5] |>
  gt()

This doesn't :(

library(tinytable)

mtcars[1:5, 1:5] |>
  tt()


<img width="1242" alt="image" src="https://github.com/vincentarelbundock/tinytable/assets/73663/1e0488d1-4f7e-41de-91b2-6e3ea8aeb38e">

&nbsp;

It seems like `print.tinytable()` is trying to use `rstudioapi::viewer()` inside Positron? In VS Code, `print.tinytable()` doesn't try to use {rstudioapi} and instead shows the table in the Viewer tab, as expected:

<img width="1102" alt="image" src="https://github.com/vincentarelbundock/tinytable/assets/73663/e8e2fcfd-3ab6-42d6-a059-9d90c633421b">
vincentarelbundock commented 2 months ago

Interesting. There seem to be two issues.

rstudioapi incorrectly detects RStudio when running in Positron

I believe this is a straightforward bug, and I have reported it upstream here: https://github.com/rstudio/rstudioapi/issues/302

Until this gets fixed, I implemented error catching when trying to opening the RStudio viewer in a different IDE. If this fails, print.tinytable() will keep going and launch a browser.

In VSCode, launching this kind of browser usually embeds the HTML in a dedicated pane inside the IDE. (Assuming you have some kind of local server extension installed.)

In Positron, this seems to launch a separate browser window (on my machine).

Equivalent of rstudioapi::viewer() is not implemented for Positron.

This is a known issue, and I hope the Positron team will offer this feature, because it feels quite important to me. Here's the ticket:

https://github.com/posit-dev/positron/issues/2559

What does gt do?

I have no idea. Would be very interested in learning about their solution!

kylebutts commented 2 months ago

@vincentarelbundock Both VSCode and Positron expose options("viewer")$viewer which accepts a url for html. In fact, gt uses htmltools::html_print which uses viewer = getOption("viewer", utils::browseURL)

Edit: RStudio has this too: https://github.com/rstudio/rstudio/blob/d2713874570f06dccc92a46d15e5939922817c7b/src/cpp/r/R/Options.R#L29

vincentarelbundock commented 2 months ago

Thanks for the suggestion @kylebutts . I'll look into that when I find some time.

Note: Posit recommends rstudioapi::hasFun("viewer")

https://github.com/rstudio/rstudioapi/issues/302#issuecomment-2211418452

vincentarelbundock commented 2 months ago

@andrewheiss , FYI, @kylebutts is a superstar and just fixed this for us. His PR is merged and tiny tables appear as expected in the Viewer Pane in my local Positron.

Yay!