File size: 3,779 Bytes
af1884d
 
 
 
564f884
 
af1884d
564f884
 
 
af1884d
564f884
 
 
49cb6cb
564f884
 
 
 
 
 
 
 
 
 
49cb6cb
 
 
 
 
 
 
6487bf4
49cb6cb
 
 
 
 
 
 
 
 
 
 
 
34893bf
 
 
49cb6cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af1884d
564f884
 
 
 
 
cb670e6
564f884
 
49cb6cb
564f884
49cb6cb
564f884
49cb6cb
564f884
49cb6cb
 
 
 
af1884d
49cb6cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
564f884
49cb6cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af1884d
49cb6cb
 
 
 
 
 
 
af1884d
 
49cb6cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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)