在Shiny应用程序中使用时,ggplotly无法正常工作

时间:2017-06-09 13:10:11

标签: r shiny

我的ggplotly图(参见server.R中的表3)在我的Shiny应用程序中使用时不起作用。但是,当我在RStudio中自己生成绘图时,它可以正常工作。

这是一些无法正确渲染绘图的代码。

output$facetmap=renderPlotly({

      ggplotly(

        ggplot(ranksvf(),aes(Rank,input$parameterchoice,fill=Location))+
        ggtitle("") +
        theme(axis.title.y=element_blank())+
        geom_bar(position="dodge",stat="identity")+
        facet_wrap(~Tran.Hour.2h.Slot,nrow=2)

        )

    })

当我说它没有正确渲染情节时,我指的是两件事:

1)当我在ggplot中使用input$parameterchoice时,图形变得奇怪。看起来像这样。 Incorrect Plot

2)当我在ggplot而不是input$parameterchoice中使用输入的实际名称时,情节很好。然而,当我将鼠标悬停在绘图上时,值不会显示它们应该显示(它是一个绘图,因此它应该显示)。

我觉得奇怪的是我在我的应用程序的Tab 2中也使用了ggplotly,它工作正常(鼠标悬停也可以)。

我觉得问题与我使用reactive功能的方式有关,尽管我不确定。我试图调试一段时间,但到目前为止没有运气。

这就是我的应用程序的样子。

####
#UI#
####

    ui=fluidPage(theme = shinytheme("paper"),
                 titlePanel("Visualising Site-Specific Indicators: XYZ University"),
                #img(src='xyz.jpg', align = "left"),
                   tabsetPanel(

                            #TAB 1

                            tabPanel(type="pills","Macro-View of Locations",
                                    fluidRow(
                                            column(width = 4,
                                                  wellPanel(     
                                                      selectInput("size",
                                                      label="Select Parameter for Rectangle Size",
                                                      choices=names(details)[2:5],selected = "Average Daily Transactions"))),

                                            column(width = 4,
                                                  wellPanel(
                                                      selectInput("color",
                                                      label="Select Parameter for Rectangle Color",
                                                      choices=names(details)[2:5],selected = "Unique Products Sold"))
                                                  )#Close column

                                            ), #Close fluidRow

                                    fluidRow(
                                           plotOutput("plot")),
                                    fluidRow(
                                           dataTableOutput("tab"))

                                     ),#Close tabPanel macroview

                           #TAB 2

                           tabPanel("Transaction Overiew by Location",
                                    fluidRow(
                                      column(width = 4,
                                             wellPanel(     
                                               selectInput("sitechoice",
                                                           label="Select a Site",
                                                           choices=unique(heatmap_mean$Location),selected = "Horton 1"))
                                             )#Close column

                                    ), #Close fluidRow

                                    fluidRow(
                                      plotlyOutput("heatmap")),
                                    fluidRow(
                                      dataTableOutput("tab2"))

                                     ),#Close tabPanel transactionoverview

                           #TAB 3

                           tabPanel("Parameter Ranking",
                                    fluidRow(
                                      column(width = 4,
                                             wellPanel( 
                                               selectInput("parameterchoice",
                                                           label="Rank By",
                                                           choices=unique(c(names(rankdf_avgtran),names(rankdf_ticket)))[3:4],selected = "Average Transaction Value (USD)"))
                                             ),#Close column

                                      column(width=6,
                                             wellPanel(
                                               sliderInput("rankchoice",
                                                           label="Number of Ranks Desired",
                                                           min=1,
                                                           max=10,
                                                           value=5))
                                             )#Close column

                                    ), #Close fluidRow

                                    fluidRow(
                                      plotlyOutput("facetmap")),
                                    fluidRow(
                                      dataTableOutput("tab3"))

                           )#Close tabPanel transactionoverview

    ) #Close tabsetpanel      
    ) #Close UI

    ########
    #SERVER#
    ########

    server=function(input, output,session) {


    # TAB 1

      sortTable <- reactive({
        details[do.call(order, -details[as.character(input$size)]),]
      })

      output$plot= renderPlot ({
        treemap(details,
                index=c("Site"),
                vSize=input$size,
                vColor=input$color,
                title="XYZ University: Overview of Site Data",
                fontsize.title = 20,
                #sortID = paste("-",input$sort,sep=""),
                type="value")
      })

      output$tab <- renderDataTable({
        sortTable()

      })


    #TAB 2

    test=reactive({
         heatmap_mean %>% filter(Location==input$sitechoice)
    })

    output$heatmap=renderPlotly({
      ggplotly(
        ggplot(test(), aes(Day, `Time Slot`)) +
        geom_tile(aes(fill = `Average Number of Transactions`),color = "white") +
        scale_fill_gradient(low = "lightblue", high = "darkblue") +
        ylab("") +
        xlab("") +
        theme(legend.title = element_text(size = 8),
              panel.background = element_blank(),
              legend.text = element_text(size = 8),
              plot.title = element_text(size=18),
              axis.title=element_text(size=22,face="bold"),
              axis.text.x = element_text(angle = 90, hjust = 1)) +
        labs(fill = ""))


    })

    output$tab2 <- renderDataTable({
      test()

    })

    #TAB 3

    ranks_pen <- reactive({

      if(input$parameterchoice=="Average Number of Transactions")
           { 
        showdata=rankdf_avgtran %>% 
        group_by(Tran.Hour.2h.Slot) %>%
        top_n(n = input$rankchoice, wt = `Average Number of Transactions`) %>% #For each time slot, cut off top n values.
        mutate(Rank = rank(-`Average Number of Transactions`,  ties.method = "first")) #And rank for each of the 'n' sites for each time slot
        return(showdata)   
           }

      else

         if(input$parameterchoice=="Average Transaction Value (USD)")
             {
             showdata=rankdf_ticket %>% 
             group_by(Tran.Hour.2h.Slot) %>%
             top_n(n = input$rankchoice, wt = `Average Transaction Value (USD)`) %>% #For each time slot, cut off top 'n' values.
             mutate(Rank = rank(-`Average Transaction Value (USD)`, ties.method = "first")) #And rank the 'n' sites for each time slot 
             return(showdata)
             }

    })

    ranksvf<- reactive({
              ranks_pen() %>%
              group_by(Tran.Hour.2h.Slot) %>% #Group the columns
              arrange(Rank) #Arrange rank from 1 to 'n'
    })

    output$facetmap=renderPlotly({

      ggplotly(

        ggplot(ranksvf(),aes(Rank,input$parameterchoice,fill=Location))+
        ggtitle("") +
        theme(axis.title.y=element_blank())+
        geom_bar(position="dodge",stat="identity")+
        facet_wrap(~Tran.Hour.2h.Slot,nrow=2)

        )

    })

    output$tab3 <- renderDataTable({
    ranksvf()
    })

    }#Close server

    #RUN APP
    shinyApp(ui,server)

2 个答案:

答案 0 :(得分:0)

试一试:

QLabel {
    qproperty-alignment: AlignCenter;
    border: 1px solid #FF17365D;
    border-top-left-radius: 15px;
    border-top-right-radius: 15px;
    background-color: #FF17365D;
    padding: 5px 0px;
    color: rgb(255, 255, 255);
    max-height: 25px;
    font-size: 14px;
}
QFrame {
    border: 1px solid #FF17365D;
    border-bottom-left-radius: 15px;
    border-bottom-right-radius: 15px;
}

答案 1 :(得分:0)

input$parameterchoice返回带引号的字符串,但aes仅接受未加引号的字符串作为参数。使用aes_代替解决问题

output$facetmap=renderPlotly({
  pc <- input$parameterchoice
    ggplotly(
      ggplot(ranksvf(),aes_(quote(Rank),as.name(pc),fill=quote(Location)))+
      ggtitle("") +
      theme(axis.title.y=element_blank())+
      geom_bar(position="dodge",stat="identity")+
      facet_wrap(~Tran.Hour.2h.Slot,nrow=2)
 )
})