将图显示到特定选项卡

时间:2019-08-26 10:14:48

标签: r shiny

我用边栏中的radioButtons()小部件创建了一个闪亮的应用程序。

此小部件具有3个选项"Home","Sector A","Sector B"。当用户选择一个选项时,他将导航到由一些tabsetPanel()组成的特定tabPanels

问题是我希望仅在相对的tabPanel中显示所创建的图和表格,但是您将看到它们几乎显示在所有位置。更具体地说,tabPanel "Two Bars only here"应该仅在此处显示2条。其余逻辑相同。表格应显示在相应的"Constituents"标签中,而ohlc图表应显示在相应的"Index"标签中。

我不明白为什么我使用的if条件不会限制这种行为。

#ui.r
library(shiny)
library(shinythemes)
library(plotly)
library(quantmod)
#ui.r
ui <- fluidPage(
  theme=shinytheme("slate") ,
  # App title ----
  titlePanel("Tabsets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      uiOutput("rad")
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      uiOutput("tabers"),
      plotlyOutput("bars"),
      plotlyOutput("bars2"),
      plotlyOutput("index1"),
      plotlyOutput("index2")
    )
  )
)
#server.r
library(shiny)
library(shinythemes)
library(plotly)
library(quantmod)
server = function(input, output) {

  output$rad<-renderUI({
    radioButtons("radio", label = "",
                 choices = list("Home" = 1, "Sector A" = 2, "Sector B" = 3), 
                 selected = 1)
  })

  output$tabers<-renderUI({
    # Left last else in here but should not get called as is
    if(input$radio==1){
      tabsetPanel(
        id="tabC",
        type = "tabs",
        tabPanel("Global"),
        tabPanel("Two Bars only here",
                 output$bars<-renderPlotly({
                   p <- plot_ly(
                     x = c("giraffes", "orangutans", "monkeys"),
                     y = c(20, 14, 23),
                     name = "SF Zoo",
                     type = "bar"
                   )
                 }),
                 output$bars2<-renderPlotly({
                   p <- plot_ly(
                     x = c("gir", "ora", "mon"),
                     y = c(20, 14, 23),
                     name = "SF Zoo",
                     type = "bar"
                   )
                 }))
      ) 
    }
    else if(input$radio==2){
      tabsetPanel(
        id="tabB",
        type = "tabs",
        tabPanel("Constituents Table Iris only here",
                 output$table <- DT::renderDataTable({
                   datatable(
                     iris)
                 })),
        tabPanel("Index1 only here",
                 output$index1<-renderPlotly({


                   getSymbols("AAPL",src='yahoo')

                   df <- data.frame(Date=index(AAPL),coredata(AAPL))
                   df <- tail(df, 30)

                   p <- df %>%
                     plot_ly(x = ~Date, type="ohlc",
                             open = ~AAPL.Open, close = ~AAPL.Close,
                             high = ~AAPL.High, low = ~AAPL.Low) %>%
                     layout(title = "Basic OHLC Chart")
                 }))
      )
    }
    else if(input$radio==3){
      tabsetPanel(
        id="tabA",
        type = "tabs",
        tabPanel("Constituents Mtcars only here",
                 output$table <- DT::renderDataTable({
                   datatable(
                     mtcars)
                 })),
        tabPanel("Index2 only here",
                 output$index1<-renderPlotly({


                   getSymbols("AAPL",src='yahoo')

                   df <- data.frame(Date=index(AAPL),coredata(AAPL))
                   df <- tail(df, 30)

                   p <- df %>%
                     plot_ly(x = ~Date, type="ohlc",
                             open = ~AAPL.Open, close = ~AAPL.Close,
                             high = ~AAPL.High, low = ~AAPL.Low) %>%
                     layout(title = "Basic OHLC Chart")
                 }))
      ) 
    }

  })
}

1 个答案:

答案 0 :(得分:1)

使用uiserver函数可以分离界面和应用程序的工作方式。您所有的输出都只是占位符,由server填充,我担心这是造成混乱的原因。例如,您的单选按钮完全是静态的,不需要通过server进行设置。

您可以轻松绘制整个UI,并确保各个图仅显示在各个面板中。

为初学者尝试一下:

ui <- fluidPage(
  theme=shinytheme("slate") ,
  # App title ----
  titlePanel("Tabsets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      radioButtons("radio", label = "",
                 choices = list("Home" = 1, "Sector A" = 2, "Sector B" = 3), 
                 selected = 1)
    ),

   ...
)

对于您的面板,uiOutput应该在renderUI中定义,而不是与选项卡集分开:

 output$tabers<-renderUI({
    # Left last else in here but should not get called as is
    if(input$radio==1){
      tabsetPanel(
        id="tabC",
        type = "tabs",
        tabPanel("Global"),
        tabPanel("Two Bars only here",
          plotlyOutput('bars'),
          plotlyOutput('bars2')
        )
      )
    } elseif ...

然后可以在server下分别定义各个renderPlotly调用,而不是嵌套在另一个renderUI中。

另一种方法可能是具有4-5个固定的输出,然后隐藏未使用的输出。为此,请参见shinyjs软件包。