Skip to content

Commit c994af3

Browse files
committed
gg2list() update
1 parent ba98bc6 commit c994af3

File tree

8 files changed

+44
-88
lines changed

8 files changed

+44
-88
lines changed

Examples/Diamonds/plotlyGraphWidget.R

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#This script is necessary in the app folder
1+
#This script is necessary in the app folder
22
#but the user should not have to edit it
33

44
#Output Graph Function
@@ -12,25 +12,13 @@ graphOutput <- function(inputId, width="100%", height="550px") {
1212
)
1313
}
1414

15-
#Function to change ggplot figure into plotly syntax
16-
#gg is the users ggplot
17-
#called in the server script to return data and layout info separately
18-
gg2fig <- function(gg) {
19-
fig <- gg2list(gg)
20-
data <- list()
21-
for(i in 1:(length(fig)-1)){data[[i]]<-fig[[i]]}
22-
layout <- fig$kwargs$layout
23-
result <- list(data=data,layout=layout)
24-
return(result)
25-
}
26-
2715
renderGraph <- function(expr, env=parent.frame(), quoted=FALSE) {
2816
## This gets called when inputs change --
2917
## Place data wrangling code in here
3018
## and pass the result to the client
3119
## to be graphed.
3220

33-
21+
3422
installExprFunction(expr, "func", env, quoted)
3523

3624
function(){
@@ -42,6 +30,6 @@ renderGraph <- function(expr, env=parent.frame(), quoted=FALSE) {
4230
## and https://github.com/plotly/Embed-API for more about the postMessage
4331
## graph messages
4432
return(data)
45-
33+
4634
}
4735
}

Examples/Diamonds/server.R

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
#Shiny
22
shinyServer(function(input, output, session) {
3-
3+
44
#add reactive data information. Dataset = built in diamonds data
55
dataset <- reactive({
66
diamonds[sample(nrow(diamonds), input$sampleSize),]
77
})
8-
8+
99
output$trendPlot <- renderGraph({
10-
10+
1111
# build graph with ggplot syntax
1212
# pull x and y variable info
13-
p <- ggplot(dataset(),
14-
aes_string(x = input$x,
15-
y = input$y)) +
16-
geom_point()
17-
13+
p <- ggplot(dataset(),
14+
aes_string(x = input$x,
15+
y = input$y)) +
16+
geom_point()
17+
1818
# if statements for possible color/facet row/facet column variables
1919
if (input$color != 'None')
2020
p <- p + aes_string(color=input$color)
21-
21+
2222
facets <- paste(input$facet_row, '~', input$facet_col)
2323
if (facets != '. ~ .')
2424
p <- p + facet_grid(facets)
25-
26-
# convert from ggplot->plotly
27-
gg <- gg2fig(p)
28-
25+
26+
# use gg2list() to convert from ggplot->plotly
27+
gg <- gg2list(p)
28+
2929
# make edits with plotly syntax
3030
gg$layout <- list(legend = list(
31-
x = 1,
32-
y = 1,
31+
x = 1,
32+
y = 1,
3333
bgcolor = "transparent"))
34-
34+
3535
# Send this message up to the browser client, which will get fed through to
3636
# Plotly's javascript graphing library embedded inside the graph
3737
return(list(
@@ -42,7 +42,7 @@ shinyServer(function(input, output, session) {
4242
layout= gg$layout
4343
)
4444
))
45-
45+
4646
})
47-
47+
4848
})

Examples/Movies/plotlyGraphWidget.R

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#This script is necessary in the app folder
1+
#This script is necessary in the app folder
22
#but the user should not have to edit it
33

44
#Output Graph Function
@@ -12,25 +12,13 @@ graphOutput <- function(inputId, width="100%", height="550px") {
1212
)
1313
}
1414

15-
#Function to change ggplot figure into plotly syntax
16-
#gg is the users ggplot
17-
#called in the server script to return data and layout info separately
18-
gg2fig <- function(gg) {
19-
fig <- gg2list(gg)
20-
data <- list()
21-
for(i in 1:(length(fig)-1)){data[[i]]<-fig[[i]]}
22-
layout <- fig$kwargs$layout
23-
result <- list(data=data,layout=layout)
24-
return(result)
25-
}
26-
2715
renderGraph <- function(expr, env=parent.frame(), quoted=FALSE) {
2816
## This gets called when inputs change --
2917
## Place data wrangling code in here
3018
## and pass the result to the client
3119
## to be graphed.
3220

33-
21+
3422
installExprFunction(expr, "func", env, quoted)
3523

3624
function(){
@@ -42,6 +30,6 @@ renderGraph <- function(expr, env=parent.frame(), quoted=FALSE) {
4230
## and https://github.com/plotly/Embed-API for more about the postMessage
4331
## graph messages
4432
return(data)
45-
33+
4634
}
4735
}
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#This script is necessary in the app folder
1+
#This script is necessary in the app folder
22
#but the user should not have to edit it
33

44
#Output Graph Function
@@ -12,25 +12,13 @@ graphOutput <- function(inputId, width="100%", height="550px") {
1212
)
1313
}
1414

15-
#Function to change ggplot figure into plotly syntax
16-
#gg is the users ggplot
17-
#called in the server script to return data and layout info separately
18-
gg2fig <- function(gg) {
19-
fig <- gg2list(gg)
20-
data <- list()
21-
for(i in 1:(length(fig)-1)){data[[i]]<-fig[[i]]}
22-
layout <- fig$kwargs$layout
23-
result <- list(data=data,layout=layout)
24-
return(result)
25-
}
26-
2715
renderGraph <- function(expr, env=parent.frame(), quoted=FALSE) {
2816
## This gets called when inputs change --
2917
## Place data wrangling code in here
3018
## and pass the result to the client
3119
## to be graphed.
3220

33-
21+
3422
installExprFunction(expr, "func", env, quoted)
3523

3624
function(){
@@ -42,6 +30,6 @@ renderGraph <- function(expr, env=parent.frame(), quoted=FALSE) {
4230
## and https://github.com/plotly/Embed-API for more about the postMessage
4331
## graph messages
4432
return(data)
45-
33+
4634
}
4735
}

Examples/UN_Advanced/server.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,14 @@ shinyServer(function(input, output, session) {
3434
ggideal_point <- ggideal_point+
3535
theme(legend.direction = "horizontal", legend.position = "bottom")
3636

37-
38-
3937
# Year range
4038
min_Year <- min(df_trend$Year)
4139
max_Year <- max(df_trend$Year)
4240

43-
# This converts the ggplot2 graph into Plotly's format.
44-
# This is a list of lists which declaratively describe every attribute
45-
# of the plotly graph
46-
gg<- gg2fig(ggideal_point)
47-
41+
# Use gg2list() to convert from ggplot->plotly
42+
gg<- gg2list(ggideal_point)
43+
44+
# Use Plotly syntax to further edit the plot:
4845
gg$layout$annotations <- NULL # Remove the existing annotations (the legend label)
4946
gg$layout$annotations <- list()
5047

Examples/UN_Simple/server.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ shinyServer(function(input, output, session) {
2222
min_Year <- min(df_trend$Year)
2323
max_Year <- max(df_trend$Year)
2424

25-
#This grabs data and layout information from the ggplot
25+
# use gg2list() to convert from ggplot->plotly
2626
gg <- gg2list(ggideal_point)
2727

2828
# Send this message up to the browser client, which will get fed through to

What_You_Need/plotlyGraphWidget.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ graphOutput <- function(inputId, width="100%", height="550px") {
1212
)
1313
}
1414

15-
# Function to change ggplot figure into plotly syntax
16-
# Takes gg (the user's ggplot) as argument.
17-
# Provided by gg2list directly since package version 0.5.30
18-
gg2fig <- gg2list
19-
2015
renderGraph <- function(expr, env=parent.frame(), quoted=FALSE) {
2116
## This gets called when inputs change --
2217
## Place data wrangling code in here
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
## Template for server.R script for Plotly/Shiny apps
22

33
shinyServer(function(input, output, session) {
4-
4+
55
## ADD DATA
66
YOUR_DATA<- call_your_data_file
7-
7+
88
output$trendPlot <- renderGraph({
9-
9+
1010
## Create your ggplot
11-
YOUR_PLOT <- ggplot(YOUR_DATA, YOUR_GGPLOT_SPECIFICATIONS)
12-
11+
YOUR_PLOT <- ggplot(YOUR_DATA, YOUR_GGPLOT_SPECIFICATIONS)
12+
1313
## You can edit your graph with ggplot syntax here!
14-
14+
1515
## This function rewrites the ggplot figure in Plotly syntax
16-
## and returns data information (gg$data) and layout information (gg$layout)
17-
gg<- gg2fig(YOUR_PLOT)
18-
16+
## and returns data information (gg$data) and layout information (gg$layout)
17+
gg<- gg2list(YOUR_PLOT)
18+
1919
## You can edit your graph with Plotly syntax here!
20-
20+
2121
# This sends message up to the browser client, which will get fed through to
2222
# Plotly's javascript graphing library embedded inside the graph
2323
return(list(
@@ -28,7 +28,7 @@ shinyServer(function(input, output, session) {
2828
layout=gg$layout
2929
)
3030
))
31-
31+
3232
})
33-
33+
3434
})

0 commit comments

Comments
 (0)