40767

Unable to use reactive element in my shiny app

In my shiny app,I want to change the ggplot barChart that I wish to construct. selectinput should allow to change the month (see dataset below) and so my plot should change accordingly.

<strong>problem</strong>: The isssue is, i am unable to use my reactive function or even just simple input$monthid within ggplot function.

<strong>Dataset:</strong>

Month Orders 1 Feb 984524 2 Jan 1151303 3 Mar 575000 > dput(b) structure(list(Month = c("Feb", "Jan", "Mar"), Orders = c(984524L, 1151303L, 575000L)), .Names = c("Month", "Orders"), class = "data.frame", row.names = c(NA, -3L))

<strong>ui.R</strong>

library(shiny) library(shinythemes) b<-read.csv("b.csv",header=TRUE,sep=",",stringsAsFactors=TRUE) shinyUI(fluidPage(theme= shinytheme("flatly"), sidebarLayout( sidebarPanel( selectInput(inputId = "monthid", label = "Month",choices = b$Month,selected = b$Month[1])), mainPanel(plotOutput("plot")) )) )

<strong>server.R</strong>

library(shiny) library(shinythemes) library(ggplot2) b<-read.csv("b.csv",header=TRUE,sep=",",stringsAsFactors=TRUE) shinyServer(function(input, output) { #making a reactive object m<-reactive ({ as.character(input$monthid) }) output$plot<- renderPlot({ #probably I am making a subset error in x inside aes parameter ggplot(data = b, aes(x = b[,m()] ,y = b$Orders)) + geom_bar(stat="identity") }) })

Answer1:

Here's a minimal working example you can copy and paste right in your session to run, but a bar chart with a single bar doesn't really make a lot of sense (and looks ugly if you ask me):

library(shiny) shinyApp( ui = fluidPage( sidebarLayout( sidebarPanel( selectInput( inputId = "monthid", label = "Month", choices = b$Month, selected = b$Month[1] ) ), mainPanel(plotOutput("plot")) ) ), server = function(input, output) { DF <- reactive({ b[b$Month == input$monthid, , drop = FALSE] }) output$plot <- renderPlot({ ggplot(DF(), aes(x = Month, y = Orders)) + geom_bar(stat = "identity") }) } )

It looks somewhat like this:

<img src="https://i.imgur.com/ZKx8s27.png" alt="">

Since that doesn't look nice IMO, you could do something with highlighting the currently selected bar, for example:

b$highlight <- factor("Not Selected", levels = c("Not selected", "Selected")) shinyApp( ui = fluidPage( sidebarLayout( sidebarPanel( selectInput( inputId = "monthid", label = "Month", choices = b$Month, selected = b$Month[1] ) ), mainPanel(plotOutput("plot")) ) ), server = function(input, output) { DF <- reactive({ b[b$Month == input$monthid, "highlight"] <- "Selected" b }) output$plot <- renderPlot({ ggplot(DF(), aes(x = Month, y = Orders, fill = highlight)) + geom_bar(stat = "identity") }) } )

This would look as follows:

<img src="https://i.imgur.com/M2OwzkM.png" alt="">

Recommend

  • Loop to create PDFs of each node in ego network with node ID as file names in R
  • Converting columns to character with sapply
  • r- caret package error- createDataParition no observation
  • R Data table setkey - error some columns are not in the data.table
  • 3.0.0.M1: SSL - Invalid keystore format
  • pandas computation in each group
  • Implement JwtBearer Authentication in NSwag SwaggerUi
  • How do I remove all but some records based on a threshold?
  • How to use Windows Media Foundation with UWP without a topology
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Plotting line graph with factors in R
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • wxPython: displaying multiple widgets in same frame
  • Installed module is empty
  • R - Combining Columns to String Based on Logical Match
  • Change JButton Shape while respecting Look And Feel
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • jQuery tmpl and DataLink beta
  • align graphs with different xlab
  • Unanticipated behavior
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Cannot Parse HTML Data Using Android / JSOUP
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • embed rChart in Markdown
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • How do I use LINQ to get all the Items that have a particular SubItem?