knitr icon indicating copy to clipboard operation
knitr copied to clipboard

add a helper for automatic naming of unnamed code chunks

Open rkb965 opened this issue 2 years ago • 3 comments

Feature request: Could you add automatic naming of code chunks when inserting new chunk?

Justification: I create new code chunks via Ctrl + Alt + I. Sometimes I write sensible names for the code chunks, and sometimes I don't bother and instead leave them unnamed. When I knit and see messages/warnings in, eg, Unnamed Code Chunk 23, I am less likely to investigate whether or not this message/warning needs to be addressed when I have no idea what code chunk it originated from. That's definitely bad practice on my part.

Solution: I am aware of namer but (1) I think the warning re: version control is a bit off-putting as a newer user to git and (2) I think this functionality should be incorporated directly into rmarkdown/knitr. I could envision a small random string of letters or a more human-readable random 2-or-3 word string (eg as GitHub does for new repo naming suggestions). If it adds complexity, I don't think there needs to be any overhead re: ensuring no repeats of names since, when duplicate code chunk names are found, that error is thrown so quickly upon rendering.

Thank you so much for considering this!

I conflate knitr and rmarkdown so apologies if this is the wrong location for this feature request.


By filing an issue to this repo, I promise that

  • [X] I have fully read the issue guide at https://yihui.org/issue/.
  • [X] I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • [X] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

rkb965 avatar Feb 11 '23 19:02 rkb965

Thanks for the suggestion !

This would be an issue for the RStudio IDE (https://github.com/rstudio/rstudio) as this is where the CTRL + ALT + I feature is implemented. We can't change anything regarding what will be inserted in the source pane of the IDE from the knitr or rmarkdown package.

I believe this is a great suggestions and you should open there if there are none on that topic already

(2) I think this functionality should be incorporated directly into rmarkdown/knitr

We could do something like namer to "fix" a document by naming the unnamed chunk. Historically, knitr is labelling chunk internally using unamed-chunk-i, i being the chunk number. Hence why it is reported in issue. It is not core to knitr knitting feature though, and it already lives in another R package on CRAN that can easily be used.

We need to think about it. 🤔 @yihui some thoughts about a helper function to modify a Rmd source to add random name to unnamed chunks ?

cderv avatar Feb 13 '23 10:02 cderv

Thanks for the clarification, @cderv ! I wasn't confident that this belonged in knitr but it was my best guess (and, not surprisingly, wrong). I'll open a different feature request ticket in rstudio.

I agree that there's a package that solves this issue, but it's such a basic and common problem that I personally think it shouldn't rely on an external package and should instead somehow be smoothly integrated into the user experience.

Thanks for your time and consideration!

rkb965 avatar Feb 13 '23 16:02 rkb965

@yihui some thoughts about a helper function to modify a Rmd source to add random name to unnamed chunks ?

Yes, we can do that. Naming is hard (except for totally random names), though.

yihui avatar Feb 13 '23 17:02 yihui