Not getting graph in Shiny App in R -
i trying create small application using shiny. below data frame trying create.
data<-data.frame(state=c('az','va','ca','az','va','ca'), city=c('phoenix','arlington','santaclara','mesa','richmond','sf'), avg=c(10,15,16,13,14,14), date=c('01/09/2017','01/10/2017','01/11/2017','02/09/2017','02/10/2017','02/10/2017'),stringsasfactors = false)
so, trying create graph between date(x-axis) , avg(y-axis). graph should change based on selection dropdown list of state.for example, particular selected state, should show cities available(in other dropdown) in state.
below code:
library(shiny) library(ggplot2) library(plotly) statelist<-as.list(data$state) citylist<-as.list(data$city) ui <- basicpage( # plotoutput("plot1", click = "plot_click"), # verbatimtextoutput("info") sidebarpanel( selectinput("plot1", label=h3("select state"), choices = statelist), selectinput("plot2", label=h3("select city"), choices = citylist) ), plotoutput(outputid="plot") ), server <- function(input, output, session) { observe( { state <- input$plot1 updateselectinput(session, "plot2", choices = data$city[data$state == state]) } ), output$plot<-renderplot({ ggplot(data[data$city == input$plot2 & data$state == input$plot1],aes(date,avg)) +geom_line() }) } shinyapp(ui, server)
dropdown working not getting graph.
thanks in advance!!
i made minor modifications code:
- there commas in places should not be: after ui constructor, , after observe constructor.
- there comma missing in
data[data$city == input$plot2 & data$state == input$plot1,]
- i edited observe observeevent, can find differences here
- i modified plot show changes, since sample data quite limited.
hope helps!
library(shiny) library(ggplot2) library(plotly) data<-data.frame(state=c('az','va','ca','az','va','ca'), city=c('phoenix','arlington','santaclara','mesa','richmond','sf'), avg=c(10,15,16,13,14,14), date=c('01/09/2017','01/10/2017','01/11/2017','02/09/2017','02/10/2017','02/10/2017'),stringsasfactors = false) statelist<-unique(data$state) citylist<-unique(data$city) ui <- basicpage( # plotoutput("plot1", click = "plot_click"), # verbatimtextoutput("info") sidebarpanel( selectinput("plot1", label=h3("select state"), choices = statelist), selectinput("plot2", label=h3("select city"), choices = citylist) ), plotoutput(outputid="plot") ) server <- function(input, output, session) { observeevent(input$plot1, { state <- input$plot1 updateselectinput(session, "plot2", choices = data$city[data$state == state]) } ) output$plot<-renderplot({ data = data[data$city == input$plot2 & data$state == input$plot1,] ggplot(data,aes(date,avg)) + geom_point(size=5) + ggtitle(paste0(input$plot1," - ",input$plot2 )) }) } shinyapp(ui, server)
Comments
Post a Comment