I have two problems:
I have two dependent filters in the database, and I want to search either by player or by their ID. I also want the first filter (SelectInput) to be responsive.
If for example I enter the number 2 in the ID, I want my selectInput to display Lionel Messi automatically.
Here is the code and thank you for your answers
library(DT)
library(shinydashboard)
library(shiny)
library(shinyWidgets)
library(dplyr)
Database<- data.frame(Player=c("Cristiano Ronaldo","Lionel Messi","Neymar Jr","Cristiano Ronaldo"),ID=c(1,2,3,1))
ui<-dashboardPage(title="Application",skin="red",
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
selectInput("player",HTML('Please select your player'),choices=names(table(Database$Player))),
searchInput(inputId = "IDSEARCH", label = HTML('Or Please write the ID player'),
#placeholder = "13850",
btnSearch = icon("search"),
btnReset = icon("remove"),
width = "500px"),
DT::dataTableOutput("mtable2")
))
server <- function(input, output){
mtable2 <- reactive({filter(Database,(Player==input$player|ID==input$IDSEARCH))})
output$mtable2<-DT::renderDataTable({DT::datatable(mtable2())})
}
shinyApp(ui,server)
this is my solution to your problem. After the code I explain several things there.
HTML()
.selectInput()
. Note thestringsAsFactors = FALSE
when creating the data frame (in R >= 4.0.0 this is not needed).searchInput
for the ID, but since it was your choice I'm keeping it here.isTruthy()
function checks whether the value ininput$id
is "truthy" as the name says. Basically it checks it is not NULL, empty string, NA, etc. So, when no ID is given, we use the name in theselectInput()
to filter.Database[idx, ]
.input$id
that updates theselectInput()
. Note you need to pass thesession
, which becomes an argument to your server function...Well, just feel free to ask if you have any questions!
EDIT:
To use {dplyr} I would change the following
would be rewritten as
and replace
with