Selectinput使用Ggplot闪亮的仪表板不工作

问题描述 投票:1回答:1

嗨,我使用Ggplot我闪亮的仪表板。我想绘制季度Vs的频率和季度Vs的FreqbyPercent选择“A”和四分之一Vs的TRinM和四分之一Vs的TRinM时

和我使用Ggplot的功能。当我选择,但“A”有没有情节和“B”我得到的情节。

我试图通过单独使用Ggplot所有选项来获得输出。但同样的结果。

library(shinydashboard)
library(shiny)
library(shinyWidgets)
library(ggplot2)
## test data
Quarter <- c("Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q3",
             "Fy17Q3","Fy17Q3","Fy17Q4","Fy17Q4","Fy17Q4","Fy18Q1","Fy18Q1",
             "Fy18Q1","Fy18Q2","Fy18Q2","Fy18Q2") 
RiskTierDesc <- c("Above Normal","High","Normal","Above Normal","High","Normal",
                  "Above Normal","High","Normal","Above Normal","High","Normal",
                  "Above Normal","High","Normal","Above Normal","High","Normal")
Freq <- c(502,62,1452,549,88,1582,617,80,1578,530,68,1455,536,61,1551,600,52,2038) 
FreqbyPercent <- c(25,3,72,25,4,71,27,4,69,26,3,71,25,3,72,22,2,76)
QuarterInNum<- c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6) 
TotalNRinM <- c(33.044,0,56.459,18.089,0.234,39.774,99.451,20.608,86.166,257.532,
                3.93,336.079,493.464,7.952,298.565,661.728,189.184,1172.245) 
TotalNRinMPercent <- c(37,0,63,31,0,68,48,10,42,43,1,56,62,1,37,33,9,58) 
File2<- data.frame(Quarter,RiskTierDesc,Freq,FreqbyPercent,QuarterInNum,TotalNRinM,
                   TotalNRinMPercent) 
File2$RiskTierDesc = factor(File2$RiskTierDesc, levels=c("High", "Above Normal", "Normal"))

ui <- dashboardPage(
  dashboardHeader(title = "Basic Dashboard"),

  dashboardSidebar(
                   sidebarMenu( selectInput("app", 
                                         "Select App:", 
                                         choices = c("","A","B"), 
                                         selected = "A", 
                                         multiple = FALSE)),
                             sliderTextInput("Quarter","Select Quarter:",
                                              choices =  unique(File2$Quarter),
                                              selected =  unique(File2$Quarter)[c(2, 5)])),

  dashboardBody(
    fluidRow(
      box(solidHeader = TRUE 
          ,collapsible = TRUE,align="center",offset = 2,title = "RiskTier Vs Quater",status = "warning", plotOutput("k", height = "300px"),width = 6)
      ,


      box(solidHeader = TRUE 
          ,collapsible = TRUE,align="center",offset = 4,title = "RiskTier Vs Quater(%)",status = "warning", plotOutput("l", height = "300px"),width = 6)



      )))



#==========================================SERVER=======================================================#

server <- function(input, output) {

  dataInput <- reactive({

    qfrom <- File2$QuarterInNum[match(input$Quarter[1], File2$Quarter)]
    qto <- File2$QuarterInNum[match(input$Quarter[2], File2$Quarter)]
    test <- File2[File2$QuarterInNum %in% seq(from=qfrom,to=qto),]
    #print(test)
    test
  })

  X<-reactive({input$app})

  Test1 <- function(df,y){

    arg <- match.call()

    p <- ggplot(df(), 
                aes(x=Quarter, y= eval(arg$y) , group= RiskTierDesc , colour= RiskTierDesc )) + 
      geom_line(aes(size= RiskTierDesc)) +
      geom_point() + ylim(0,15000) +
      scale_color_manual(values=c("red","orange","green")) +
      scale_size_manual(values=c(1,1,1)) +
      labs( x = "Quarter", y = input$yaxis) +
      geom_text(aes(label = eval(arg$y)), position = position_dodge(0),vjust = -1) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

    p

  }

  Test2 <- function(df,y){

    arg <- match.call()

    p <- ggplot(df(), 
                aes(x=Quarter, y= eval(arg$y) , group= RiskTierDesc , colour= RiskTierDesc )) + 
      geom_line(aes(size= RiskTierDesc)) +
      geom_point() + ylim(0,100) +
      scale_color_manual(values=c("red","orange","green")) +
      scale_size_manual(values=c(1,1,1)) +
      labs( x = "Quarter", y = input$yaxis) +
      geom_text(aes(label = eval(arg$y)), position = position_dodge(0),vjust = -1) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

    p
  }

  output$k<- renderPlot({
    if(input$app == "A"){plot(Test1(dataInput,Freq))}})

  output$l<- renderPlot({
    if(input$app == "A"){plot(Test2(dataInput,FreqbyPercent))}})


  output$k<- renderPlot({
    if(input$app == "B"){plot(Test1(dataInput,TotalNRinM))}})

  output$l<- renderPlot({
    if(input$app == "B"){plot(Test2(dataInput,TotalNRinMPercent))}})

}

shinyApp(ui, server)  

请让我知道如何让剧情为“A”。或者我在哪里丢失或者是我的做法是不正确的。

r shinydashboard selectinput
1个回答
0
投票

与你的代码的问题是在这里:

 output$k<- renderPlot({
    if(input$app == "A"){plot(Test1(dataInput,Freq))}})

  output$l<- renderPlot({
    if(input$app == "A"){plot(Test2(dataInput,FreqbyPercent))}})



  output$k<- renderPlot({
    if(input$app == "B"){plot(Test1(dataInput,TotalNRinM))}})

  output$l<- renderPlot({
    if(input$app == "B"){plot(Test2(dataInput,TotalNRinMPercent))}})

input$app == "A",两条曲线呈现,但随后R也将执行

output$k<- renderPlot({
        if(input$app == "B"){plot(Test1(dataInput,TotalNRinM))}})

output$l<- renderPlot({
        if(input$app == "B"){plot(Test2(dataInput,TotalNRinMPercent))}})

而且由于input$app == "B"FALSE,一个空的情节将通过对被A生成的。

这是我的建议:

 output$k<- renderPlot({
    if(input$app == "A"){plot(Test1(dataInput,Freq))}
    if(input$app == "B"){plot(Test1(dataInput,TotalNRinM))}

    })

  output$l<- renderPlot({
    if(input$app == "A"){plot(Test2(dataInput,FreqbyPercent))}
    if(input$app == "B"){plot(Test2(dataInput,TotalNRinMPercent))}
    })

可以肯定,我将使用一个if else,但是这是给你!

最好

© www.soinside.com 2019 - 2024. All rights reserved.