【问题标题】:Dashboard display issues / layout / plot / reactive / UI仪表板显示问题 / 布局 / 绘图 / 反应式 / UI
【发布时间】:2018-09-21 03:56:50
【问题描述】:

我正在尝试用 3 页(dtable、sales、forecast)制作一个闪亮的仪表板,到目前为止只有第一个 - 数据表显示。

销售选项卡显示所有按钮,但没有显示图表。预测选项卡也是如此。另外,我该如何调整它,所以所有按钮都以统一的方式在左侧,然后是图表?

我的方法正确吗?还是我应该使用条件面板?不能对这里的问题做出正面或反面。 我现在想让所有 3 个选项卡都响应它们各自的图。 任何帮助,将不胜感激。谢谢

   wmp <- read.csv("ABC.csv",stringsAsFactors = FALSE)

UI 基本布局

     ui <- dashboardPage(skin=c("blue"),
    dashboardHeader(title = "Layout"),
     dashboardSidebar(id="",
                   sidebarMenu(menuItem("Data Table", tabName = "Data_Table", icon = icon("table")),
                              menuItem("Sales Performance", tabName = "Sales_Performance",icon = icon("bar-chart-o")),
                              menuItem("Forecast",tabName = "Fore_Cast",icon = icon("bolt"))
                              )
                   ),
  #Main Body
  dashboardBody(tabItems(
                tabItem(tabName = "Data_Table",
                        box(title = "Data Table ",width=50,
                        column(2,
                       selectInput("PInput","Product:",c("All",unique(wmp$Product)))),
                        column(2,
                        selectInput("CInput","Category:",c("All",unique(wmp$Category)))),
                        column(4,
                        selectizeInput("PSCInput","Product Sub Category:",c("All",unique(wmp$sub.category))))), DT::dataTableOutput("table")),
                tabItem(tabName = "Sales_Performance",
                        sidebarMenu(fluidRow(title= "Sales Breakdown",width = 500,
                        column(4,    
                        sliderInput("monthInput","Month",min=1,max = 12,step = 1,c(25,45)),br()),
                        column(4,
                        selectizeInput("weekInput","Week",choices=unique(wmp$week),br())),
                        column(7,
                        selectizeInput("wdayInput","Week day",choices=unique(wmp$wday))),
                        column(4,
                        checkboxGroupInput("yearInput","Year",choices = unique(wmp$year))))),plotOutput("plot1")
                        #br(),br(),
                #tableOutput("Results")
                ),
                tabItem(tabName = "Fore_cast",width=400,
                        #UI buttons/options to be added later,
                        title="Plot2",plotOutput("plot2"))
                      )))

服务器反应部分

server <- function(input,output) {
   #For Data table
       output$table <- DT::renderDataTable(DT::datatable({
data1 <- wmp
if (input$PInput != "All") {
  data <- data1[data1$Product == input$PInput,] }
if (input$CInput != "All") {
  data <- data1[data1$Category == input$CInput,] }
if (input$PSCInput != "All") {
  data1 <- data1[data1$sub.category == input$PSCInput,] }
data1
}))
   #For interactive plot
    filtered <- reactive({ 
      if (is.null(input$yearInput)) {
      return(NULL)
      }    
    wmp %>%  filter(
    year == input$yearInput,
    week == input$weekInput,
  wday == input$wdayInput,
  month >= input$monthInput[1],
  month <= input$monthInput[2]
)})

    output$plot1 <- renderPlot({

wmp2 <- data.frame(filtered())
avg <- mean((wmp2$Amount))


  plotwmp2 <- ggplot(filtered(),aes(Date,Amount,group=filtered()$year,color=filtered()$year)) + scale_x_date() + geom_point() + geom_line() + xlab("Yearly Span") + theme_grey() + ggtitle("Weekly breakdown") + geom_hline(yintercept=avg,linetype="dashed", color = "red")


plotwmp2
})


   #For Time series, sample plot.
   output$plot2 <- renderPlot({ 

plot.ts(wmp$Amount)})}


   #Final App
   shinyApp(ui=ui, server=server)

【问题讨论】:

    标签: r user-interface shiny dashboard shinydashboard


    【解决方案1】:

    如果您想在左侧有按钮并在右侧进行绘图,我认为您应该将页面分为两部分。像这样的:

    tabItem(tabName = "Sales_Performance",
            fluidRow(title= "Sales Breakdown",width = 500,
               column(4,    
                      sliderInput("monthInput","Month",min=1,max = 12,step = 1,c(25,45)),
                      br(),
                      selectizeInput("weekInput","Week",choices=unique(wmp$week)),
                      br(),
                      selectizeInput("wdayInput","Week day",choices=unique(wmp$wday)),
                      br(),
                      checkboxGroupInput("yearInput","Year",choices = unique(wmp$year))
                ),
               column(8,
                      plotOutput("plot1")
               )
            )
    )
    

    你也可以把它放在boxes

    如果您提供数据样本,绘图问题将更容易理解和解决。据我判断,plot2 的问题不应该是这样。 在结束括号“})”之前的 plot1 代码部分“plotwmp2”的末尾有额外的字符。

    UPD。 Plot2 没有出现,因为您在 menuItem 和 tabItem 中有不同的标签 - 分别为“Fore_Cast”和“Fore_cast”。

    关于情节1。当我运行应用程序时,我看到:“错误:找不到对象'日期'”,所以我查看了代码 output$plot1。你用这个: aes(日期,金额,group=filtered()$year,color=filtered()$year) 我将其更正为filtered()$Date,filtered()$Amount 并出现了情节。但只有部分

    scale_x_date()
    

    被评论。如果我使用“scale_x_date()”,则会出现错误:无效输入:date_trans 仅适用于 Date 类的对象。也许您应该通过 mutate 方法来转换此列。

    【讨论】:

    • 我试了一下,现在“销售”选项卡看起来好多了。按钮位于左侧,图形位于侧面。但我似乎无法在 ggplot 中为 plotwmp2 行找到额外的字符 :( 请看一下我现在附上的数据样本并告诉我。关于第三个选项卡 plot2 你还有其他输入吗?
    • @9minus4,我已经更新了我的答案。我运行了这个脚本,除了 scale_x_date() 方法外它正常工作。
    • 我知道我在哪里出错了,^^ 感谢您更新您的答案。我已经进行了必要的更改,它们现在都响应了! **但是使用 plotwmp2 我得到一个新错误:“警告:match.arg 中的错误:'arg' 必须为 NULL 或字符向量”124:match.arg 123:过滤器 121:withVisible 120:freduce 118:eval 117: eval 116: withVisible 115: %>% 114: [#18] 103: 过滤 102: renderPlot [#28] 92: 81: plotObj 80: origRenderFunc 79: output$plot1跨度>
    • 找到match.arg错误的解决方法,转换日期列后就消失了。它不是“日期”格式。感谢您的所有投入! @奥尔加:D
    猜你喜欢
    • 2021-11-16
    • 2017-12-19
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    相关资源
    最近更新 更多