r-for-educators / flair

decorate your R code
https://r-for-educators.github.io/flair/index.html
Other
211 stars 21 forks source link

R Markdown files will not knit in RStudio 1.3+ #13

Closed andrewheiss closed 4 years ago

andrewheiss commented 4 years ago

In RStudio Preview (v1.3.957), using R 4.0.0, knitting a document with flair() in it causes an error:

Error in viewer(htmlFile) : could not find function "viewer"
Calls: <Anonymous> ... knit_print.default -> normal_print -> print -> print.with_flair
Execution halted

This only happens when using RStudio. Running rmarkdown::render() from the terminal works fine, which makes me think it's an issue either internal to RStudio (perhaps they changed something with rstudioapi::getSourceEditorContext() and that is then preventing the viewer <- getOption('viewer') line in the print.with_flair() function definition? I have no idea 🤣)

A workaround is to manually include knit_print.with_flair() at the end of the decorate() %>% flair() pipeline to force knitr to not try to use RStudio's internal viewer panel.

Here's a reproducible example:

This causes the error (only in RStudio; this knits fine from outside RStudio):

---
title: "Testing"
output: html_document
---

```{r include=FALSE}
library(flair)
library(dplyr)
iris %>%
  group_by(Species) %>%
  summarize(mean(Sepal.Length))
decorate("how_to_pipe", eval = FALSE) %>% 
  flair("%>%")

This fixes the error:

title: "Testing" output: html_document

library(flair)
library(dplyr)
iris %>%
  group_by(Species) %>%
  summarize(mean(Sepal.Length))
decorate("how_to_pipe", eval = FALSE) %>% 
  flair("%>%") %>% 
  knit_print.with_flair()
kbodwin commented 4 years ago

I was able to partially duplicate this error: Running it in R 4.0.0 and RStudio 1.2.5033, I don't get an error, but I also don't get any output without adding the knit_print.with_flair line.

In R 3.6, it seems to work fine still.

I'll make a point to try to track down what changed in R 4.0, and possibly also RStudio 1.3., when I have time.

Thanks for calling this to my attention!

cathblatter commented 3 years ago

@kbodwin - thanks for this great package!

I encounter the same issue as described above with: R version 4.1.0 and RStudio Desktop 1.4.1717 - knitting fails with the same Error-message as above and rmarkdown::render() produces an empty slide.

The workaround with adding knit_print.with_flair() still works though 👍

BEAST-Community commented 6 months ago

Error in viewer(): ! could not find function "viewer" Execution halted

BEAST-Community commented 6 months ago

how should I do ,thanks.