Skip to content

Issue with knitting R Markdown documents #2344

Closed

Description

@bhattmaulik

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

Activity

  1. cderv commented on Apr 7, 2022

    @cderv
    Collaborator

    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 ?

  2. bhattmaulik commented on Apr 7, 2022

    @bhattmaulik
    Author

    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.

  3. cderv commented on Apr 7, 2022

    @cderv
    Collaborator

    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 !

  4. bhattmaulik commented on Apr 8, 2022

    @bhattmaulik
    Author

    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.

  5. bhattmaulik commented on Apr 8, 2022

    @bhattmaulik
    Author

    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?

  6. yihui commented on Apr 8, 2022

    @yihui
    Collaborator

    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.

  7. bhattmaulik commented on Apr 8, 2022

    @bhattmaulik
    Author

    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.

  8. yihui commented on Apr 8, 2022

    @yihui
    Collaborator

    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?

  9. bhattmaulik commented on Apr 8, 2022

    @bhattmaulik
    Author

    The Project Build Tools: (None)

  10. yihui commented on Apr 8, 2022

    @yihui
    Collaborator

    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.

  11. bhattmaulik commented on Apr 8, 2022

    @bhattmaulik
    Author

    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.

  12. bhattmaulik commented on Apr 8, 2022

    @bhattmaulik
    Author

    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.

  13. cderv commented on Apr 8, 2022

    @cderv
    Collaborator

    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.

  14. bhattmaulik commented on Apr 8, 2022

    @bhattmaulik
    Author

    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

  15. cderv commented on Apr 8, 2022

    @cderv
    Collaborator

    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 ?

  16. 4 remaining items

  17. moved this from Backlog to Todo in R Markdown Team Projectson Apr 8, 2022
  18. self-assigned this
    on Apr 8, 2022
  19. moved this from Todo to No status in R Markdown Team Projectson Apr 8, 2022
  20. moved this to In Progress in R Markdown Team Projectson Apr 8, 2022
  21. cderv commented on Apr 8, 2022

    @cderv
    Collaborator

    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")
  22. removed their assignment
    on Apr 8, 2022
  23. Repository owner moved this from In Progress to Done in R Markdown Team Projectson Apr 8, 2022
  24. cderv commented on Apr 8, 2022

    @cderv
    Collaborator

    @bhattmaulik this is now in dev version of rmarkdown

    remotes::install_github("rstudio/rmarkdown")

    Thanks!

  25. bhattmaulik commented on Apr 8, 2022

    @bhattmaulik
    Author

    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!

  26. github-actions commented on Oct 11, 2022

    @github-actions

    This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

  27. locked as resolved and limited conversation to collaborators on Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions