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

Knitting with the Button in the IDE renders HTML even with method != 'html' #562

Closed
cderv opened this issue Jan 7, 2021 · 11 comments
Closed
Labels
bug an unexpected problem or unintended behavior

Comments

@cderv
Copy link
Collaborator

cderv commented Jan 7, 2021

It seems there is something we don't expect in RStudio IDE when using the knit button with blogdown.

blogdown offer different rendering method like

  • method = "custom"
  • method = "markdown"

However, even with this method, a HTML is produced when it should not be only when using the knit button

This steps should help reproduce

  • Open a new example site to no break anything.
  • Use options(blogdown.method = "custom") in .Rprofile. This should tell blogdown to let the build.R script do the rendering.
  • Restart and check blogdown:::build_method() is "custom"
  • leave build.R empty - as do nothing.
  • blogdown::build_site(build_rmd = TRUE) will correctly do nothing
  • Use the Knit button on a Rmd post => The HTML file will be rendered

It is the same with method = "markdown"

I was under the impression that the Knit button in the IDE would run rmarkdown::render_site(<absolut_input_path>) but

  • running rmarkdown::render_site(<absolut_input_path>) in the R console will correctly output a .md file
  • Clicking on the knit button will run the same function, but in the Rmarkdown pane and will output .html

This happens only with .Rmd to .md - .Rmarkdown are correctly rendered to .markdown.

It is as if the IDE is modifying the behavior somehow. IDE bug ?

Discovered in #530

@cderv cderv added the bug an unexpected problem or unintended behavior label Jan 7, 2021
@cderv
Copy link
Collaborator Author

cderv commented Jan 7, 2021

@yihui continuing the discussion here.

I tested with blogdown 0.21 and this also happens with method = "custom".

  • using command line does not produce HTML
  • using Knit button does produce HTML

@yihui
Copy link
Member

yihui commented Jan 7, 2021

I've figured out the reason, but don't know what the proper solution should be. When you knit a single Rmd with the Knit button, RStudio starts up the R session in the directory of the Rmd file, instead of the project root. The .Rprofile lives in the root, so it's ignored when RStudio starts R from the directory of the Rmd.

@cderv
Copy link
Collaborator Author

cderv commented Jan 7, 2021

I tried that to verified that by adding a chunk with blogdown:::build_method() and I thought it picked it right... I may have not correctly tested that.

@yihui
Copy link
Member

yihui commented Jan 7, 2021

It only works with blogdown.knit.on_save = TRUE (because in this case, R is started from the root dir). When you click the Knit button, R won't be aware of any settings in .Rprofile, so blogdown.method will be NULL.

@yihui yihui closed this as completed in 94d84f9 Jan 7, 2021
@yihui
Copy link
Member

yihui commented Jan 7, 2021

I just pushed a fix to execute .Rprofile when users click the Knit button. I don't really like this hack (e.g., .Renviron is also ignored), and would rather have the IDE start R from the project root instead, but I don't know how difficult this change in IDE would be. Besides, even if it is fixed in the IDE, users with older versions of IDE will still have this problem, so my hack has to live in this package anyway for a couple of years.

@cderv
Copy link
Collaborator Author

cderv commented Jan 7, 2021

I am not sure to follow completely. What I tried is adding in the Rmd file

```{r}
blogdown:::build_method()
```

Having this in .Rprofile

options(
  # to automatically serve the site on RStudio startup, set this option to TRUE
  blogdown.serve_site.startup = FALSE,
  # to disable knitting Rmd files on save, set this option to FALSE
  blogdown.knit.on_save = FALSE,
  # build .Rmd to .html (via Pandoc); to build to Markdown, set this option to 'markdown'
  blogdown.method = 'custom'
)

And got

blogdown:::build_method()
## [1] "custom"

in the HTML produced by the knit button

So I concluded (wrongly it seems) that the method was correctly seen.

With

> packageVersion("blogdown")
[1] ‘0.21.81

Would having a configuration file instead of a .Rprofile a solution that would prevent this ?

I'll try to come up with a reproducible example to add in the list of issue for RStudio IDE.

@cderv
Copy link
Collaborator Author

cderv commented Jan 7, 2021

And same thing with custom made option in .Rprofile.

In fact, I guess this is because we run render in its own process using Rscript_calland this take care correctly of the .Rprofile.

Thanks for the quick fix ! One more hack we'll need to maintain then !

@yihui
Copy link
Member

yihui commented Jan 7, 2021

Oh you are right. It was because Rscript_call() launches a new R session:

x = xfun::Rscript_call(

and the new R session is started from the root directory of the project.

@cderv
Copy link
Collaborator Author

cderv commented Jan 7, 2021

About the importance of creating good test before coming to a conclusion 😅
I conclude wrong because my test was... not good - Learning from experience is good!

@zenggyu
Copy link

zenggyu commented Sep 15, 2021

I recently ran into a problem described in this post, and then I found this issue. I guess perhaps this issue is related to the problem but as a novice, I don't quite follow your discussions, so I still can't figure out the solution. I'd really appreciate if you can help! 😃

@zenggyu
Copy link

zenggyu commented Sep 27, 2021

Sorry to bother but @cderv would you mind giving some advice? I posted my question on rstudio community and stackoverflow but haven't received any reply so far. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants