as we can see when we click on select input value it is changing in the graph but why it is not displaying value, I am sure than i am missing something regarding data on the y-axis but don't know what, could someone please guide me through how to rectify this issue.

library(plotly)
library(shiny)
library(ggplot2)

data <- tibble::tribble(
        ~month, ~UnitName, ~abortion, ~delivery, ~pregnant,
  "01-01-2017",       "a",        13,        30,        43,
  "01-02-2017",       "b",        40,        14,        54,
  "01-03-2017",       "c",        19,        15,        34,
  "01-04-2017",       "d",        45,        20,        65,
  "01-05-2017",       "e",        16,        60,        76,
  "01-06-2017",       "f",        10,        35,        45,
  "01-07-2017",       "g",        10,        55,        65,
  "01-08-2017",       "h",        17,        70,        87,
  "01-09-2017",       "i",        10,        88,        98,
  "01-10-2017",       "j",        18,        60,        78,
  "01-11-2017",       "k",        25,        40,        65,
  "01-12-2017",       "l",        30,        37,        67,
  "01-01-2018",       "m",        30,        26,        56,
  "01-02-2018",       "n",        25,        20,        45,
  "01-03-2018",       "o",        20,        14,        34,
  "01-04-2018",       "p",        30,        24,        54,
  "01-05-2018",       "q",        20,        45,        65,
  "01-06-2018",       "r",        10,        57,        67,
  "01-07-2018",       "s",        10,        88,        98,
  "01-08-2018",       "t",        60,        18,        78,
  "01-09-2018",       "u",        30,        35,        65,
  "01-10-2018",       "v",        30,        37,        67,
  "01-11-2018",       "w",        10,        46,        56,
  "01-12-2018",       "x",        20,        45,        65,
  "01-01-2019",       "y",        10,        35,        45,
  "01-02-2019",       "z",        10,        24,        34,
  "01-03-2019",      "aa",        30,        35,        65,
  "01-04-2019",      "bb",        40,        25,        65,
  "01-05-2019",      "cc",        40,        48,        88
  )


ui<-
  fluidPage(
    titlePanel("report"),

  sidebarLayout(
    sidebarPanel(
    h4("Parameters"),
    br(),
    selectInput("Measure", "Measure",data$UnitName)
    ),
    mainPanel(

      plotlyOutput("lineChart"))))

server<-function(input,output){
output$lineChart <- renderPlotly({
# p <- ggplot(data=data, aes_string(x=data$MthYr, y = input$Measure)) +geom_line(size = 1.5) + theme_minimal()
# ggplotly(p)
layout <- list(
      font = list(size = 12),
      title = 'Sonography District-Wise',
      xaxis = list(title = "District"),
      yaxis = list(title = "Sonography Records",automargin = FALSE)
    )
      
    
  p <- plot_ly(data, x = data$month, y = input$Measure , type = 'bar') %>%
  layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
})
}

shinyApp(ui,server)

Your call to plot_ly has the argument y set to input$Measure so it will have a value like a or d. The tibble data does not have a column named a or d. If you set y = data$pregnant, you do get a graph. What do you want to achieve by picking a single value of the UnitName column?

By the way, since you set the data argument, your call to plot_ly could be of this form.

plot_ly(data, x = ~ month, y =  ~ pregnant , type = 'bar')

I knew this passing the value like this would plot the chart but this is not what I wanna do here, if I pass the y axis like this then how would it render or plot the different chart on click of another unitname,I wanna make it dynamic that's why i was trying to pass the y axis through the input.
do you know how could I achieve the same.

passing the direct Y axis would plot this but it won't change when I select the other unitname from selectinput or make it reactive.

p <- plot_ly(data, x = data$month, y = data$abortion , type = 'bar')

 p <- plot_ly(data, x = data$month, y = input$abortion , type = 'bar') 

I am unsure what you want to plot. Do you want to always plot abortion on the y axis and filter the data to show the values from one unitname?

yes, that's exactly what I wanna do, you got me absolutely right. could you please guide me through how could I get it done.

To always filter on unit name based on selectInput Measure, and plot abortion by month use

server<-function(input,output){
  
  filtered_data <- reactive({
    dplyr::filter(
      data,
      UnitName == req(input$Measure)
    )
  })
  
  output$lineChart <- renderPlotly({
    # p <- ggplot(data=data, aes_string(x=data$MthYr, y = input$Measure)) +geom_line(size = 1.5) + theme_minimal()
    # ggplotly(p)
    layout <- list(
      font = list(size = 12),
      title = 'Sonography District-Wise',
      xaxis = list(title = "District"),
      yaxis = list(title = "Sonography Records",automargin = FALSE)
    )
    
    # observeEvent(filtered_data(),
    #              print(filtered_data()))
    # 
    p <- plot_ly(filtered_data(), x = ~month, y = ~abortion , type = 'bar') %>%
      layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
  })
}

Note that for the example data provided this wont give interesting charts as each month has a unique unitname, therefore only one bar on the x-axis is to be expected.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.