Skip to content

Instantly share code, notes, and snippets.

@daattali
Created September 29, 2016 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daattali/0be8d26380b08a02e7de1622ed17d70d to your computer and use it in GitHub Desktop.
Save daattali/0be8d26380b08a02e7de1622ed17d70d to your computer and use it in GitHub Desktop.
An attempt to have a way to create the code for an output only once, and be able to both display it and its code
# Disclaimer: I'm a noob at NSE and this implementation is just something I whipped up in 5 minutes at 3am.
# So if it's terrible, no judging!
library(shiny)
library(ggplot2)
ui <- fluidPage(
colourpicker::colourInput("col", "Select colour", "red"),
plotOutput("plot"),
verbatimTextOutput("code")
)
server <- function(input, output, session) {
plot_code <- reactive({
steps <- NULL
steps <- append(steps, substitute(
ggplot(mtcars, aes(mpg, wt))
))
steps <- append(steps, substitute(
+ geom_point(color = col)
, list(col = input$col)))
paste(unlist(lapply(steps, deparse)), collapse = " ")
})
output$plot <- renderPlot({
eval(parse(text = plot_code()))
})
output$code <- renderText({
plot_code()
})
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment