Created
September 29, 2016 09:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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