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

Issue with knitting R Markdown documents #2344

Closed
bhattmaulik opened this issue Apr 7, 2022 · 22 comments · Fixed by #2345
Closed

Issue with knitting R Markdown documents #2344

bhattmaulik opened this issue Apr 7, 2022 · 22 comments · Fixed by #2345

Comments

@bhattmaulik
Copy link

@bhattmaulik bhattmaulik commented Apr 7, 2022

I am facing a strange issue in last 2-3 days. I am unable to knit any .Rmd file to any format (HTML/PDF/Word). First, I create an RStudio Project. Within the RStudio Project, from RStudio File Menu>New File > R Markdown, I choose any of the three output formats. I simply save the document without making any changes in the defaults in the Rmd file. Then I press the "knit" button in RStudio. I get the following error while rendering it.

processing file: _main.Rmd
Error in parse_block(g[-1], g[1], params.src, markdown_mode) : 
  Duplicate chunk label 'setup', which has been used for the chunk:
knitr::opts_chunk$set(echo = FALSE)
Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block
Please delete _main.Rmd after you finish debugging the error.
Execution halted

Isn't _main.Rmd supposed to be in bookdown? How can I get this error while knitting a single R Markdown file? Moreover, this _main.Rmd file is saved in a different location - it's the default working directory (while not in a project).
Irrespective of this, if I try to knit it again, I get the error message

Error in render_book(input_file, output_format, envir = envir, preview = TRUE) : 
  The file _main.Rmd exists. Please delete it if it was automatically generated. If you are sure it can be safely overwritten or deleted, please set the option 'delete_merged_file' to true in _bookdown.yml.
Calls: <Anonymous> -> <Anonymous> -> <Anonymous> -> render_book
Execution halted

So, again it confirms that the underlying function is from bookdown instead of R Markdown.
I have updated all the relevant software - downloaded knitr, R Markdown and bookdown from github. I have also installed latest daily build of tinytex (using tinytex::install_prebuilt("TinyTeX"))and RStudio. The session info is as follows;

xfun::session_info("Rmarkdown")
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044), RStudio 2022.6.0.270

Locale:
  LC_COLLATE=English_India.1252 
  LC_CTYPE=English_India.1252   
  LC_MONETARY=English_India.1252
  LC_NUMERIC=C                  
  LC_TIME=English_India.1252    
system code page: 65001

Package version:
  Rmarkdown_2.13.3
@cderv
Copy link
Collaborator

@cderv cderv commented Apr 7, 2022

From all those error, I would say rmarkdown or RStudio IDE is kind of detecting you are in a bookdown project.

Moreover, this _main.Rmd file is saved in a different location - it's the default working directory (while not in a project).

Are you working in a project ? Or are you working in a default working directory with all those files around ?
It is best to work in projects to avoid those type of issues.

Is rmarkdown::render() in console working ok ?

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 7, 2022

I am working in an RStudio Project, not in the default working directory. But despite that, the _main.Rmd file is saved in the default working directory instead of the project's directory.
And as you have asked, I tried rmarkdown::render(input_file = "filename", output_format = "pdf_document"). It is working as expected. I again updated the RStudio daily build from 2022.6.0.270 to 2022.6.0.277. But the problem continues with that version as well.

@cderv
Copy link
Collaborator

@cderv cderv commented Apr 7, 2022

It is working as expected. I again updated the RStudio daily build from 2022.6.0.270 to 2022.6.0.277. But the problem continues with that version as well.

This means it is something related to RStudio IDE. what is the function called when you are clicking the knit / render button ?
Should be written in the build or rmarkown pane. thanks !

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

When I press the knit button, the function being called is rmarkdown::render_site. This is what I get in the "render" tab

==> rmarkdown::render_site('path/to/Rmd/file',  encoding = 'UTF-8');
Error in render_book(input_file, output_format, envir = envir, preview = TRUE) : 
  The file _main.Rmd exists. Please delete it if it was automatically generated. If you are sure it can be safely overwritten or deleted, please set the option 'delete_merged_file' to true in _bookdown.yml.
Calls: <Anonymous> -> <Anonymous> -> <Anonymous> -> render_book
Execution halted

After your message, I uninstalled the RStudio IDE, and reinstalled it again. But still the problem continues.

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

So, as a workaround, I added one more line in the YAML header of the .Rmd file

---
knit: rmarkdown::render
---

After this, I could compile it properly. But it also means that I will have to add this line to all the R Markdown documents now onwards, unless and until the behavior of the Knit button changes. I will have to add this line even to the old R markdown documents that I have prepared earlier. How can I change the default behavior of the Knit button now?

@yihui
Copy link
Member

@yihui yihui commented Apr 8, 2022

Could you share the whole project? You can compress it into a zip archive and upload here. It's hard to tell what's wrong without a reproducible example.

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

TeXLive.zip
Please check this file. I am just trying to arrange all the tinytex packages in descending order of their sizes. I have installed these packages using tinytex:::install_prebuilt("TinyTeX). I have not added the extra line in YAML header yet (knit: rmarkdown::render). So, here in my computer, I am still getting that same error while knitting the document. This happens irrespective of the output format I choose - be it HTML/Word/PDF.
Thank you for your continuous support.
Regards,
Maulik.

@yihui
Copy link
Member

@yihui yihui commented Apr 8, 2022

That's really odd. I can't reproduce the problem, and can't think of a reason why RStudio would treat any of your Rmd file as a bookdown project. What is your project option Tools -> Project Options -> Build Tools?

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

The Project Build Tools: (None)

@yihui
Copy link
Member

@yihui yihui commented Apr 8, 2022

That's correct. Then I really don't know how your RStudio ended up in that weird state. Do you have anything suspicious in your ~/.Rprofile file? Or do you have a ~/.RData file?

Perhaps the last suggestion that I could give is reset RStudio: https://support.rstudio.com/hc/en-us/articles/200534577-Resetting-RStudio-Desktop-s-State If that doesn't work, I'll be out of ideas.

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

I don't save .RData file. And I haven't created .Rprofile file either.
I have finally put this question on RStudio Community as to how to change the default behavior of knit button in RStudio. If I find a solution there, I would intimate here, and close the issue. Thanks a lot for your support.

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

On further experiments, when I uninstalled bookdown package, and then tried knitting the document through "Knit" button, it knits properly, but I get an error:

Error in loadNamespace(x) : there is no package called ‘bookdown’

At this time, I can see in the "Render" tab that the knit button is taking rmarkdown::render function to knit the document. I can knit in all the three formats (word, html and pdf).
But when I re-install the bookdown package, the knit button again starts taking rmarkdown::render_site function to knit the document. That means, the behavior of "knit" button is different when bookdown package is present.

@cderv
Copy link
Collaborator

@cderv cderv commented Apr 8, 2022

Can you run

rmarkdown::site_generator("path/to/file")

rmarkdown::site_generator(".")

I believe this is what RStudio IDE is using to decide if it is a website project.

Basically, it would been seen as a bookdown project if you have a index.Rmd file with a YAML field site: bookdown::bookdown_site like in a bookdown project.

We recently had issues with our internal proj_root() function and tweak it. Maybe there is still issue with it.

  • Please install the last dev version
  • And run this command
rmarkdown:::proj_root("path/to/file", "^index.R?md$", "^\\s*site:.*::.*$")

I wonder if what happens is that your current project is located in a subfolder somehow of another folder/project that would have and index.Rmd with site key.

Thank you.

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

You are probably right. I don't fully understand the output of these functions you have suggested, but I have still followed your suggestion. I am attaching the results here.
debug.pdf

@cderv
Copy link
Collaborator

@cderv cderv commented Apr 8, 2022

Yes this is it. The results you shared shows that a book project website is detected, and that is why the IDE is behaving as in a book project. It seems it comes from E:/Maulik/Projects folder seems to have a index.Rmd file with site: bookdown::bookdown_site project.

Can you check that ? If that is correct, is that index.Rmd there for a reason ?

That being said, I don't think we have a correct behavior here.

We should not probably check for website project that far up the current folder. I am thinking we should stop at the root of the RStudio project (when .Rproj is found in the current root being checked).

@yihui does it feel off to you too ?

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

Yes, there is an index.Rmd file in that folder. I deleted the _main.Rmd from E:/Maulik?Projects, and moved the index.Rmd file (along with its other associated files) to a new folder. After that, I restarted RStudio. Now the Knit button works properly.

@cderv
Copy link
Collaborator

@cderv cderv commented Apr 8, 2022

So that was the culprit ! Glad we found it!

I'll leave this issue open until we decide if this needs to be improved (which I do) and how.

Thanks a lot for the report!

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

Thanks for your continuous support. Let me know if you decide to improve it. I would like to participate in its testing.
This root folder (E/Maulik/Projects) was created when I started learning R 5-6 years ago. After more working in R, I realized that RStudio Project + R markdown is a better way of working in R. So, now I follow that structure. But still, the old files had remained in their original place, which caused the trouble today. Thanks a lot for guiding me through it.

@yihui
Copy link
Member

@yihui yihui commented Apr 8, 2022

I am thinking we should stop at the root of the RStudio project (when .Rproj is found in the current root being checked).

@cderv That makes sense to me.

@cderv
Copy link
Collaborator

@cderv cderv commented Apr 8, 2022

Let me know if you decide to improve it. I would like to participate in its testing.

@bhattmaulik if would help if you can test #2345 ?

remotes::install_github("rstudio/rmarkdown#2345")

@cderv cderv removed their assignment Apr 8, 2022
cderv added a commit that referenced this issue Apr 8, 2022
Previously, it was looking in all the folders until main root of the tree. This was causing issue in RStudio IDE where a website project was detected incorrectly for a non-site project  included into a subfolder of a site project. See #2344 for context.
@cderv
Copy link
Collaborator

@cderv cderv commented Apr 8, 2022

@bhattmaulik this is now in dev version of rmarkdown

remotes::install_github("rstudio/rmarkdown")

Thanks!

@bhattmaulik
Copy link
Author

@bhattmaulik bhattmaulik commented Apr 8, 2022

I just checked it. I had both the files (index.Rmd and _main.Rmd) in the global root directory, and yet the Rmd file inside an RStudio project knitted a pdf file without any trouble. Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants