library(shiny) library(bslib) library(dplyr) library(ggplot2) library(shinyAce) library(shinyjs) renderLogEntry <- function(entry){ paste0(entry, " - ", date()) } withConsoleRedirect <- function(containerId, expr) { txt <- capture.output(results <- expr, type = "output") if (length(txt) > 0) { appendTabsetPanel(Session, containerId, tabPanel("Console", verbatimTextOutput(paste0(txt, "\n")))) } results } ui <- shinyUI( tagList( tags$head( tags$link(rel = "stylesheet", type = "text/css", href = "styles.css"), tags$script(src = "script.js") ), fluidPage( useShinyjs(), div( id = "sidebar", sidebarPanel( h2("Opciones"), actionButton("theme_button", "Elige un tema"), selectInput('theme_code', 'Tema editor', choices = getAceThemes(), selected = 'ambiance'), downloadButton('save_code', 'Guardar codigo', icon = icon('save')), downloadButton('save_knit', 'Guardar knitr', icon = icon('save')), tags$a(href = "https://github.com/pablovanegas/runr", target = "_blank", class = "btn btn-default shiny-bound-input", "Ver Código Fuente") ) ), mainPanel( actionButton("toggleSidebar", "Opciones"), h1("Simple R"), div(class = "row", div(class = "col-md-12", h2("Tu codigo: "), verbatimTextOutput("log"), aceEditor("rmd", mode = "markdown", value = '```{r} 2*3 ```', hotkeys = list( open_chunk = 'Ctrl-Alt-I', save_code = "Ctrl-S", help_key = "F1" ), autoComplete = "live" ), actionButton("eval", "Run", icon = icon('play')), actionButton('clear', 'Clear', icon = icon('eraser')), actionButton("open_chunk", "Insert Chunk", icon = icon('plus')) ))) , div( class = "row", div( class = "col-md-12", h1("Resultado: "), htmlOutput("knitDoc") ) ) ) ) ) server <- shinyServer(function(input, output, session) { hide("sidebar") observeEvent(input$theme_button, { showModal(modalDialog(title = "Elige un tema", shinythemes::themeSelector())) }) observe({ updateAceEditor(session,"rmd", theme = input$theme_code ) }) observeEvent(input$eval, { HTML(knitr::knit2html(text = isolate(input$rmd), quiet = TRUE)) }) observeEvent(input$clear, { updateAceEditor(session, 'rmd', value = '') }) observeEvent(input$open_chunk, { delay(3000, { updateAceEditor(session, 'rmd', value = paste(isolate(input$rmd), "\n{r}\n\n\n", sep = '')) }) }) observeEvent(input$rmd_open_chunk, { delay(3000, { old_val <- isolate(input$rmd) new_chunk <- "\n{r}\n\n\n" new_val <- paste(old_val, new_chunk, sep = "") updateAceEditor(session, 'rmd', value = new_val) }) }) observeEvent(input$rmd_help_key, { showModal(modalDialog( title = "Help Menu", h2("Hot-Keys"), "Use the following hot-keys:", tags$ul( tags$li("Ctrl-Alt-I: Open Chunk"), tags$li("Ctrl-F: Find & Replace"), tags$li("F1: Help Menu"), tags$li("Ctrl-Z: Undo"), tags$li("Ctrl-Y: Redo"), ) )) }) output$save_code <- downloadHandler( filename = function() { paste("code-", Sys.Date(), ".Rmd", sep = "") }, content = function(file) { writeLines(input$rmd, file) } ) output$save_knit <- downloadHandler( filename = function() { paste("knit-", Sys.Date(), ".html", sep = "") }, content = function(file) { tmp_file <- tempfile(fileext = ".Rmd") writeLines(input$rmd, tmp_file) rmarkdown::render(input = tmp_file, output_file = file, output_format = "html_document") } ) observeEvent(input$toggleSidebar, { toggle(session, "sidebar") } ) }) shinyApp(ui, server)