2017-04-11 33 views
1

我已經成功地放入了一年的滑塊並創建了一個ggplot。但是,我很難弄清楚如何按年來過濾數據(目前它每年都會顯示一次),如滑塊所示。R Shiny:每年將ggplot鏈接到滑動條

如何獲取數據:

library(WDI) 
xdat <- WDI(indicator=c('NY.GNP.PCAP.CD', 'SH.DYN.MORT', 'SP.POP.TOTL', 'SP.DYN.LE00.IN', 'SP.DYN.TFRT.IN'), start=1960, end=2014, extra=TRUE) 
sdata <- subset(xdat, xdata$region != "Aggregates") 
sdata <- na.omit(sdata) 

sdata$reg <- ifelse(sdata$region == "Europe & Central Asia (all income levels)", 1, ifelse(sdata$region == "Middle East & North Africa (all income levels)", 2, ifelse(sdata$region == "South Asia", 3, ifelse(sdata$region=="Latin America & Caribbean (all income levels)", 4, ifelse(sdata$region=="Sub-Saharan Africa (all income levels)", 5, ifelse(sdata$region=="East Asia & Pacific (all income levels)", 6, ifelse(sdata$region=="North America", 7, 0))))))) 

sdata$col <- ifelse(sdata$reg == 1, "#FFFFFF", ifelse(sdata$reg == 2, "yellow", ifelse(sdata$reg == 3, "red", ifelse(sdata$reg== 4, "green", ifelse(sdata$reg== 5, "orange", ifelse(sdata$reg==6, "lavender", ifelse(sdata$reg== 7, "pink", 0))))))) 

ui.r

library(googleCharts) 


shinyUI(fluidPage(
    plotOutput("view"), 
    titlePanel("title panel"), 
    column(
     sliderInput("slider1", label=h3("Slider"), 
        min=1960, max=2014, value=50, sep="", animate=TRUE), 
     ), 
    fluidRow(
    column(
      radioButtons("radio", label=h3("Radio buttons"), 
         choices = list("Mortality" = 1, "Choice 2" = 2, "Choice 3" = 3), selected=1)), 
    sidebarLayout(
     sidebarPanel(""), 
     mainPanel("Main") 
    )) 
)) 

server.r

library(dplyr) 

shinyServer(function(input, output) { 
    output$view <- renderPlot({ 
    ggplot(sdata, aes(x=NY.GNP.PCAP.CD, y = SP.DYN.LE00.IN)) +  geom_point(aes(size=pop^(0.75)/20000000+2, colour=col)) 
    }) 
}) 

的問題是,每年都顯示出來,當我使用滑塊,它什麼都不做:

Image

我也想知道如何使用單選按鈕來更改變量。

回答

0

的ui.r不工作對我來說(編輯:改變了一些標籤),但我猜是這樣的:

ui.r

library(googleCharts) 

shinyUI(fluidPage(
    plotOutput("view"), 
    titlePanel("title panel"), 
    column(4, 
     sliderInput("slider1", label=h3("Slider"), 
       min=1960, max=2014, value=50, sep="", animate=TRUE) 
    ), 
fluidRow(
    column(2, 
     radioButtons("radio", label=h3("Radio buttons"), 
        choices = list("Mortality" = 1, "Choice 2" = 2, "Choice 3" = 3), selected=1)), 
sidebarLayout(
    sidebarPanel(""), 
    mainPanel("Main") 
)) 
)) 

和服務器

library(dplyr) 

shinyServer(function(input, output) { 
    output$view <- renderPlot({ 
    temp = sdata[sdata$year>(input$slider1)&sdata$year<2014,] 
    ggplot(temp, aes(x=NY.GNP.PCAP.CD, y = SP.DYN.LE00.IN)) + geom_point(aes(size=temp$SP.POP.TOTL^(0.75)/20000000+2, colour=col)) 
    }) 
}) 
+0

不幸的是沒有工作。 –

+0

現在應該工作,我看到你只是改變了開始輸入,所以'輸入$ slider1'現在選擇最小年份 – timfaber

+0

也許這是我的ui.r? –