简体   繁体   English

闪亮-不同选项卡中的不同地块

[英]Shiny - different plots in different tabs

I have what could appear as a very simple problem. 我有一个看起来很简单的问题。 I'd like to display different plots in different tabs. 我想在不同的选项卡中显示不同的图。 For this I have some code already where I use output$Sidebar and output$TABUI for the content of the tabs. 为此,我已经有一些代码,在其中将output$Sidebaroutput$TABUI用于选项卡的内容。

I do wish to use some controls for the plots, but all the controls being identical, I need them just below the different tabs, as I don't want to replicate them across and having them appearing within each tab. 我确实希望对绘图使用某些控件,但是所有控件都是相同的,我需要它们位于不同选项卡的下面,因为我不想在所有选项卡之间复制它们并使它们出现在每个选项卡中。

I must miss something in my code because nothing show up in the dashboardbody. 我必须错过代码中的某些内容,因为仪表板主体中什么都没有显示。 The tabs are created just fine (as it seems) so are my controls, just below them. 选项卡的创建就很好(看起来很不错),我的控件也在它们的下面。 My data is read through (can see this in the console) and I can work with the controls, but nothing appears in the body. 我的数据被读取(可以在控制台中看到),并且可以使用控件,但正文中什么也没有出现。

I've tried to modify my code (much longer) to make a minimal example, as follow. 我试图修改我的代码(更长)来做一个最小的示例,如下所示。

Edit : If both the sidebarmenu and tabitems are in the UI.R, then everything on the ui gets compiled correctly, except that my data, which are being loaded at the beginning of the SERVER.R are not loaded. 编辑:如果sidebarmenutabitems都在UI.R中,则ui上的所有内容都会正确编译,除了不加载正在SERVER.R开头加载的我的数据。 It seems as if server.R is not even ran. 似乎server.R甚至没有运行。 If I define the sidebarmenu and thetabitems from the server.R, then the data are loaded, but only my controls are displayed, sidebarmenu and body are not displayed. 如果我从server.R定义了边栏菜单和选项卡,则将加载数据,但仅显示我的控件,不显示边栏菜单和主体。 I can't understand this behavior either. 我也无法理解这种行为。 If I leave tabitems in the UI.R and sidebarmenu from server.R, it does not load the data either. 如果我将tabitems留在server.R的UI.R和sidebarmenu中,它也不会加载数据。 The app just seats there and nothing happens. 该应用程序就在那儿,没有任何反应。 If someone think they might know why, I'd be glad to have an explanation. 如果有人认为他们可能知道为什么,那么我很乐意提供解释。 Thank you. 谢谢。

ui.R : ui.R:

library(shiny)
library(shinydashboard)

body <- dashboardBody(
  tags$head(
    tags$link(
      rel = "stylesheet",
      type = "text/css",
      href = "css/custom.css"
    )
  ),
  uiOutput("TABUI")

)

sidebar <- dashboardSidebar(
  width = 350,
  uiOutput("Sidebar")
)

header <- dashboardHeader(
  title = "Dashboard",
  titleWidth = 350,

  tags$li(
    class = "dropdown",
    img(
      src = 'img/General_Logo.png',
      style =  'margin-right:150px; margin-top:21px')
  )
)

dashboardPage(
  header,
  sidebar,
  body
)

Server.R Server.R

library(ggplot2)
library(dplyr)
library(RColorBrewer)
library(XLConnect)
library(htmlTable)
library(plotly)

# Loading data  -----------------------------------------------------
raw_data <- read.csv("file.csv")

# Server function ---------------------------------------------------
shinyServer(function(input, output) {

  # Tabs and content

  ntabs <- 4
  tabnames <- paste0("tab ", 1:ntabs)

  output$Sidebar <- renderUI({
    Menus <- vector("list", ntabs + 2)
    for (i in 1:ntabs){
             Menus[[i]] <- menuItem(tabnames[i], tabName = tabnames[i], icon = icon("dashboard"))
    }
    # Controls to appear below tabs
    Menus[[ntabs + 1]] <- selectInput("dpt", "Departments :",
                c("dpt 1" = "DPT1",
                  "dpt 2" = "DPT2",
                  "dpt 3" = "DPT3"),
                multiple = TRUE,
                selectize = TRUE)

    Menus[[ntabs + 2]] <- uiOutput("bottleneck")
    Menus[[ntabs + 3]] <- uiOutput("daterange")
    Menus[[ntabs + 4]] <- submitButton()

    do.call(function(...) sidebarMenu(id = 'sidebarMenu', ...), Menus)
  })

  # content of each tab
  output$TABUI <- renderUI({
    Tabs <- vector("list", ntabs)
    Tabs[[1]] <- tabItem(tabName = tabnames[1],
                         # fluidRow(box(h3("foo.")))
                         fluidRow(
                           box(
                             plotOutput("plot_1")
                           )
                         )
    )
    Tabs[[2]] <- tabItem(tabName = tabnames[2],
                         "Tab 2 Stuff")
    Tabs[[3]] <- tabItem(tabName = tabnames[3],
                         "Tab 3 Stuff")
    Tabs[[4]] <- tabItem(tabName = tabnames[4],
                         "Tab 4 Stuff")
    do.call(tabItems, Tabs)
  })

  formulaText <- reactive({

    if (is.null(data.r())) {
      return("some text")
    }

    paste0(as.character(input$daterange[1]), " to ", as.character(input$daterange[2]), " - blah blah")
  })

  output$bottleneck <- renderUI({
    selectInput('bottleneck', HTML('<font color=\"black\"> Bottlenecks : </font>'), c(Choose = '', raw_data[raw_data$is_bottleneck == 1 & !is.na(raw_data$Sort.field) & raw_data$Cost.Center %in% input$dpt,]$Sort.field %>%  unique() %>% sort()), selectize = TRUE)
  })

  output$daterange <- renderUI({
    dateRangeInput(inputId = 'daterange', 
                   label = HTML('<font color=\"black\"> Select period : </font>'), 
                   min = min(raw_data$Completn.date) , 
                   start = min(raw_data$Completn.date) ,
                   max = max(raw_data$Completn.date), 
                   end = max(raw_data$Completn.date))
  })

  data.r = reactive({

    if (is.null(input$dpt)) {
      return(NULL)
    }  

    ifelse(input$bottleneck == "", a <- raw_data %>% filter(Completn.date >=     input$daterange[1],
                                                                            Completn.date <= input$daterange[2]),
           a <- raw_data %>% filter(Completn.date >= input$daterange[1],
                                    Completn.date <= input$daterange[2],
                                    Sort.field %in% input$bottleneck))

    return(a)
  })

  output$table_ranking <- renderHtmlTableWidget({

    if (is.null(data.r())) {
      return()
    }

    ranking <- read.csv("ranking.csv", header = TRUE)
    htmlTableWidget(ranking)
  })

  output$caption <- renderText({
    formulaText()
  })

  output$plot_1 <- renderPlot({

    if (is.null(data.r())) {
      return()
    }

    current_data <- data.r()

    p0 <- current_data %>% 
      ggplot(aes(x = x1, y = y1)) +
      geom_point()

    p0

  })

  output$plot_2 <- renderPlot({

    if (is.null(data.r())) {
      return()
    }

    current_data <- data.r()

    p0 <- current_data %>% 
      ggplot(aes(x = x2, y = y2)) +
      geom_point()

    p0

  })
})

This is a failed attempt to replicate what was suggested here . 这是重复此处建议的尝试失败。

Thanks ahead of time for looking into this. 提前感谢您对此进行调查。

I finally got to find the answer. 我终于找到答案了。

I've had several reactive element duplicated across different tabs. 我在不同的选项卡中重复了几个反应性元素。 For some reason Shiny does not like this. 由于某些原因,Shiny不喜欢这样。 Once I've created different reactive strings (in my case) then everything was fine (tabitems with renderUI in server.r, as well as sidebarmenu). 一旦创建了不同的反应性字符串(以我为例),那么一切就很好了(在server.r和sidebarmenu中带有renderUI的选项)。

Weird but anyway. 很奇怪,但无论如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM