闪亮:在多个选项卡中重复使用相同的图是行不通的

Shiny: Re-using the same plot in multiple tabs is not working

我正在尝试创建一个有两个选项卡的闪亮仪表板。

第一个选项卡(称为:dashboard)显示两个图形,另一个(称为:widgets)旨在显示第一个选项卡(称为:[=13)中的第一个图形=]) 下面是 rpivottable。

问题是当我将 graphs/rpivottable 添加到第二个选项卡时,所有图表都消失了。

我发现当我删除第二个选项卡的内容时,仪表板开始显示第一个选项卡的内容。知道为什么会发生这种情况以及如何解决它吗?

示例代码:

library(shiny)
library(shinydashboard)
library(rhandsontable)
library(writexl)
library(readxl)
library(stringr)
library(ggplot2)
library(rpivotTable)

ui <- dashboardPage(skin = 'green',

  dashboardHeader( title = "Test", titleWidth = 280),
  dashboardSidebar(width = 280,  
  sidebarMenu(
  menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
  menuItem("Pivot", tabName = "widgets", icon = icon("th"))

  )),

  dashboardBody(
  tabItems(
  # First tab content
  tabItem(tabName = "dashboard",
  fluidRow(
  column(5, 'Mpg Table') ), 
  br(), 
  fluidRow(
  rHandsontableOutput ('mpg')),
  br(),
  fluidRow(
  column(5,'mtcars Summary')),
  br(),
  fluidRow(
  column(3),column(6, tableOutput ('mtcars')),column(3))

  ),
  # Second tab content
  tabItem(tabName = "widgets",
  fluidRow(
  column(5,'Mpg table')),

  br(),

  fluidRow(
  rHandsontableOutput ('mpg')),

  br(),

  fluidRow(
  rpivotTableOutput('pivot')  
  )

  )
    )
      )
        )

server <- shinyServer(function(input, output) {

  #mpg
  output$mpg <- renderRHandsontable ({ rhandsontable({
   mpg[1,]  })
    })

  #mtcars

  output$mtcars <-renderTable ({  
   head(mtcars)})

 # pivot table

  output$pivot <- renderRpivotTable({ rpivotTable(mtcars)})


})

shinyApp(ui, server)

简单示例:

library(shiny)
library(shinydashboard)
ui <- dashboardPage(skin = 'green',

                    dashboardHeader( title = "Test", titleWidth = 280),
                    dashboardSidebar(width = 280,  
                                     sidebarMenu(
                                       menuItem(text = "Dashboard", tabName = "dashboard", icon = icon("dashboard")),
                                       menuItem(text = "Pivot", tabName = "widgets", icon = icon("th"))

                                     )),

                    dashboardBody(
                      tabItems(
                        # First tab content
                        tabItem(tabName = "dashboard",
                                fluidRow(
                                  column(5, 'Mpg Table') ), 
                                br(), 

                                fluidRow(column(width = 12, plotOutput("plot1")
                                                )
                                                        )),
                        # Second tab content
                        tabItem(tabName = "widgets",
                                fluidRow(
                                  column(5,'Mpg table')),

                                br(),

                                fluidRow(column(width = 6, plotOutput("plot2")),
                                        column(width = 6, plotOutput("plot3"))
                                ),
                                br(),
                                fluidRow(column(width = 12, plotOutput("plot4"))
                                         )
                      )
                    )
)
)

server <- shinyServer(function(input, output) {

  output$plot1 <- renderPlot({
    hist(rnorm(1000))
  })
  output$plot2 <- renderPlot({
    plot(rnorm(1000), rnorm(1000))
  })
  output$plot3 <- renderPlot({
    boxplot(rnorm(100))
  })
  output$plot4 <- renderPlot({
    ts.plot(rnorm(100))
  })
})

shinyApp(ui, server)

您不能重复使用同一个 ID 来绑定多个输出 (Look here)。因此,一种选择是在两个选项卡中为 mpg table 提供唯一 ID,并在服务器中两次呈现 table 输出:output$mpg1 <- output$mpg2 <- renderRHandsontable ({}).

工作示例:

library(shiny)
library(shinydashboard)
library(rhandsontable)
library(writexl)
library(readxl)
library(stringr)
library(ggplot2)
library(rpivotTable)

ui <- dashboardPage(skin = 'green',
                    dashboardHeader(title = "Test", titleWidth = 280),
                    dashboardSidebar(width = 280,  
                                     sidebarMenu(
                                       menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
                                       menuItem("Pivot", tabName = "widgets", icon = icon("th"))
                                     )),
                    dashboardBody(
                      tabItems(
                        # First tab content
                        tabItem(tabName = "dashboard",
                                fluidRow(
                                  column(5, 'Mpg Table') ), 
                                br(), 
                                fluidRow(
                                  rHandsontableOutput ('mpg1')),
                                br(),
                                fluidRow(
                                  column(5, 'mtcars Summary')),
                                br(),
                                fluidRow(
                                  column(3),
                                  column(6, tableOutput ('mtcars')),column(3))
                        ),
                        # Second tab content
                        tabItem(tabName = "widgets",
                                fluidRow(
                                  column(5,'Mpg table')),
                                br(),
                                fluidRow(
                                  rHandsontableOutput ('mpg2')),
                                br(),
                                fluidRow(
                                  rpivotTableOutput('pivot'))
                                )
                        )
                      )
                    )

server <- shinyServer(function(input, output) {
  #mpg
  output$mpg1 <-output$mpg2<- renderRHandsontable ({
    rhandsontable({
      mpg[1,]})
    })
  #mtcars
  output$mtcars <-renderTable ({
    head(mtcars)})
  # pivot table
  output$pivot <- renderRpivotTable({rpivotTable(mtcars)})
})

shinyApp(ui, server)