-->

ggvis output not appearing in shiny application

2019-03-01 15:02发布

问题:

I'm at a loss for what is wrong here, and the other threads on Stack in which users discuss their ggvis + shiny woes don't seem to apply, in spite of being very similar.

First, the basic problem is that my ggvis visualization does not appear in the Shiny app. I have another tab in my Shiny app that shows a data table, and that works fine. Also, when I swap out ggvis and use ggplot2 (leaving the rest of the code intact except for using changing to a compatible function in ui.R that calls the visualization), that works fine also. I'm not getting any error messages or warnings in RStudio. I suspect I have some type of problem with the bind_shiny function, but nothing I've tried seems to be working.

Here is my server.R code:

library(shiny)
library(ggvis)
library(dplyr)

#import file created by screener.R

final <- read.csv("final-test.csv") 


 # Define server logic required to complete visualization

shinyServer(function(input, output) {

reactviz <- reactive({allviz <- filter(final, DivYield >= input$Yield[1])
allviz <- filter(allviz, DivYield <= input$Yield[2])
allviz <- filter(allviz, PAYOUTRATIO >= input$PayoutRatio[1])
allviz <- filter(allviz, PAYOUTRATIO <= input$PayoutRatio[2])
allviz <- filter(allviz, PriceSales >= input$PriceSales[1])
allviz <- filter(allviz, PriceSales <= input$PriceSales[2])
allviz <- filter(allviz, MarketCapinMil >= input$MarketCap[1])
allviz <- filter(allviz, MarketCapinMil <= input$MarketCap[2])
allviz <- allviz[1:35,]
})

output$visplot <- renderPlot({

allviztip <- function(x) { if(is.null(x))return(NULL) 
  row <- allviz[allviz$id == x$id,c(1,4,8,10,11,12,13)]
  paste0(names(row),":",format(row),"</div>",collapse="<br />")}

reactviz() %>% ggvis(x=~PriceBook,y=~DivYield,key:=~id,size=~MarketCapinMil,shape=~PayoutCat,fill=~PriceSales) %>%
  layer_points() %>% add_tooltip(allviztip,"click") %>% add_axis('x', title='Price/Book Ratio',title_offset=60,properties=axis_props(labels=list(fontSize=12), title=list(fontSize=18))) %>% 
  add_axis("y", title = "Dividend Yield",title_offset=50,properties=axis_props(labels=list(fontSize=12), title=list(fontSize=18))) %>% 
  add_legend(scales="shape", title="Payout Ratio Category") %>% 
  add_legend(scales="fill", title="Price/Sales Ratio", properties = legend_props(legend = list(y = 100))) %>%
  add_legend(scales="size", title="Market Cap",values=c(1,25,50,100,500,1000,5000,50000,100000,300000),properties = legend_props(legend = list(y = 200))) %>% 
  set_options(duration = 0,height="auto",width="auto") %>%
  scale_numeric(property="fill",range=c("lightblue","darkblue")) %>%
  bind_shiny("visplot","visplot_ui") 

})

output$stockdata <- renderTable({

tableviz <- filter(final, DivYield >= input$Yield[1])
tableviz <- filter(tableviz, DivYield <= input$Yield[2])
tableviz <- filter(tableviz, PAYOUTRATIO >= input$PayoutRatio[1])
tableviz <- filter(tableviz, PAYOUTRATIO <= input$PayoutRatio[2])
tableviz <- filter(tableviz, PriceSales >= input$PriceSales[1])
tableviz <- filter(tableviz, PriceSales <= input$PriceSales[2])
tableviz <- filter(tableviz, MarketCapinMil >= input$MarketCap[1])
tableviz <- filter(tableviz, MarketCapinMil <= input$MarketCap[2])
tableviz


})



})

Here is my ui.R code:

library(shiny)
library(ggvis)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Visual Stock Screener"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("MarketCap",
                  "Market Capitalization (in Tens of Millions)",
                  min = 1,
                  max = 60000,
                  value = c(100,30000)),

      sliderInput("Yield",
                  "Dividend Yield:",
                  min = 0,
                  max = 1,
                  value = c(.04,.5)),

      sliderInput("PayoutRatio",
                  "Payout Rato:",
                  min = 0,
                  max = 5,
                  value = c(0,.5)),


      sliderInput("PriceSales",
                  "Price/Sales Ratio:",
                  min = 0,
                  max = 25,
                  value = c(0,7)),


      sliderInput("PriceBook",
                  "Price/Book Ratio:",
                  min = 0,
                  max = 30,
                  value = c(0,4))

    ),

    # Show a plot of the generated distribution
    mainPanel(
      tabsetPanel(type="tab",
                  tabPanel("Plot",ggvisOutput("visplot"),uiOutput("visplot_ui")),
                  tabPanel("Data",tableOutput("stockdata")),
                  tabPanel("Documentation",includeHTML("screener-documentation.html"))
                  )


  )
)))

Any thoughts on what I'm doing wrong and why my visualization isn't appearing?

标签: r shiny ggvis