-
-
Save FrissAnalytics/3567fb939590d55b108b to your computer and use it in GitHub Desktop.
using renderUI based input in a Shiny Module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(ggplot2) | |
# module UI function | |
scatterOuput <- function(id){ | |
ns <- NS(id) | |
tagList( | |
br(), | |
fluidRow( | |
column(6, | |
# dynamic controls to select x and y in plot1, to be rendered via server | |
uiOutput(ns("dynamicControls")), | |
# plot 1 | |
plotOutput(ns("plot1")) | |
), | |
column(6, | |
# dynamic controls to select x and y in plot2 | |
fluidRow( | |
column(offset = 2, 4, | |
selectInput(ns("x2"),"x2",choices = names(mtcars), width = "100%") | |
), | |
column(4, | |
selectInput(ns("y2"),"y2",choices = names(mtcars), width = "100%") | |
) | |
), | |
# plot 2 | |
plotOutput(ns("plot2")) | |
) | |
) | |
) | |
} | |
# module server function | |
scatter <- function(input, output, session, data) { | |
# set data | |
data <- mtcars | |
# dynamic controls | |
output$dynamicControls <- renderUI({ | |
tagList( | |
fluidRow( | |
column(offset = 2, 4, | |
selectInput("x1","x1",choices = names(mtcars), width = "100%") | |
), | |
column(4, | |
selectInput("y1","y1",choices = names(mtcars), width = "100%") | |
) | |
) | |
) | |
}) | |
# plot 1 | |
output$plot1 <- renderPlot({ | |
scatterPlot(data, c(input$x1,input$y1)) | |
#scatterPlot(data, c("mpg","disp")) | |
}) | |
# plot2 | |
output$plot2 <- renderPlot({ | |
scatterPlot(data, c(input$x2,input$y2)) | |
}) | |
} | |
# plot helper function | |
scatterPlot <- function(data, cols) { | |
ggplot(data, aes_string(x = cols[1], y = cols[2])) + geom_point() | |
} | |
# main ui | |
ui <- fixedPage( | |
scatterOuput("id1") | |
) | |
# main server | |
server <- function(input, output, session) { | |
callModule(scatter, "id1", data = mtcars) | |
} | |
# run app | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BTW, to get rid of red error message that flashes before plot #1 is displayed, modify output$plot1 ...