modify
Browse files
server.R
CHANGED
@@ -1,28 +1,125 @@
|
|
1 |
-
|
2 |
-
#
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
function(
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
source('init.R')
|
2 |
+
# server.R
|
3 |
+
library(shiny)
|
4 |
+
library(shinythemes)
|
5 |
+
library(shinyAce)
|
6 |
+
library(shinyjs)
|
7 |
+
# Design 2:
|
8 |
+
renderLogEntry <- function(entry){
|
9 |
+
paste0(entry, " - ", date())
|
10 |
+
}
|
11 |
+
|
12 |
+
|
13 |
+
withConsoleRedirect <- function(containerId, expr) {
|
14 |
+
txt <- capture.output(results <- expr, type = "output")
|
15 |
+
if (length(txt) > 0) {
|
16 |
+
appendTabsetPanel(session, "console", tabPanel("Console", verbatimTextOutput(paste0(txt, "\n"))))
|
17 |
+
}
|
18 |
+
results
|
19 |
+
}
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
shinyServer(function(input, output, session) {
|
24 |
+
|
25 |
+
# Ocultar el panel lateral al inicio
|
26 |
+
hide("sidebar")
|
27 |
+
observeEvent(input$theme_button, {
|
28 |
+
showModal(modalDialog(
|
29 |
+
title = "Elige un tema",
|
30 |
+
shinythemes::themeSelector()
|
31 |
+
))
|
32 |
+
})
|
33 |
+
|
34 |
+
# Agrega este observador para actualizar el tema del editor
|
35 |
+
observe({
|
36 |
+
updateAceEditor(
|
37 |
+
session,
|
38 |
+
"rmd",
|
39 |
+
theme = input$theme_code
|
40 |
+
)
|
41 |
+
})
|
42 |
+
# Evaluar el c贸digo
|
43 |
+
output$knitDoc <- renderUI({
|
44 |
+
input$eval
|
45 |
+
HTML(knitr::knit2html(text = isolate(input$rmd), quiet = TRUE))
|
46 |
+
})
|
47 |
+
|
48 |
+
#clear the editor
|
49 |
+
observeEvent(input$clear,{
|
50 |
+
updateAceEditor(session,'rmd',value = '')
|
51 |
+
})
|
52 |
+
|
53 |
+
#Open chunk
|
54 |
+
observeEvent(input$open_chunk,{
|
55 |
+
delay(3000,{
|
56 |
+
updateAceEditor(session,'rmd',value = paste(isolate(input$rmd),"\n```{r}\n\n```\n",sep = ''))
|
57 |
+
})
|
58 |
+
#ADD DELAY TO THE SERVER SAVE THE EDITOR
|
59 |
+
})
|
60 |
+
|
61 |
+
#Hotkeys
|
62 |
+
|
63 |
+
## Open chunk hotkey
|
64 |
+
observeEvent(input$rmd_open_chunk, {
|
65 |
+
|
66 |
+
delay(3000, {
|
67 |
+
# Get the current value of the editor
|
68 |
+
old_val <- isolate(input$rmd)
|
69 |
+
|
70 |
+
# Define the new chunk
|
71 |
+
new_chunk <- "\n```{r}\n\n```\n"
|
72 |
+
|
73 |
+
# Insert the new chunk at the end of the current value
|
74 |
+
new_val <- paste(old_val, new_chunk, sep = "")
|
75 |
+
|
76 |
+
# Update the editor with the new value
|
77 |
+
updateAceEditor(session, 'rmd', value = new_val)
|
78 |
+
})
|
79 |
+
|
80 |
+
})
|
81 |
+
|
82 |
+
## Open help menu hotkey
|
83 |
+
observeEvent(input$rmd_help_key, {
|
84 |
+
# Mostrar el men煤 de ayuda
|
85 |
+
showModal(modalDialog(
|
86 |
+
title = "Help Menu",
|
87 |
+
h2("Hot-Keys"), # T铆tulo del men煤 de ayuda
|
88 |
+
"Use the following hot-keys:", # Descripci贸n de los hot-keys
|
89 |
+
tags$ul(
|
90 |
+
tags$li("Ctrl-Alt-I: Open Chunk"), # Hot-key para abrir un nuevo bloque de c贸digo
|
91 |
+
tags$li("Ctrl-F: Buscar y Reemplazar"), # Hot-key para guardar el c贸digo
|
92 |
+
tags$li("F1: Help Menu"), # Hot-key para abrir el men煤 de ayuda
|
93 |
+
tags$li("Ctrl-Z: Undo"), # Hot-key para deshacer la 煤ltima acci贸n
|
94 |
+
tags$li("Ctrl-Y: Redo"), # Hot-key para rehacer la 煤ltima acci贸n
|
95 |
+
)
|
96 |
+
))
|
97 |
+
})
|
98 |
+
# Agrega estos manejadores de descarga para los botones save_code y save_knit
|
99 |
+
output$save_code <- downloadHandler(
|
100 |
+
filename = function() {
|
101 |
+
paste("code-", Sys.Date(), ".Rmd", sep="")
|
102 |
+
},
|
103 |
+
content = function(file) {
|
104 |
+
writeLines(input$rmd, file)
|
105 |
+
}
|
106 |
+
)
|
107 |
+
|
108 |
+
output$save_knit <- downloadHandler(
|
109 |
+
filename = function() {
|
110 |
+
paste("knit-", Sys.Date(), ".html", sep="")
|
111 |
+
},
|
112 |
+
content = function(file) {
|
113 |
+
# Guarda el contenido del editor Ace en un archivo temporal
|
114 |
+
tmp_file <- tempfile(fileext = ".Rmd")
|
115 |
+
writeLines(input$rmd, tmp_file)
|
116 |
+
|
117 |
+
# Renderiza el archivo temporal a HTML
|
118 |
+
rmarkdown::render(input = tmp_file, output_file = file, output_format = "html_document")
|
119 |
+
}
|
120 |
+
)
|
121 |
+
# Toggle sidebar
|
122 |
+
observeEvent(input$toggleSidebar, {
|
123 |
+
toggle("sidebar")
|
124 |
+
})
|
125 |
+
})
|