I'm playing around with the geom_mosaic()
function (part of the ggmosaic
package) on R Shiny and came up with a problem I've been trying to solve for a few days.
First, some sample data:
a <- "a"
b <- "b"
c <- "c"
df <- tribble(
~id, ~var1, ~var2, ~var3,
1, a, b, c,
2, b, b, c,
3, b, b, c,
4, a, c, b,
5, a, a, a,
6, b, c, c,
7, b, c, a,
8, a, a, b,
9, a, a, a,
10, b, b, c
)
It seems that geom_mosaic()
isn't accepting reactive inputs on Shiny, i.e. Code 1 (below) works fine on the R console, but Code 2 (the R Shiny equivalent) isn't working, giving the following Error message:
Error: object ______ not found
Code 1 (geom_mosaic works fine on console):
library(tidyverse)
library(ggmosaic)
selected_var1 <- "var1"
selected_var1_dat <- df[[selected_var1]]
selected_var2 <- "var2"
selected_var2_dat <- df[[selected_var2]]
ggplot(data = df) +
geom_mosaic(aes(x = product(selected_var1_dat),
fill = selected_var2_dat, na.rm = T))
Output ggplot (everything looks good):
Code 2 (now, to implement in Shiny):
library(shiny)
library(tidyverse)
library(ggmosaic)
varOptions <- c("var1", "var2", "var3")
a <- "a"
b <- "b"
c <- "c"
df <- tribble(
~id, ~var1, ~var2, ~var3,
1, a, b, c,
2, b, b, c,
3, b, b, c,
4, a, c, b,
5, a, a, a,
6, b, c, c,
7, b, c, a,
8, a, a, b,
9, a, a, a,
10, b, b, c
)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "selected_var1",
label = "X:",
choices = varOptions),
selectInput(inputId = "selected_var2",
label = "Y:",
choices = varOptions)
),
mainPanel(
plotlyOutput(outputId = "mosaic")
)
)
)
server <- function(input, output) {
output$mosaic <- renderPlotly({
selected_var1 <- input$selected_var1
selected_var1_dat <- df[[selected_var1]]
selected_var2 <- input$selected_var2
selected_var2_dat <- df[[selected_var2]]
ggplot(data = df) +
geom_mosaic(aes(x = product(selected_var1_dat),
fill = selected_var2_dat, na.rm = T))
})
}
shinyApp(ui = ui, server = server)
Which outputs this error message:
I've tried different ways to get around this problem with no luck. I've worked with Shiny quite a bit and it seems to work with every other ggplot
graph I've used. Does anyone have any ideas on what might be going on?
ggmosaic
and plotly
are not compatible currently. You can get your app to work without plotly with the following code.
library(shiny)
library(tidyverse)
library(ggmosaic)
#library(plotly)
a <- "a"
b <- "b"
c <- "c"
df <- tribble(
~id, ~var1, ~var2, ~var3,
1, a, b, c,
2, b, b, c,
3, b, b, c,
4, a, c, b,
5, a, a, a,
6, b, c, c,
7, b, c, a,
8, a, a, b,
9, a, a, a,
10, b, b, c
)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "selected_var1",
label = "X:",
choices = names(df)[-1]),
selectInput(inputId = "selected_var2",
label = "Y:",
choices = names(df)[-1])
),
mainPanel(
plotOutput(outputId = "mosaic")
)
)
)
server <- function(input, output) {
output$mosaic <- renderPlot({
ggplot(data = df) +
geom_mosaic(aes(x = product(!!sym(input$selected_var1)),
fill = !!sym(input$selected_var2)))
})
}
shinyApp(ui = ui, server = server)