Created
May 26, 2017 20:49
-
-
Save seankross/520f3096a12724216f8408d8cbcaeaf8 to your computer and use it in GitHub Desktop.
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
#' @importFrom shiny | |
#' @import miniUI | |
codefinch_app <- function(){ | |
img_path <- tempfile(fileext = ".png") | |
ui <- miniPage( | |
gadgetTitleBar("Codefinch: Tweet Your Code", | |
right = miniTitleBarButton("tweet_it", "Send Tweet", primary = TRUE)), | |
miniContentPanel( | |
fillRow(flex = c(2, 3), | |
fillCol( | |
uiOutput("status_box"), | |
textOutput("chars"), | |
checkboxInput("gist_url", "Include Gist URL in Image?"), | |
selectInput("highlight", "Select Highlight", choices = c( | |
"default", "tango", "pygments", "kate", "monochrome", | |
"espresso", "zenburn","haddock" | |
)), | |
actionButton("update_preview", "Update Code Preview") | |
), | |
fillCol(imageOutput("code_png")) | |
) | |
) | |
) | |
server <- function(input, output){ | |
ctx <- getSourceEditorContext() | |
path <- tempfile(fileext = ".R") | |
raw_code <- ctx$contents | |
writeLines(raw_code, path) | |
gist_obj <- gist_create(path, browse = FALSE) | |
observeEvent(input$update_preview, ignoreNULL = FALSE, { | |
ctx <- getSourceEditorContext() | |
raw_code <- ctx$contents | |
writeLines(raw_code, path) | |
gist_obj %>% | |
update_files(path) %>% | |
update() | |
create_image(highlight = input$highlight, image_path = img_path, | |
include_gist_url = input$gist_url, | |
gist_obj = gist_obj) | |
}) | |
observeEvent(input$tweet_it, { | |
app <- oauth_app("twitter", | |
key = "cpYmlaSCcBKFBf7icpDT9sJww", | |
secret = "x8pvQAfziT3vVb8A1cQQB1xSBYdZ2ZhuRs4hTzifwLtoQf2iRF" | |
) | |
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), app) | |
req <- POST("https://api.twitter.com/1.1/statuses/update_with_media.json", | |
config(token = twitter_token), body = list( | |
status = input$status_box, | |
media = upload_file(img_path) | |
)) | |
stop_for_status(req) | |
stopApp(returnValue = invisible(gist_obj$html_url)) | |
}) | |
# img <- reactive({ | |
# create_image(highlight = input$highlight) | |
# }) | |
output$status_box <- renderUI({ | |
textAreaInput("status", label = NULL, width = "100%", height = "100%", | |
resize = "both", value = gist_obj$html_url) | |
}) | |
output$code_png <- renderImage({ | |
input$update_preview | |
list(src = img_path, width = "100%") | |
}) | |
output$chars <- renderText({ | |
140 - nchar(input$status) | |
}) | |
} | |
runGadget(ui, server, viewer = paneViewer()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment