如何从uiOutput和renderUI重用selectInput?

时间:2019-07-05 08:42:27

标签: r shiny shinydashboard

我们如何重用用于selectInput下拉菜单的uiOutput 重用Server函数中的column2并在UI中显示?

Shiny renderUI selectInput returned NULL

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)
    })
  }
))

基本上是这些selectInput的图

###############

模块化版本

为什么此注释接受在Server函数中使用selectInput的值?

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, file) {

  output$x= renderUI({
    mydata =  reactive({ 
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({
    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) {
  displayFile <- shiny::callModule(mod_server, "ui")
}

shinyApp(ui,server)

1 个答案:

答案 0 :(得分:1)

我想你想要

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)