pajuan commited on
Commit
564f884
verified
1 Parent(s): bd55233

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +168 -43
app.R CHANGED
@@ -1,58 +1,183 @@
 
1
  library(shiny)
2
  library(bslib)
3
  library(dplyr)
4
  library(ggplot2)
 
 
 
 
5
 
6
- df <- readr::read_csv("penguins.csv")
7
- # Find subset of columns that are suitable for scatter plot
8
- df_num <- df |> select(where(is.numeric), -Year)
9
-
10
- ui <- page_sidebar(
11
- theme = bs_theme(bootswatch = "minty"),
12
- title = "Penguins explorer",
13
- sidebar = sidebar(
14
- varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
15
- varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
16
- checkboxGroupInput("species", "Filter by species",
17
- choices = unique(df$Species), selected = unique(df$Species)
18
- ),
19
- hr(), # Add a horizontal rule
20
- checkboxInput("by_species", "Show species", TRUE),
21
- checkboxInput("show_margins", "Show marginal plots", TRUE),
22
- checkboxInput("smooth", "Add smoother"),
23
- ),
24
- plotOutput("scatter")
25
- )
26
 
27
- server <- function(input, output, session) {
28
- subsetted <- reactive({
29
- req(input$species)
30
- df |> filter(Species %in% input$species)
31
- })
32
 
33
- output$scatter <- renderPlot(
34
- {
35
- p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) +
36
- theme_light() +
37
- list(
38
- theme(legend.position = "bottom"),
39
- if (input$by_species) aes(color = Species),
40
- geom_point(),
41
- if (input$smooth) geom_smooth()
42
- )
43
 
44
- if (input$show_margins) {
45
- margin_type <- if (input$by_species) "density" else "histogram"
46
- p <- p |> ggExtra::ggMarginal(
47
- type = margin_type, margins = "both",
48
- size = 8, groupColour = input$by_species, groupFill = input$by_species
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  )
50
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- p
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  },
54
- res = 100
 
 
55
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  }
57
 
58
  shinyApp(ui, server)
 
1
+ source('init.R')
2
  library(shiny)
3
  library(bslib)
4
  library(dplyr)
5
  library(ggplot2)
6
+ library(shiny)
7
+ library(shinythemes)
8
+ library(shinyAce)
9
+ library(shinyjs)
10
 
11
+ 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
+ )
75
+ )
76
+ )
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)