Skip to content

Document symbol display in Rmd is not reliable #421

Closed

Description

@jooyoungseo

In R Markdown files, sometimes document symbols are not displayed in VSCode's breadcrumbs view.

However, I have no idea what causes this issue. Please see the reprex below.

Reprex

Case 1: Document Symbol Appears Correctly

  1. Download this Rmd file
  2. Open the file in VSCode.
  3. Press Ctrl+Shift+. (period).

Case 2: Document Symbol Does Not Appear

  1. Download another Rmd file
  2. Open the file in VSCode.
  3. Press Ctrl+Shift+. (period).

System Info

Version: 1.55.2 (system setup)
Commit: 3c4e3df9e89829dce27b7b5c24508306b151f30d
Date: 2021-04-13T09:35:57.887Z
Electron: 11.3.0
Chrome: 87.0.4280.141
Node.js: 12.18.3
V8: 8.7.220.31-electron.0
OS: Windows_NT x64 10.0.19042

Activity

  1. renkun-ken commented on Apr 24, 2021

    @renkun-ken
    Member

    For case 2, if you turn on diagnostics, you will notice a syntax error at line 101-102 as the following:

    image

    Although you mark the chunk as eval=FALSE, the code is still parsed. If a syntax error is found, then we can't generate document symbols with it.

    In this case, I suggest that you write as the following so that it won't be regarded as an RMarkdown code chunk but a plain R code block in the markdown document and the code in it won't be parsed.

    ```r
    ggplot(data = <DATA>) + 
      <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
    ```
    
  2. jooyoungseo commented on Apr 24, 2021

    @jooyoungseo
    Author

    Aw, thanks! That makes sense. BTW, my lsp diagnostic has already been turned on like below:

    "r.lsp.diagnostics": true

    However, it does not give me such the error message as your screenshot.

    Would there be any additional settings I need to configure?

    screenshot

  3. jooyoungseo commented on Apr 24, 2021

    @jooyoungseo
    Author

    screenshot2

  4. renkun-ken commented on Apr 24, 2021

    @renkun-ken
    Member

    You may want to update your lintr to latest development version and then you should be able to see the parse error.

  5. jooyoungseo commented on Apr 24, 2021

    @jooyoungseo
    Author

    Thanks! It works now.

  6. randy3k commented on Apr 24, 2021

    @randy3k
    Member

    I guess we might want to handle the case that eval = false or error = TRUE.

  7. jooyoungseo commented on Apr 25, 2021

    @jooyoungseo
    Author

    @randy3k, that would be greatly appreciated. I'll re-open this issue.

  8. jooyoungseo commented on May 8, 2021

    @jooyoungseo
    Author

    I have just found that Math formula-contained code chunk (e.g., bookdown-style Theorem) also generates errors.

    ```{theorem, name="Pythagorean theorem"}
    For a right triangle, if $c$ denotes the length of the hypotenuse
    and $a$ and $b$ denote the lengths of the other two sides, we have
    $$a^2 + b^2 = c^2$$
    ```
    

    Retrieved from https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html

    Shouldn't it be excluded from errors?

  9. renkun-ken commented on May 9, 2021

    @renkun-ken
    Member

    @jooyoungseo Thanks for reporting!

    Currently, we use lintr::lint() to diagnose the whole Rmd document. It handles code chunks internally (https://github.com/jimhester/lintr/blob/master/R/extract.R). If we keep it this way, then we probably need lintr to fix its code chunk detection. Would you like to also raise an issue at https://github.com/jimhester/lintr/issues?

  10. renkun-ken commented on May 9, 2021

    @renkun-ken
    Member

    Looks like there's a notable gap between how knitr and lintr handles rmarkdown code chunks.

    ```{r}
    x <- rnorm(1000)+1
    y <- rnorm(1000)+2
    ```
    
    ```{R}
    x <- rnorm(1000)+1
    y <- rnorm(1000)+2
    ```
    

    In the above example, knitr renders both but lintr only checks the first chunk.

  11. jooyoungseo commented on May 10, 2021

    @jooyoungseo
    Author

    @renkun-ken -- Since I don't use {lintr} separately, I am not sure how I could best elaborate this issue in their repo concisely. Would you mind moving this issue there?

  12. renkun-ken commented on May 10, 2021

    @renkun-ken
    Member

    I've already raised the linked issue above so there's no need for you to file a new one.

  13. jooyoungseo commented on May 10, 2021

    @jooyoungseo
    Author

    @renkun-ken, thank you! Hope that it could be addressed on their side soon.

  14. renkun-ken commented on Oct 6, 2026

    @renkun-ken
    Member

    Fixed in master by #752, which parses R Markdown/Quarto cells independently rather than treating all extracted code as one expression. Current tests preserve document symbols when another R cell is malformed or has eval: false, and exclude non-R engines from R parsing. This addresses the chunk-detection and whole-document failure discussed here.

    Closing as resolved.

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions