【问题标题】:How to reuse the selectInput from uiOutput and renderUI?如何重用 uiOutput 和 renderUI 中的 selectInput?
【发布时间】:2019-11-15 21:11:42
【问题描述】:

我们如何重用用于 selectInput 下拉菜单的 uiOutput 重用 Server 函数中的 columns2 并在 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的情节。

模块化版本

为什么这个注释接受在服务器函数中使用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)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案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)
          }
        })
      }
    ))
    

    模块化应用

    模块化应用无法运行的主要原因是selectInput 中缺少ns

        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)
    

    【讨论】:

    • 请在您的代码适配中找到有问题的更新请求
    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 2015-08-03
    • 2023-01-30
    • 2015-10-27
    • 2015-11-27
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多