In the shiny App below, I need to make variables X and Y be reactive across the scatter and density plots. That means that if the user changes the variable X and/or Y in a certain plot tab, so such variable(s) has/have to remain selected when the user to jump to another plot tab. E.g.: if Sepal.Width (in X) is selected in the 'scatter plot' tab, so 'Sepal.Width' has to remain selected if the user jump to the 'density plot' tab.
library(shiny)
library(shinydashboard)
library(shinyBS)
library(plotly)
shinyApp(
ui = dashboardPage(
dashboardHeader(title = "My plot"),
dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem("1. Data", icon = icon("circle-arrow-up"), tabName = "data"),
menuItem("2. Choose plot", tabName = "charts", icon = icon("signal"),
menuSubItem("Scatter plot", tabName = "scatter"),
menuSubItem("Density plot", tabName = "density"))
)),
dashboardBody(
tabItems(
tabItem(tabName = "data",
dataTableOutput("irisData")),
tabItem(tabName = "scatter",
uiOutput("panelPlot")),
tabItem(tabName = "density",
uiOutput("panelPlot2"))
))),
server = function(input, output, session) {
output$irisData <- renderDataTable({
getModel <- reactive({
names(iris) })
varx <- selectInput("varsel.x", "Variable X",
choices = as.list(getModel()), multiple = F)
getModelnum <- reactive({
filtroNumeric <- iris[sapply(iris, is.numeric)]
names(filtroNumeric) })
vary <- selectInput("varsel.y", "Variable Y",
choices = as.list(getModelnum()), multiple = F)
output$panelPlot <- renderUI({
bsCollapse(open = "Variables and plot",
bsCollapsePanel("Variables and plot",
column(12,
column(3,
fluidRow(
varx,
vary)),
column(9,
fluidRow(align="center",
plotlyOutput('Gdisp')))
))) })
output$panelPlot2 <- renderUI({
bsCollapse(open = "Variables and plot",
bsCollapsePanel("Variables and plot",
column(12,
column(3,
fluidRow(
varx,
vary)),
column(9,
fluidRow(align="center",
plotlyOutput('Gdensity')))
))) })
output$Gdisp <- renderPlotly({
p <- iris %>%
ggplot(aes(x=!!rlang::sym(input$varsel.x), y=!!rlang::sym(input$varsel.y))) +
geom_point()
})
output$Gdensity <- renderPlotly({
p <- iris %>%
ggplot(aes(x=!!rlang::sym(input$varsel.x), y=!!rlang::sym(input$varsel.y))) +
geom_hex(bins=50)
})
iris
})
})