Update app.R
Browse files
app.R
CHANGED
|
@@ -3,8 +3,6 @@ library(shiny)
|
|
| 3 |
library(bslib)
|
| 4 |
library(dplyr)
|
| 5 |
library(ggplot2)
|
| 6 |
-
library(shiny)
|
| 7 |
-
library(shinythemes)
|
| 8 |
library(shinyAce)
|
| 9 |
library(shinyjs)
|
| 10 |
|
|
@@ -12,63 +10,59 @@ renderLogEntry <- function(entry){
|
|
| 12 |
paste0(entry, " - ", date())
|
| 13 |
}
|
| 14 |
|
| 15 |
-
|
| 16 |
withConsoleRedirect <- function(containerId, expr) {
|
| 17 |
txt <- capture.output(results <- expr, type = "output")
|
| 18 |
if (length(txt) > 0) {
|
| 19 |
-
appendTabsetPanel(
|
| 20 |
}
|
| 21 |
results
|
| 22 |
}
|
| 23 |
|
| 24 |
-
|
| 25 |
ui <- shinyUI(
|
| 26 |
tagList(
|
| 27 |
tags$head(
|
| 28 |
tags$link(rel = "stylesheet", type = "text/css", href = "styles.css"),
|
| 29 |
tags$script(src = "script.js")
|
| 30 |
),
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
)
|
| 71 |
-
)
|
| 72 |
)
|
| 73 |
)
|
| 74 |
)
|
|
@@ -77,107 +71,81 @@ ui <- shinyUI(
|
|
| 77 |
|
| 78 |
server <- shinyServer(function(input, output, session) {
|
| 79 |
|
| 80 |
-
# Ocultar el panel lateral al inicio
|
| 81 |
hide("sidebar")
|
|
|
|
| 82 |
observeEvent(input$theme_button, {
|
| 83 |
-
showModal(modalDialog(
|
| 84 |
-
title = "Elige un tema",
|
| 85 |
-
shinythemes::themeSelector()
|
| 86 |
-
))
|
| 87 |
})
|
| 88 |
-
|
| 89 |
-
# Agrega este observador para actualizar el tema del editor
|
| 90 |
observe({
|
| 91 |
-
updateAceEditor(
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
)
|
| 96 |
-
})
|
| 97 |
-
# Evaluar el c贸digo
|
| 98 |
-
output$knitDoc <- renderUI({
|
| 99 |
-
input$eval
|
| 100 |
-
HTML(knitr::knit2html(text = isolate(input$rmd), quiet = TRUE))
|
| 101 |
-
})
|
| 102 |
-
|
| 103 |
-
#clear the editor
|
| 104 |
-
observeEvent(input$clear,{
|
| 105 |
-
updateAceEditor(session,'rmd',value = '')
|
| 106 |
-
})
|
| 107 |
-
|
| 108 |
-
#Open chunk
|
| 109 |
-
observeEvent(input$open_chunk,{
|
| 110 |
-
delay(3000,{
|
| 111 |
-
updateAceEditor(session,'rmd',value = paste(isolate(input$rmd),"\n```{r}\n\n```\n",sep = ''))
|
| 112 |
-
})
|
| 113 |
-
#ADD DELAY TO THE SERVER SAVE THE EDITOR
|
| 114 |
-
})
|
| 115 |
-
|
| 116 |
-
#Hotkeys
|
| 117 |
-
|
| 118 |
-
## Open chunk hotkey
|
| 119 |
-
observeEvent(input$rmd_open_chunk, {
|
| 120 |
-
|
| 121 |
-
delay(3000, {
|
| 122 |
-
# Get the current value of the editor
|
| 123 |
-
old_val <- isolate(input$rmd)
|
| 124 |
-
|
| 125 |
-
# Define the new chunk
|
| 126 |
-
new_chunk <- "\n```{r}\n\n```\n"
|
| 127 |
-
|
| 128 |
-
# Insert the new chunk at the end of the current value
|
| 129 |
-
new_val <- paste(old_val, new_chunk, sep = "")
|
| 130 |
-
|
| 131 |
-
# Update the editor with the new value
|
| 132 |
-
updateAceEditor(session, 'rmd', value = new_val)
|
| 133 |
-
})
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
# Renderiza el archivo temporal a HTML
|
| 173 |
-
rmarkdown::render(input = tmp_file, output_file = file, output_format = "html_document")
|
| 174 |
-
}
|
| 175 |
-
)
|
| 176 |
-
# Toggle sidebar
|
| 177 |
-
observeEvent(input$toggleSidebar, {
|
| 178 |
-
toggle("sidebar")
|
| 179 |
-
})
|
| 180 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
shinyApp(ui, server)
|
|
|
|
|
|
| 3 |
library(bslib)
|
| 4 |
library(dplyr)
|
| 5 |
library(ggplot2)
|
|
|
|
|
|
|
| 6 |
library(shinyAce)
|
| 7 |
library(shinyjs)
|
| 8 |
|
|
|
|
| 10 |
paste0(entry, " - ", date())
|
| 11 |
}
|
| 12 |
|
|
|
|
| 13 |
withConsoleRedirect <- function(containerId, expr) {
|
| 14 |
txt <- capture.output(results <- expr, type = "output")
|
| 15 |
if (length(txt) > 0) {
|
| 16 |
+
appendTabsetPanel(Session, containerId, tabPanel("Console", verbatimTextOutput(paste0(txt, "\n"))))
|
| 17 |
}
|
| 18 |
results
|
| 19 |
}
|
| 20 |
|
|
|
|
| 21 |
ui <- shinyUI(
|
| 22 |
tagList(
|
| 23 |
tags$head(
|
| 24 |
tags$link(rel = "stylesheet", type = "text/css", href = "styles.css"),
|
| 25 |
tags$script(src = "script.js")
|
| 26 |
),
|
| 27 |
+
fluidPage(
|
| 28 |
+
useShinyjs(),
|
| 29 |
+
div(
|
| 30 |
+
id = "sidebar",
|
| 31 |
+
sidebarPanel(
|
| 32 |
+
h2("Opciones"),
|
| 33 |
+
actionButton("theme_button", "Elige un tema"),
|
| 34 |
+
selectInput('theme_code', 'Tema editor', choices = themes, selected = 'ambiance'),
|
| 35 |
+
downloadButton('save_code', 'Guardar codigo', icon = icon('save')),
|
| 36 |
+
downloadButton('save_knit', 'Guardar knitr', icon = icon('save')),
|
| 37 |
+
tags$a(href = "https://github.com/pablovanegas/runr", target = "_blank", class = "btn btn-default shiny-bound-input", "Ver C贸digo Fuente")
|
| 38 |
+
)
|
| 39 |
+
),
|
| 40 |
+
mainPanel(
|
| 41 |
+
actionButton("toggleSidebar", "Opciones"),
|
| 42 |
+
h1("Simple R"),
|
| 43 |
+
div(class = "row",
|
| 44 |
+
div(class = "col-md-12",
|
| 45 |
+
h2("Tu codigo: "),
|
| 46 |
+
verbatimTextOutput("log"),
|
| 47 |
+
aceEditor("rmd", mode = "markdown", value = init,
|
| 48 |
+
hotkeys = list(
|
| 49 |
+
open_chunk = 'Ctrl-Alt-I',
|
| 50 |
+
save_code = "Ctrl-S",
|
| 51 |
+
help_key = "F1"
|
| 52 |
+
),
|
| 53 |
+
autoComplete = "live"
|
| 54 |
+
),
|
| 55 |
+
actionButton("eval", "Run", icon = icon('play')),
|
| 56 |
+
actionButton('clear', 'Clear', icon = icon('eraser')),
|
| 57 |
+
actionButton("open_chunk", "Insert Chunk", icon = icon('plus'))
|
| 58 |
+
)))
|
| 59 |
+
,
|
| 60 |
+
div(
|
| 61 |
+
class = "row",
|
| 62 |
+
div(
|
| 63 |
+
class = "col-md-12",
|
| 64 |
+
h1("Resultado: "),
|
| 65 |
+
htmlOutput("knitDoc")
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
)
|
| 68 |
)
|
|
|
|
| 71 |
|
| 72 |
server <- shinyServer(function(input, output, session) {
|
| 73 |
|
|
|
|
| 74 |
hide("sidebar")
|
| 75 |
+
|
| 76 |
observeEvent(input$theme_button, {
|
| 77 |
+
showModal(modalDialog(title = "Elige un tema", shinythemes::themeSelector()))
|
|
|
|
|
|
|
|
|
|
| 78 |
})
|
| 79 |
+
|
|
|
|
| 80 |
observe({
|
| 81 |
+
updateAceEditor(session,"rmd",
|
| 82 |
+
theme = input$theme_code
|
| 83 |
+
)
|
| 84 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
observeEvent(input$eval, {
|
| 87 |
+
HTML(knitr::knit2html(text = isolate(input$rmd), quiet = TRUE))
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
observeEvent(input$clear, {
|
| 91 |
+
updateAceEditor(session, 'rmd', value = '')
|
| 92 |
+
})
|
| 93 |
+
|
| 94 |
+
observeEvent(input$open_chunk, {
|
| 95 |
+
delay(3000, {
|
| 96 |
+
updateAceEditor(session, 'rmd', value = paste(isolate(input$rmd), "\n{r}\n\n\n", sep = ''))
|
| 97 |
+
})
|
| 98 |
+
})
|
| 99 |
+
|
| 100 |
+
observeEvent(input$rmd_open_chunk, {
|
| 101 |
+
delay(3000, {
|
| 102 |
+
old_val <- isolate(input$rmd)
|
| 103 |
+
new_chunk <- "\n{r}\n\n\n"
|
| 104 |
+
new_val <- paste(old_val, new_chunk, sep = "")
|
| 105 |
+
updateAceEditor(session, 'rmd', value = new_val)
|
| 106 |
+
})
|
| 107 |
+
})
|
| 108 |
+
|
| 109 |
+
observeEvent(input$rmd_help_key, {
|
| 110 |
+
showModal(modalDialog(
|
| 111 |
+
title = "Help Menu",
|
| 112 |
+
h2("Hot-Keys"),
|
| 113 |
+
"Use the following hot-keys:",
|
| 114 |
+
tags$ul(
|
| 115 |
+
tags$li("Ctrl-Alt-I: Open Chunk"),
|
| 116 |
+
tags$li("Ctrl-F: Find & Replace"),
|
| 117 |
+
tags$li("F1: Help Menu"),
|
| 118 |
+
tags$li("Ctrl-Z: Undo"),
|
| 119 |
+
tags$li("Ctrl-Y: Redo"),
|
| 120 |
+
)
|
| 121 |
+
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
})
|
| 123 |
+
|
| 124 |
+
output$save_code <- downloadHandler(
|
| 125 |
+
filename = function() {
|
| 126 |
+
paste("code-", Sys.Date(), ".Rmd", sep = "")
|
| 127 |
+
},
|
| 128 |
+
content = function(file) {
|
| 129 |
+
writeLines(input$rmd, file)
|
| 130 |
+
}
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
output$save_knit <- downloadHandler(
|
| 134 |
+
filename = function() {
|
| 135 |
+
paste("knit-", Sys.Date(), ".html", sep = "")
|
| 136 |
+
},
|
| 137 |
+
content = function(file) {
|
| 138 |
+
tmp_file <- tempfile(fileext = ".Rmd")
|
| 139 |
+
writeLines(input$rmd, tmp_file)
|
| 140 |
+
rmarkdown::render(input = tmp_file, output_file = file, output_format = "html_document")
|
| 141 |
}
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
observeEvent(input$toggleSidebar, {
|
| 145 |
+
toggle(session, "sidebar")
|
| 146 |
+
}
|
| 147 |
+
)
|
| 148 |
+
})
|
| 149 |
|
| 150 |
shinyApp(ui, server)
|
| 151 |
+
|