0

I have the dataframe below:

x1<-floor(runif(100, 1,5))
x2<-floor(runif(100, 6,10))
x3<-floor(runif(100, 10,14))
x4<-floor(runif(100, 15,19))
result<-floor(runif(100,100,110))
df<-data.frame(x1,x2,x3,x4,result)

x1 x2 x3 x4 result
1  4  6 11 15    101
2  1  6 12 18    108
3  2  6 13 15    103
4  3  7 13 17    107
5  2  8 10 18    108
6  2  9 12 16    105

and I have a created a shiny app with 4 sliders one for every x value and a histogram which displays the frequency of result. What I want to do is highlight with a different color than the darkblue the values of result based on the combination of the x values. If for example I select 4,6,11,15 (first line of the df) then the 101 bar should be highlighted. If I select one value from every line of the first 4 then 101,108,103,107 should be highlighted with the same color which should be different from darkblue.

#ui.r
library(shiny)
library(shinyWidgets)
library(ggplot2)
pageWithSidebar(
  headerPanel("Histogram"),
  sidebarPanel(width=2,
               h4("Blood-center specific variables"),
               sliderTextInput(
                 inputId = "col", label = "Annual number of collections", 
                 choices = sort(unique(df$x1),decreasing = F), selected = min(df$x1), 
                 grid = TRUE
               ),
               sliderTextInput(
                 inputId = "rat", label = "Collection: import ratio ", 
                 choices = sort(unique(df$x2),decreasing = F), selected = min(df$x2), 
                 grid = TRUE
               ),
               sliderTextInput(
                 inputId = "aph", label = "Apheresis: WB ratio", 
                 choices = sort(unique(df$x3),decreasing = F), selected = min(df$x3), 
                 grid = TRUE
               ),
               sliderTextInput(
                 inputId = "tra", label = "Current trauma distribution %", 
                 choices = sort(unique(df$x4),decreasing = F), selected = min(df$x4), 
                 grid = TRUE
               )


  ),
  mainPanel(
    fluidRow(
      plotOutput("h1")
    )

  )
)
#server.r
library(shiny)
library(shinyWidgets)
library(ggplot2)
function(input, output) {

  output$h1<-renderPlot(
    ggplot(df, aes(x=result))+
      geom_histogram(color="darkblue", fill="lightblue")
  )

}
firmo23
  • 7,490
  • 2
  • 38
  • 114
  • Your app is not working for me, even after adding `shinyApp(ui = ui, server = server)` and after assigning `ui` and `server`. Could you clear your environment and then rerun your exact code to see if it responds as you expect besides the highlighting issue? – DaveM Sep 21 '19 at 16:22
  • have you loaded the dataset? – firmo23 Sep 21 '19 at 16:40
  • Yes. Data set is fine. – DaveM Sep 21 '19 at 16:40
  • it runs normally. what error do you get? – firmo23 Sep 21 '19 at 16:42
  • I didn’t get any errors, it just didn’t run and did not respond to inputs. I had to add ui and server definitions and the shiny app line. Made me think you might not have copied all of the code to make it run as a reprex. – DaveM Sep 21 '19 at 16:54
  • no eveything is there but the sliders are not connected with the plot.This is the Q – firmo23 Sep 21 '19 at 17:04
  • 1
    It seems to me that you are looking for this: https://stackoverflow.com/questions/21858394/partially-color-histogram-in-r – novica Sep 21 '19 at 18:14
  • it is a good approach .could this be adapted in my case? – firmo23 Sep 22 '19 at 11:16

0 Answers0