2015-10-06 51 views
3

我有下面的代碼,我嘗試運行失敗。我只是希望能夠根據輸入範圍過濾圖形輸出。在這個例子中,如果範圍輸入設置爲1-5,我想在圖上看到5個點,同樣2-5的範圍輸入顯示4個點。Shiny Reactive ggplot Output

##UI 
require(shiny) 
shinyUI(fluidPage(
titlePanel(title=h4("Races", align="center")  
), 
sidebarPanel( 
sliderInput("num", "Number:", 
      min = 0, max = 5,step=1,value=c(0,2)), 
mainPanel(
    plotOutput("plot2"))))) 

##server 
library(dplyr) 
library(ggplot2) 
shinyServer(function(input,output){ 
num<-c(1,2,3,4,5) 
let<-c("A","B","C","D","E") 
date<-c("2015-5-1","2015-6-1","2015-7-1","2015-8-1","2015-9-1") 
df<-data.frame(num,let,date) 
dat<-reactive({ 
df %>% filter(num==input$num)}) 
output$plot2<-renderPlot({ 
ggplot(dat(),aes(x=date,y=num))+geom_point(colour='red')},height = 400, 
width = 600)}) 

我想顯示基於所述輸入範圍圖表上的值的範圍,但只有1值被示出,與對應的日期值不在該範圍。

在此先感謝。

回答

4

這是你想要的嗎? 編輯:現在你就可以看到所有您指定

#rm(list = ls()) 
library(shiny) 
library(ggplot2) 

num<-c(1,2,3,4,5) 
let<-c("A","B","C","D","E") 
date<-c("2015-5-1","2015-6-1","2015-7-1","2015-8-1","2015-9-1") 
df <- data.frame(num,let,date) 

ui <- fluidPage(
    titlePanel(title=h4("Races", align="center")), 
    sidebarPanel( 
    sliderInput("num", "Number:",min = 0, max = 5,step=1,value=c(1,2))), 
    mainPanel(plotOutput("plot2"))) 

server <- function(input,output){ 

    dat <- reactive({ 
    test <- df[df$num %in% seq(from=min(input$num),to=max(input$num),by=1),] 
    print(test) 
    test 
    }) 

    output$plot2<-renderPlot({ 
    ggplot(dat(),aes(x=date,y=num))+geom_point(colour='red')},height = 400,width = 600)} 
shinyApp(ui, server) 

enter image description here

+0

沒有,點@Pork印章,如果輸入範圍是1-5,我想看到5點,同樣與輸入範圍2-5我想看到4點。我懷疑會帶來的1-5範圍輸入:ggplot(df,aes(x = date,y = num))+ geom_point(color ='red')但是不會 – Lebeauski