if(interactive()){
library(shiny)
my_code <-
'
# load package and data
library(ggplot2)
data(mpg, package="ggplot2")
# mpg <- read.csv("http://goo.gl/uEeRGu")
# Scatterplot
theme_set(theme_bw()) # pre-set the bw theme.
g <- ggplot(mpg, aes(cty, hwy))
g + geom_jitter(width = .5, size=1) +
labs(subtitle="mpg: city vs highway mileage",
y="hwy",
x="cty",
title="Jittered Points")
'
html_code <-
'
ABC
Some HTML
'
ui <- fluidPage(
fluidRow(
column(
6,
h3("Display by modal"),
column(
6, h4("default"),
spsCodeBtn(id = "a", my_code)
),
column(
6, h4("change color and shape"),
spsCodeBtn(
id = "b", c(my_code, my_code),
color = "red", shape = "circular")
)
),
column(
6,
h3("Display by collapse"),
column(
6, h4("collapse"),
spsCodeBtn(id = "c", my_code, display = "collapse")
),
column(
6, h4("different programming language"),
spsCodeBtn(
id = "d", html_code,
language = "html", display = "collapse")
)
)
),
fluidRow(
column(
6,
h3("Update code"),
spsCodeBtn(
"update-code",
"# No code here",
display = "collapse"
),
actionButton("update", "change code in the left `spsCodeBtn`"),
actionButton("changeback", "change it back")
)
)
)
server <- function(input, output, session) {
observeEvent(input$update, {
shinyAce::updateAceEditor(
session, editorId = "update-code-ace",
value = "# code has changed!\n 1+1"
)
})
observeEvent(input$changeback, {
shinyAce::updateAceEditor(
session, editorId = "update-code-ace",
value = "# No code here"
)
})
}
shinyApp(ui, server)
}
Run the code above in your browser using DataLab