【问题标题】:Ggplot in Shiny Dashboard闪亮仪表板中的 Ggplot
【发布时间】:2019-06-20 13:55:29
【问题描述】:

尝试通过 FYQuarter 和动态图表创建带有滑块的仪表板。在 Shiny 查询下运行时出错

错误:意外的 '}' in: " geom_text(aes(label = Freq), position = position_dodge(0),vjust = -1) + 主题(panel.grid.major = element_blank(), panel.grid.minor = element_blank())})}"

Data-File2

  structure(list(Quater = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
  3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
  8L, 9L, 9L, 9L, 10L, 10L, 10L), .Label = c("Fy17Q1", "Fy17Q2", 
  "Fy17Q3", "Fy17Q4", "Fy18Q1", "Fy18Q2", "Fy18Q3", "Fy18Q4", "Fy19Q1", 
  "Fy19Q2"), class = "factor"), RiskTierDesc = structure(c(1L, 
  2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
  3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Above Normal", 
  "High", "Normal"), class = "factor"), Freq = c(519L, 63L, 1514L, 
  563L, 87L, 1662L, 643L, 81L, 1618L, 562L, 69L, 1524L, 555L, 61L, 
  1609L, 622L, 52L, 2090L, 800L, 86L, 2052L, 681L, 66L, 1811L, 
  622L, 57L, 2317L, 344L, 14L, 1537L)), .Names = c("Quater", "RiskTierDesc", 
  "Freq"), class = "data.frame", row.names = c(NA, -30L))
library(shiny)
library(shinydashboard)
library(ggplot2)
library(shinyWidgets)
ui <- dashboardPage(
 dashboardHeader(title = "Basic Dashboard"),

 dashboardSidebar(
   sidebarMenu(sliderTextInput("Quater","Select Quarter:" ,
                                              choices = File2$Quater,
                                                          selected = File2$Quater, #values which will be selected by default
                                                          animate = FALSE, grid = FALSE,
                                                          hide_min_max = FALSE, from_fixed = FALSE,
                                                          to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
                                                          to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
                                                          post = NULL, dragRange = TRUE))),

 dashboardBody(

   fluidRow( 
     box(
       title = "RiskTier Vs Quater"
       ,status = "primary"
       ,solidHeader = TRUE 
       ,collapsible = TRUE 
       ,plotOutput("k", height = "300px")
     ))))                  




Server<- function(input, output){
 dataset <- reactive({
   File2[(File2$Quater, input$Quater),]
 })
 output$k<- renderPlot({
 ggplot(dataset, aes(x=Quater, y=Freq , group=RiskTierDesc, colour=RiskTierDesc)) + 
 geom_line(aes(size=RiskTierDesc)) +
 geom_point() +
 scale_color_manual(values=c("red","orange","green")) +
 scale_size_manual(values=c(1,1,1)) +
 labs(title ="RiskTier Vs Quater", x = "Quarter", y = "Frequency") +
 geom_text(aes(label = Freq), position = position_dodge(0),vjust = -1) +
 theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())})}


shinyApp(ui, Server)

请帮忙给点建议

【问题讨论】:

  • 我认为File2[(File2$Quater, input$Quater),] 是问题所在。你能提供你的代码,包括数据,然后我可以尝试精确调试
  • 请找到我的样本数据 Quater
  • 你快到了,只需将File2[(File2$Quater,input$Quater),] 更改为File2[File2$Quater==input$Quater,] 并将ggplot(dataset, ...) 更改为ggplot(dataset(), ...)。当您需要共享 df 时,最好使用 dput(File2)
  • 谢谢。现在我收到此错误警告:错误:data 必须是数据帧,或fortify() 可强制的其他对象,而不是具有 reactiveExpr/reactive 类的 S3 对象 [无可用堆栈跟踪]

标签: r shiny


【解决方案1】:

我希望这会有所帮助,这正是您正在寻找的,如果您需要改进,请告诉我。

问候/Revanth Nemani

# Making Data Frame------------
File2 <- structure(list(Quater = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
                                             3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
                                             8L, 9L, 9L, 9L, 10L, 10L, 10L), .Label = c("Fy17Q1", "Fy17Q2", 
                                                                                        "Fy17Q3", "Fy17Q4", "Fy18Q1", "Fy18Q2", "Fy18Q3", "Fy18Q4", "Fy19Q1", 
                                                                                        "Fy19Q2"), class = "factor"), RiskTierDesc = structure(c(1L, 
                                                                                                                                                 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
                                                                                                                                                 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Above Normal", 
                                                                                                                                                                                                                 "High", "Normal"), class = "factor"), Freq = c(519L, 63L, 1514L, 
                                                                                                                                                                                                                                                                563L, 87L, 1662L, 643L, 81L, 1618L, 562L, 69L, 1524L, 555L, 61L, 
                                                                                                                                                                                                                                                                1609L, 622L, 52L, 2090L, 800L, 86L, 2052L, 681L, 66L, 1811L, 
                                                                                                                                                                                                                                                                622L, 57L, 2317L, 344L, 14L, 1537L)), .Names = c("Quater", "RiskTierDesc", 
                                                                                                                                                                                                                                                                                                                 "Freq"), class = "data.frame", row.names = c(NA, -30L))

# Needed Libraries-------------------
library(shinydashboard)
library(shiny)
library(shinyWidgets)
library(ggplot2)
library(dplyr)

#UI---------------------------
ui <- dashboardPage(
  #Header
  dashboardHeader(title = "Basic Dashboard"),
  #Sidebar
  dashboardSidebar(
    sidebarMenu(sliderTextInput("Quater","Select Quarter:" ,
                                choices = unique(File2$Quater),#To not repeat values in the slidertextinput if the values are not sorted
                                selected = unique(File2$Quater), #values which will be selected by default
                                animate = FALSE, grid = FALSE,
                                hide_min_max = FALSE, from_fixed = FALSE,
                                to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
                                to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
                                post = NULL, dragRange = TRUE))),
  #Body
  dashboardBody(

    fluidRow( 
      box(
        title = "RiskTier Vs Quater"
        ,status = "primary"
        ,solidHeader = TRUE 
        ,collapsible = TRUE 
        ,plotOutput("k", height = "300px")
      ))))                  

#Server Function------------------
Server<- function(input, output){
  #React to the slider input
  dataset <- reactive({
    File2 <- File2 %>% filter(Quater %in% input$Quater) #use == instead of %in% if you need only a single quater data
    return(File2)
  })
  output$k<- renderPlot({
    ggplot(dataset(), #You forgot to add the "()" next to dataset. Remember all reactive objects are functions
           aes(x=Quater, y=Freq, group=RiskTierDesc, colour=RiskTierDesc)) + 
      geom_line(aes(size=RiskTierDesc)) +
      geom_point() +
      scale_color_manual(values=c("red","orange","green")) +
      scale_size_manual(values=c(1,1,1)) +
      labs(title ="RiskTier Vs Quater", x = "Quarter", y = "Frequency") +
      geom_text(aes(label = Freq), position = position_dodge(0),vjust = -1) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
    })
  }
#App--------------------------------------
shinyApp(ui, Server)

【讨论】:

  • 谢谢 Revanth,仪表板现在可以使用了。我想展示从 FY17Q1 到 FY19Q2 的情节。现在,如果我从 FY17Q1 到 FY19Q2 进行选择,它只显示 FY17Q1 和 Y19Q2 值,而 FY 之间没有显示。我们可以将其添加到代码中吗?
  • 您好,看来您正在尝试时间序列分析。我们可以尝试将季度转换为日期(结束)格式或使用复选框组输入并添加全选并清除所有按钮。第二个选项将为您提供离散系列,而第一个选项将为您提供连续系列。如果你有兴趣,我可以和你一起制作你的仪表板。我没有画廊可以炫耀,因为即使在我的办公室里,我的所有作品都是高度机密的。我提供面向用户且对用户友好的应用程序,达到 C 级别,也用于法律要求的外部报告。
  • 空闲时帮助别人只是我的爱好
  • 当然喜欢这个想法,如果我们可以联系,请告诉我。我是新来的。谢谢
猜你喜欢
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 2016-05-02
  • 2020-11-20
  • 2023-03-22
  • 2018-05-30
  • 1970-01-01
相关资源
最近更新 更多