Я думаю, что вы хотите:
runApp(list(
ui = bootstrapPage(
selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris')),
uiOutput('x'),
uiOutput('y'),
plotOutput('plot')
),
server = function(input, output){
mydata <- reactive({
get(input$dataset)
})
output$x= renderUI({
selectInput('columns2', 'X axis', names(mydata()))
})
output$y = renderUI({
selectInput('columns3', 'Y axis', names(mydata()))
})
output$plot = renderPlot({
req(input$columns2, input$columns3)
x <- input$columns2
y <- input$columns3
if(all(c(x,y) %in% names(mydata()))){ # to avoid an error when the user changes the dataset
plot(mydata()[[x]], mydata()[[y]], xlab = x, ylab = y)
}
})
}
))
Модульное приложение
Основной причиной, по которой модульное приложение не работает, является отсутствие ns
в selectInput
:
selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris'))
заменить на
selectInput(ns('dataset'), 'Choose Dataset', c('mtcars', 'iris'))
Полный код:
library(shiny)
mod_ui <- function(id){
ns <- shiny::NS(id)
shiny::tagList(
selectInput(ns('dataset'), 'Choose Dataset', c('mtcars', 'iris')),
uiOutput(ns('x')),
uiOutput(ns('y')),
plotOutput(ns('plot'))
)
}
mod_server = function(input, output, session, file) {
ns <- session$ns
mydata = reactive({
get(input$dataset)
})
output$x = renderUI({
selectInput(ns('columns2'), 'X axis', names(mydata()))
})
output$y = renderUI({
selectInput(ns('columns3'), 'Y axis', names(mydata()))
})
output$plot = renderPlot({
req(input$columns2, input$columns3)
x <- input$columns2
y <- input$columns3
if(all(c(x,y) %in% names(mydata()))){ # to avoid an error when the user changes the dataset
plot(mydata()[[x]], mydata()[[y]], xlab = x, ylab = y)
}
})
}
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, session) {
displayFile <- shiny::callModule(mod_server, "ui")
}
shinyApp(ui,server)