0

Below is a code to my shiny app

library(shiny)

DF = data.frame(ID = c ("1","2","3","4","5"),
                product = c("A","B","C","A","C"),
                sentiment = c("good","medium","bad","medium","bad"),
                online = c(1,0,1,1,0),
                ooh = c(0,1,0,1,1),
                Date= c("2020-08-11", "2020-09-16","2021-10-20", "2020-11-25", "2022-11-27"), 
                event = c(1,1,0,0,0))



# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Product Sentiment"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("selectsentiment", label = "Select sentiment", choices = unique(DF$sentiment)), 
      dateRangeInput("daterange", h5("Date range"), 
                     start = ymd("2020-08-11"), 
                     end = ymd("2022-09-06"), 
                     min = min(DF$Date), 
                     max = max(DF$Date),
                     format = "yyyy-mm-dd"
      ) 
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      dataTableOutput("DT_Table")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$DT_Table <- renderDataTable({ 
    DF[DF$sentiment %in% input$selectsentiment,]
    
  })   
  
}

# Run the application 
shinyApp(ui = ui, server = server)

I want to filter the output of the datatable when a person does both. I.e. selecting positive or negative as well as selecting a date date range How do I go about that?

andy
  • 1,947
  • 5
  • 27
  • 46

1 Answers1

0

Please check the updated renderDataTable call:

library(shiny)

DF = data.frame(ID = c ("1","2","3","4","5"),
                product = c("A","B","C","A","C"),
                sentiment = c("good","medium","bad","medium","bad"),
                online = c(1,0,1,1,0),
                ooh = c(0,1,0,1,1),
                event = c(1,1,0,0,0))




# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Product Sentiment"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("selectsentiment", label = "Select sentiment", choices = unique(DF$sentiment))
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      dataTableOutput("DT_Table")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$DT_Table <- renderDataTable({ 
    DF[DF$sentiment %in% input$selectsentiment,]
  })   
  
}

# Run the application 
shinyApp(ui = ui, server = server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • Thanks, Please see updated code? How do I filter both date range and the sentiment – andy Apr 17 '23 at 08:17
  • Please don't [replace or update your initial question with another one](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions). If the answer was helpful accept and upvote it. If you have another question - post a new one. Regarding your update please see [this](https://stackoverflow.com/questions/62168406/filter-r-shiny-data-frame-based-on-slider-range-and-display-result-in-data-table). – ismirsehregal Apr 17 '23 at 08:29