yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.37k stars 874 forks source link

add a helper for automatic naming of unnamed code chunks #2228

Open rkb965 opened 1 year ago

rkb965 commented 1 year ago

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

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

cderv commented 1 year ago

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 ?

rkb965 commented 1 year ago

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!

yihui commented 1 year ago

@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.