pajuan commited on
Commit
49cb6cb
verified
1 Parent(s): c67c9f7

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +109 -141
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(session, "console", tabPanel("Console", verbatimTextOutput(paste0(txt, "\n"))))
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
- tags$div(
32
- fluidPage(
33
- useShinyjs(),
34
- div(id = "sidebar",
35
- sidebarPanel(
36
- h2("Opciones"),
37
- actionButton("theme_button", "Elige un tema"),
38
- selectInput('theme_code', 'Tema editor', choices = themes, selected = 'ambiance'),
39
- downloadButton('save_code', 'Guardar codigo', icon = icon('save')),
40
- downloadButton('save_knit', 'Guardar knitr', icon = icon('save')),
41
- tags$a(href = "https://github.com/pablovanegas/runr", target = "_blank", class = "btn btn-default shiny-bound-input", "Ver C贸digo Fuente") )
42
- ),#
43
- mainPanel(
44
- actionButton("toggleSidebar", "Opciones"),
45
- h1("Simple R"),
46
- tags$div(
47
- class = "row",
48
- tags$div(
49
- class = "col-md-12", # Cuadrado 1
50
- h2("Tu codigo: "),
51
- verbatimTextOutput("log"),
52
- aceEditor("rmd", mode = "markdown", value = init,
53
- hotkeys = list(
54
- open_chunk = 'Ctrl-Alt-I',
55
- save_code = "Ctrl-S",
56
- help_key = "F1"
57
- ),
58
- autoComplete = "live"),
59
- actionButton("eval", "Run", icon = icon('play')),
60
- actionButton('clear', 'Clear', icon = icon('eraser')),
61
- actionButton("open_chunk", "Insert Chunk", icon = icon('plus'))
62
- )
63
- ),
64
- tags$div(
65
- class = "row",
66
- tags$div(
67
- class = "col-md-12", # Cuadrado 2
68
- h1("Resultado: "),
69
- htmlOutput("knitDoc")
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
- session,
93
- "rmd",
94
- theme = input$theme_code
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
- ## Open help menu hotkey
138
- observeEvent(input$rmd_help_key, {
139
- # Mostrar el men煤 de ayuda
140
- showModal(modalDialog(
141
- title = "Help Menu",
142
- h2("Hot-Keys"), # T铆tulo del men煤 de ayuda
143
- "Use the following hot-keys:", # Descripci贸n de los hot-keys
144
- tags$ul(
145
- tags$li("Ctrl-Alt-I: Open Chunk"), # Hot-key para abrir un nuevo bloque de c贸digo
146
- tags$li("Ctrl-F: Buscar y Reemplazar"), # Hot-key para guardar el c贸digo
147
- tags$li("F1: Help Menu"), # Hot-key para abrir el men煤 de ayuda
148
- tags$li("Ctrl-Z: Undo"), # Hot-key para deshacer la 煤ltima acci贸n
149
- tags$li("Ctrl-Y: Redo"), # Hot-key para rehacer la 煤ltima acci贸n
150
- )
151
- ))
152
- })
153
- # Agrega estos manejadores de descarga para los botones save_code y save_knit
154
- output$save_code <- downloadHandler(
155
- filename = function() {
156
- paste("code-", Sys.Date(), ".Rmd", sep="")
157
- },
158
- content = function(file) {
159
- writeLines(input$rmd, file)
160
- }
161
- )
162
-
163
- output$save_knit <- downloadHandler(
164
- filename = function() {
165
- paste("knit-", Sys.Date(), ".html", sep="")
166
- },
167
- content = function(file) {
168
- # Guarda el contenido del editor Ace en un archivo temporal
169
- tmp_file <- tempfile(fileext = ".Rmd")
170
- writeLines(input$rmd, tmp_file)
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
+