reprex icon indicating copy to clipboard operation
reprex copied to clipboard

fling reprex on the internet e.g. as a gist

Open cderv opened this issue 6 years ago • 14 comments

Code snippet exchange are being quite popular as reprex for issue filing, shared example or other needs. Currently, they are published most often on SO, GH and Rstudio community, and mainly used as minimal reprex, so short snippet.

Sometimes, reprex can be long on purpose (not minimal reprex) and it could worth being published online for isolation, reuse or just clarity.

A gist seems to me very similar to a code snippet or example, and I used some gist sometimes for that. I think it could be interesting to be able to publish a reprex as a gist containing the source code and the files to create the reprex.

There is one example in this gist where I wanted to publish some code examples online, related to this discussion but outside. For this one, I took the yaml front matter used for reprex to generate the md file and use gistr :package: to render and publish, as this package has rendering hability like reprex.

callr::r(function() {
  gistr::gist_create("map_flat_use_case.R", description = "map_flat usage examples", 
                     knit = TRUE, include_source = TRUE)
}, show = TRUE)

In this case, the R script and the md file are published in the same gist. Reprex is a great way to create quickly a reproductible code using R code and markdown, even for long script. Using Rmd format could have been another way, publish the Rmd file and the md output. But it felt easier to use reprex.

I think it could be useful to have in reprex such a feature of pusblishing results as a gist and retrieve the url to copy-paste somewhere. This would help sharing reprex more easily, even for minimal reprex.

What do you think on this feature ? Is a gist the correct tool for publishing ?

For this gistr can help and gistfo can be of inspiration.

Questions identified:

  • Which file to publish : R script with user code ?, R script created by reprex ?, code as Rmd file ? md output only ?
  • How to deal with external files ? (mainly images, but I think it is ok through imgur
  • Should it provide a way to update the gist ?

I can work further on something (i.e a POC) if you want.

cderv avatar May 08 '18 23:05 cderv

It would be great to be able to post a full reprex outside of issues in a specific repo. A gist is a great solution for this.

jayhesselberth avatar Jul 31 '18 03:07 jayhesselberth

It also reminds me of https://github.com/ropenscilabs/codefinch

maelle avatar Sep 16 '18 06:09 maelle

I keep rediscovering this issue when I want to post a standalone reprex. What do you think @jennybc?

jayhesselberth avatar Feb 20 '19 13:02 jayhesselberth

Yeah I'm in favour of this.

Thought dump: it would be interesting if your source code went up into the gist, but also the rendered product for one or more venues.

jennybc avatar Feb 20 '19 18:02 jennybc

Possibly relevant/useful https://github.com/MilesMcBain/gistfo

batpigandme avatar Feb 20 '19 19:02 batpigandme

OK I can work on this if it is getting interest.

cderv avatar Feb 20 '19 23:02 cderv

We can discuss design here.

For example, I am generally anti-gist because notifications don't work, among other things. I frequently end feeling like it's GitHub-lite, but in a bad way. So I'd like to explore the pros/cons of targeting a gist vs. a GitHub repo.

Pros for GitHub repo: all sorts of existing tooling (technical and mental) works as usual.

Cons for GitHub repo: would this lead to unsavory proliferation of repos? When people use a gist vs gh repo, is this one of their motivations, i.e. to reduce clutter?

Pros for gist: I suspect there are existing tools that integrate better with gists than gh repos. But does that matter to us? Morally, reprexes certainly feel like what gists are for. Probably easier to create.

Cons for gist: git/github-lite. No notifications, making conversation around a "reprex as gist" essentially impossible.

jennybc avatar Feb 21 '19 00:02 jennybc

I semi-frequently want to provide a reprex with some demonstration code and send it to someone in rendered form. But this code isn't really associated with any particular repo—i.e., I'm not raising an "issue", just want to share something.

I suppose I could setup a personal repo called "reprexes" and post these types of things there. Or maybe there should be a communal version of this somewhere? E.g. in the tidyverse org? This would solve the unwanted ephemeral nature of one-off reprex posts as gists.

jayhesselberth avatar Feb 21 '19 00:02 jayhesselberth

I think, if gh repo were the target, it would have to be at the user-level (versus some large communal bucket). I have contemplated doing this sort of thing for local/personal reasons anyway. Sometimes I wish I had a central record of my reprexes (or, at least, some of them).

jennybc avatar Feb 21 '19 01:02 jennybc

Personally I use a gist to take some note or save some snippets I want to share or remember, specifically when those are not associated with any repo. Often (if not all the time), if the gist is shared, the link is posted in a conversation (github, community.rstudio.com, SO, ...). The discussion, if any, happens in this other site and not in the gist website.

I find that reprex is like a snippet and should be share in something like a gist. For me Github repo are for larger piece of code, and I associate it with a project. What I am feeling:

  • Having a Github repo per reprex, is like having a RStudio project per reprex,
  • one central Github repo is like having a project for reprex on my computer. However, every time I make a reprex, I am not specifically in a project. Also, it is like graphic: it is upload in imgur and not a github repo. It is like image gist I guess.

I guess It could make sense to adapt the workflow and to have a central repo as user where to store reprex, with an associated project. I know some people have a central repo for presentation or for saving notes. I agree that this should be per user and not central. (like one reprex repo configured as an option to use with the 📦 for all the reprexes) On the contrary, having one repo per reprex would create to many repositories on github and fill unnecessarily the github account of a user. I would be like trying to use github to replicate what gists are.

I find the tool for gist useful (gistr :package:) but it is a different approach than github and existing tools around R workflow with git.

One way to tackle this is also to see what would be bundled in the shared reprex for this feature: the code source and an rendering ? with images ? several rendering ? several formats (R scripts, Rmd, md, html ?). Depending on this, and how far we go from a snippet to a small reproducible project, gist would be less ok and github would be prefered.

cderv avatar Feb 21 '19 07:02 cderv

Ok let's go with the gist version of this.

Perhaps it could start as a PR for an article that explores a few scenarios of putting code in a gist along with various products, like a rendered version, figs, etc.

Then it would be much easier to decide if wrapping this into a function -- and a function that lives here -- is the way to go.

jennybc avatar May 17 '19 22:05 jennybc

I had a bit of a look through this. gistr should be sufficient for this functionality, however you would need to use gistr::gist_create_git() as a reprex may contain binary files (such as plots).

Creating gists would also require a GITHUB_PAT to be specified that has sufficient scoping to create new gists.

Some minor path changes to the knitted resources would also be needed, as gists are flat structures.

mitchelloharawild avatar Jul 08 '19 14:07 mitchelloharawild

Does anyone know how gist pulls the print output from Python (e.g. Jupyter) Notebooks and includes that in what it renders?

geotheory avatar Aug 03 '20 22:08 geotheory

A proof of concept with gh as a new dependency to access github gists API. The only objects/files needed are the expression/input as a string and the content of the markdown file (both already created in a reprex).

Then a gist is defined with the code as a file, while the markdown output is posted as a comment allowing the rendering for tables, figures, etc.

It could be a new venue. I am happy to make a PR, if the following example suits well the purpose.

input <- c(
  "#' Hello world",
  "## comment",
  "1:5"
)
(output <- reprex::reprex(input = input, venue = "gh", html_preview = FALSE))
#> i Rendering reprex...
#> CLIPR_ALLOW has not been set, so clipr will not run interactively
#> [1] "Hello world"                                                                                    
#> [2] ""                                                                                               
#> [3] "``` r"                                                                                          
#> [4] "## comment"                                                                                     
#> [5] "1:5"                                                                                            
#> [6] "#> [1] 1 2 3 4 5"                                                                               
#> [7] "```"                                                                                            
#> [8] ""                                                                                               
#> [9] "<sup>Created on 2021-04-11 by the [reprex package](https://reprex.tidyverse.org) (v2.0.0)</sup>"

(file_id <- sprintf("%s-%s.R", sample(reprex:::adjective_animal, 1), as.character(as.integer(Sys.time()))))
#> [1] "steep-carp-1618149950.R"

gist_info <- gh::gh(
  "POST /gists", 
  public = "true", 
  files = `names<-`(
    list(list("content" = paste(input, collapse = "\n"))), 
    file_id
  )
)

gist_comment <- gh::gh(gist_info$comments_url, .method = "POST", body = paste(output, collapse = "\n"))

sprintf("https://gist.github.com/%s/%s/", gh::gh_whoami()$login, gist_info$id)
#> [1] "https://gist.github.com/mcanouil/3a55759930cfb70f382cf3e68c4b81d0/"

mcanouil avatar Apr 11 '21 14:04 mcanouil