在 R 的多个地方使用闪亮的 plotOutput [英] Using shiny plotOutput in multiple places in R

查看:56
本文介绍了在 R 的多个地方使用闪亮的 plotOutput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在多个子菜单项的 R 中使用 plotOutput.但是,我相信 plotOutput 不可能在具有相同 id 的多个地方使用.如果可能的话,请帮助我.附上快照以供参考.

I am trying to use plotOutput in R shiny in multiple sub-menu items. However, I believe it is not possible for plotOutput to be used at multiple places with same id. Please help me if it is possible somehow. attaching the snapshot for reference.

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
id = "tabs",
  menuItem("Charts", icon = icon("bar-chart-o"),
           menuSubItem("Sub-item 1", tabName = "subitem1"),
           menuSubItem("Sub-item 2", tabName = "subitem2")
  ))),
dashboardBody(
tabItems(tabItem("subitem1", 3),
         tabItem("subitem2", plotOutput("brand_selector")))
 ))
server <- shinyServer(function(input, output) {
output$brand_selector <-  renderPlot({
plot(iris$Sepal.Length)
}) 
})
shinyApp(ui = ui, server = server)

更新

library(shiny)
library(shinydashboard)
submenuUI <- function(id) {
ns <- NS(id)
# return a list of tags
tagList(
column(2,offset = 0, style='padding:1px;', 

selectInput(ns("select1"),"select1",c("A1","A2","A3","A2","A1","A3","A1"))),
column(2,offset = 0, style='padding:1px;', 

selectInput(ns("select2"),"select2",c("B3","B4","B5","B3","B6","B2","B3")))
)
}
submenu <- function(input,output,session){}
ui <- dashboardPage(
dashboardHeader(), 
dashboardSidebar(
sidebarMenu(
id = "tabs",
  menuItem("Charts", icon = icon("bar-chart-o"),
           menuSubItem("Sub-item 1", tabName = "subitem1"),
           menuSubItem("Sub-item 2", tabName = "subitem2"),
           menuSubItem("Sub-item 3", tabName = "subitem3"),
           menuSubItem("Sub-item 4", tabName = "subitem4")
  ))),
 dashboardBody(
 tabItems(tabItem("subitem1", submenuUI('submenu1')),
         tabItem("subitem2", submenuUI('submenu2')),
         tabItem("subitem3", submenuUI('submenu3')),
         tabItem("subitem4", "Sub-item 2 tab content"))))
  server <- function(input, output, session) {
  observeEvent(input$Select1,{
  updateSelectInput(session,'Select2',
                  choices= input$select2[input$Select1 == "A1"] )
   }) 
   callModule(submenu, "submenu")
   }
   shinyApp(ui, server)

请帮助我根据前一个 selectInput 的输入更新第二个 selectInput.

Please help me to update the second selectInput based on input from the previous selectInput.

推荐答案

这是用于多重绘图的模块化代码:与 selectInput one 非常相似.

This is the modularized code for multiple plotting: Very similar to the selectInput one.

library(shiny)
library(shinydashboard)

plotopUI <- function(id) {
  ns <- NS(id)


  # return a list of tags
  tagList(
   plotOutput(ns('plt'))
  )


}



plotop <- function(input,output,session){
  ns <- session$ns

  output$plt <- renderPlot({
    plot(iris$Sepal.Length)
  })
}


ui <- dashboardPage(
  dashboardHeader(), 
  dashboardSidebar(
    sidebarMenu(

      id = "tabs",
      menuItem("Charts", icon = icon("bar-chart-o"),
               menuSubItem("Sub-item 1", tabName = "subitem1"),
               menuSubItem("Sub-item 2", tabName = "subitem2")
      ))),
  dashboardBody(
    tabItems(tabItem("subitem1", plotopUI('plt1')),
             tabItem("subitem2", plotopUI('plt2')))))

server <- function(input, output, session) {
  callModule(plotop, "plt1")
  callModule(plotop, "plt2")
}
shinyApp(ui, server)

这篇关于在 R 的多个地方使用闪亮的 plotOutput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆