Shiny: повторное использование одного и того же графика в нескольких вкладках не работает - PullRequest
0 голосов
/ 24 октября 2018

Я пытаюсь создать блестящую информационную панель с двумя вкладками.

Первая вкладка (называемая: dashboard) показывает два графика, а другая (называемая: widgets) предназначена для отображения первого графика из первой вкладки (называемой: mpg) и ниже.это основной вопрос.

Проблема в том, что когда я добавляю 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)

Ответы [ 2 ]

0 голосов
/ 24 октября 2018

Вы не можете повторно использовать один и тот же идентификатор для привязки нескольких выходов ( Смотрите здесь ).Таким образом, одним из вариантов будет присвоение таблице mpg уникального идентификатора на обеих вкладках и отображение таблицы дважды на сервере с помощью (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)
0 голосов
/ 24 октября 2018

простой пример:

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