How can we reuse the uiOutput used for selectInput drop downs
to reuse the columns2 within Server function and display in UI ?
similar Q:
runApp(list(
ui = bootstrapPage(
selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris')),
uiOutput('x'),
uiOutput('y'),
plotOutput('plot')
),
server = function(input, output){
output$x= renderUI({
mydata = get(input$dataset)
selectInput('columns2', 'X axis', names(mydata))
})
output$y = renderUI({
mydata = get(input$dataset)
selectInput('columns3', 'Y axis', names(mydata))
})
output$plot = renderPlot({
# How to use the x, y to plot something e.g Petal.Width vs Petal.Length
plot(x,y)
})
}
))
Modularized version
Why does this not accepting to use the values of selectInput within Server function ?
library(shiny)
library(tidyverse)
mod_ui <- function(id){
ns <- shiny::NS(id)
shiny::tagList(
selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris')),
uiOutput(ns('x')),
uiOutput(ns('y')),
plotOutput(ns('plot'))
)
}
mod_server = function(input, output, session) {
output$x= renderUI({
mydata = get(input$dataset)
### ?? Should ns() be applied here as well ?
selectInput(ns('columns2'), 'X axis', names(mydata))
})
output$y = renderUI({
mydata = get(input$dataset)
### ?? Should ns() be applied here as well ?
selectInput(ns('columns3'), 'Y axis', names(mydata))
})
output$plot = renderPlot({
mydata = get(input$dataset)
mydata <- mydata[,c(input$columns2, input$columns3)]
plot(mydata)
})
}
ui <-
shinydashboard::dashboardPage(
skin = "yellow",
shinydashboard::dashboardHeader(
title = "Modularizing App"
),
shinydashboard::dashboardSidebar(
shinydashboard::sidebarMenu(id = "menu",
shinydashboard::menuItem('Example', tabName = 'example')
)
),
shinydashboard::dashboardBody(
shinydashboard::tabItems(
shinydashboard::tabItem("example", mod_ui("ui"))
)
)
)
server <- function(input, output) {
displayFile <- shiny::callModule(mod_server, "ui")
}
shinyApp(ui,server)