2

我正在制作一个Shiny嵌入tabPanelsnavbarPage. 在每个tabPanel中,我生成一系列图像。当从一个面板切换到另一个面板时,其中一个面板中加载的图像会消失。

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

这是一个可重现的示例:

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

## ui ----

# Image list
imgs <- list(
    stackoverflow =
        "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)

它产生了什么: 在此处输入图像描述

4

1 回答 1

2

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

请检查以下内容:

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

## ui ----

# Image list
imgs <- list(
  stackoverflow =
    "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)
于 2021-01-22T13:55:26.957 回答