简体   繁体   中英

Shiny Reactive ggplot Output

I have the following code that I am unsuccessfully trying to run. I just want to be able to filter a graph output based on an input range. In this example, if the range input is set to 1-5, I would like to see 5 points on the graph, likewise range input of 2-5 shows 4 points.

##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)})

I would like to show the range of values on the graph based on the input range but only 1 value is showing, not the range of values with corresponding dates.

Thanks in advance.

Is this what you want? Edit: Now you would be able to see all the points you have specified

#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)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM