R 闪亮:切换 tabpanel() 时 slickROutput 消失

R shiny: slickROutput disappears when switching tabpanel()

我正在制作一个 Shiny 应用,其中 tabPanels 嵌入 navbarPage。 在每个 tabPanel 中,我生成一系列图像。从一个面板切换到另一个面板时,其中一个面板中加载的图像消失了。

我必须手动“刷新”页面才能再次看到它。该问题似乎与 here 发布的问题相似,但我无法真正将 tabPanels 子集化,即使我将 $idvalue 给了他们。

这是一个可重现的例子:

library(shiny)
library(shinythemes)
library(slickR)

## ui ----

# Image list
imgs <- list(
    Whosebug =
        "https://upload.wikimedia.org/wikipedia/fr/9/95/Stack_Overflow_website_logo.png",
    stackexchange =
        "https://upload.wikimedia.org/wikipedia/commons/6/6f/Stack_Exchange_Logo.png"
)

ui <- navbarPage(title = div(
    HTML('<span style="font-size:180%;color:white;font-weight:bold;"> Navbarpage</span></a>'),
    tags$style(style = 'position:absolute; right:42px;'),
    tags$style(HTML("#panel1{font-size: 25px}")),
    tags$style(HTML("#panel2{font-size: 25px}")),
    tags$style(HTML("#panel_about{font-size: 25px}"))
),
theme = shinytheme("flatly"),
windowTitle = "Navbarpage",
id = "navbar",

## First tabpanel ----
tabPanel(h1(id = "panel1", "Panel 1"), value = 1, fluid = TRUE,
         fluidRow(column(4,
                         selectInput("img_list", "Image list",
                                     choices = imgs,
                                     selected = imgs[1])),
                  column(8,
                         slickROutput("plot_panel1"))),
),
tabPanel(h1(id = "panel2", "Panel 2"), value = 2, fluid = TRUE,
         fluidRow(column(4,
                         selectInput("img_list", "Image list",
                                     choices = imgs,
                                     selected = imgs[1])),
                  column(8,
                         slickROutput("plot_panel2"))),
)
) # closes navbarpage

## server ----
server <- function(input, output, session){

    observe({
        output$plot_panel1 <- renderSlickR({
            slick1 <- slick_list(slick_div(
                input$img_list,
                css = htmltools::css(width = "100%", margin.left = "auto",
                                     margin.right = "auto"),
                type = "img", links = NULL))
            slickR(slick1)
        })
    })
    
    observe({
        output$plot_panel2 <- renderSlickR({
            slick2 <- slick_list(slick_div(
                input$img_list,
                css = htmltools::css(width = "100%", margin.left = "auto",
                                     margin.right = "auto"),
                type = "img", links = NULL))
            slickR(slick2)
        })
    })
}

shinyApp(ui, server)

及其产生的结果:

当有多个slickR对象要渲染时,您需要为每个对象使用唯一的slideId。此外,您不应将 render* 函数包装在 observe 中,并为两个 selectInput 使用相同的 id

请检查以下内容:

library(shiny)
library(shinythemes)
library(slickR)

## ui ----

# Image list
imgs <- list(
  Whosebug =
    "https://upload.wikimedia.org/wikipedia/fr/9/95/Stack_Overflow_website_logo.png",
  stackexchange =
    "https://upload.wikimedia.org/wikipedia/commons/6/6f/Stack_Exchange_Logo.png"
)

ui <- navbarPage(title = div(
  HTML('<span style="font-size:180%;color:white;font-weight:bold;"> Navbarpage</span></a>'),
  tags$style(style = 'position:absolute; right:42px;'),
  tags$style(HTML("#panel1{font-size: 25px}")),
  tags$style(HTML("#panel2{font-size: 25px}")),
  tags$style(HTML("#panel_about{font-size: 25px}"))
),
theme = shinytheme("flatly"),
windowTitle = "Navbarpage",
id = "navbar",

## First tabpanel ----
tabPanel(h1(id = "panel1", "Panel 1"), value = 1, fluid = TRUE,
         fluidRow(column(4,
                         selectInput("img_list1", "Image list",
                                     choices = imgs,
                                     selected = imgs[1])),
                  column(8,
                         slickROutput("plot_panel1"))),
),
tabPanel(h1(id = "panel2", "Panel 2"), value = 2, fluid = TRUE,
         fluidRow(column(4,
                         selectInput("img_list2", "Image list",
                                     choices = imgs,
                                     selected = imgs[1])),
                  column(8,
                         slickROutput("plot_panel2"))),
)
) # closes navbarpage

## server ----
server <- function(input, output, session){
  
    output$plot_panel1 <- renderSlickR({
      slick1 <- slick_list(slick_div(
        input$img_list1,
        css = htmltools::css(width = "100%", margin.left = "auto",
                             margin.right = "auto"),
        type = "img", links = NULL))
      slickR(slick1, slideId = "slide1")
    })
  
    output$plot_panel2 <- renderSlickR({
      slick2 <- slick_list(slick_div(
        input$img_list2,
        css = htmltools::css(width = "100%", margin.left = "auto",
                             margin.right = "auto"),
        type = "img", links = NULL))
      slickR(slick2, slideId = "slide2")
    })
}

shinyApp(ui, server)