Shiny selectizeInput: on click remove the initial selected value - r

i would like to remove the initial value (selected=) from selectizeInput when the user click on the widget.
Here is a sample code:
library(shiny)
library(dplyr)
ui= fluidPage(
sidebarLayout(
sidebarPanel(
selectizeInput(inputId= "cyl", label= "cyl",
choices= NULL,
selected= sort(unique(mtcars$cyl))[1],
multiple=T)
),
mainPanel(
tableOutput("tab")
)
)
)
server= function(input, output,session) {
updateSelectizeInput(session = session,inputId ="cyl",choices=sort(unique(mtcars$cyl)),selected=sort(unique(mtcars$cyl))[1], server = TRUE)
df_filtered= reactive({
mtcars %>%
{if (is.null(input$cyl)) . else filter(., cyl %in% input$cyl)}
})
output$tab= renderTable(df_filtered())
}
shinyApp(ui, server)
Just a bit of explanation on base of sample code:
The initial selected value in selectizeInput "cyl" is 4. When the user press on this widget, i would like that the value 4 is removed and the selected option is cleared. Any ideas?
*I have used the function updateSelectizeInput in server because in my shiny app choice selection is very big leading to too long loading time

You can use shinyjs::onclick to call updateSelectizeInput when the user clicks on the selectize field, eg:
library(shiny)
library(dplyr)
library(shinyjs)
ui= fluidPage(
useShinyjs(),
sidebarLayout(
sidebarPanel(
selectizeInput(inputId= "cyl", label= "cyl",
choices= NULL,
selected= sort(unique(mtcars$cyl))[1],
multiple=T)
),
mainPanel(
tableOutput("tab")
)
)
)
server= function(input, output,session) {
updateSelectizeInput(session = session,inputId ="cyl",choices=sort(unique(mtcars$cyl)),selected=sort(unique(mtcars$cyl))[1], server = TRUE)
df_filtered= reactive({
mtcars %>%
{if (is.null(input$cyl)) . else filter(., cyl %in% input$cyl)}
})
output$tab= renderTable(df_filtered())
onclick("cyl", {
updateSelectizeInput(session, "cyl", selected = "")
})
}
shinyApp(ui, server)

Related

Shiny: How can I loop thru variable names in the ui selectInput choices in the server datasetInput switch?

in Shiny I simply want to select which variable of a dataframe shall be plotted and I do not want to have to type all the variable names in the server switch part. Here is what I do:
ui <- fluidPage(
titlePanel("Hello World!"),
sidebarLayout(
sidebarPanel(
selectInput("variable", "Choose a variable:",
# choices = c("cyl", "mpg")),
choices = names(mtcars)),
),
mainPanel(
plotOutput(outputId = "BarPlot"),
)
)
)
server <- function(input, output) {
datasetInput <- reactive({
switch(input$variable,
"cyl" = mtcars[,"cyl"],
"mpg" = mtcars[,"mpg"])
})
output$BarPlot <- renderPlot({
x <- datasetInput()
barplot(table(x))
})
}
Instead of
switch(input$variable,
"cyl" = mtcars[,"cyl"],
"mpg" = mtcars[,"mpg"])
can I do something like
choices = mtcars[,get(choices)]
to cover all choices without having to type them one by one?
One approach is to use varSelectInput and pass the data frame as data (it will include all column names as the choices). Then you can extract the selected column from mtcars through mtcars[[input$variable]] in your example:
library(shiny)
ui <- fluidPage(
titlePanel("Hello World!"),
sidebarLayout(
sidebarPanel(
varSelectInput("variable",
"Choose a variable:",
data = mtcars),
),
mainPanel(
plotOutput(outputId = "BarPlot"),
)
)
)
server <- function(input, output) {
datasetInput <- reactive({
mtcars[[input$variable]]
})
output$BarPlot <- renderPlot({
x <- datasetInput()
barplot(table(x))
})
}
shinyApp(ui, server)

Dynamically set dropdown options based on other dropdown selection

The following code allows the user to select between 2 datasets in a dropdown and displays all of the columns for the selected dataset. I would like to add a second dropdown that is dynamically cast with the names of the columns in the selected first dropdown so that the user can select to display a single column only. The goal is to be able to add any dataset to the first dropdown and have it's columns listed in the second dropdown.
library(shiny)
ui <- shinyUI(
fluidPage(
selectInput("dataset", label = NULL, choices = c("mtcars", "rock")),
tableOutput("contents")
)
)
server <- function(input, output, session) {
myData <- reactive({
switch(input$dataset,
"rock" = rock,
"mtcars" = mtcars)
})
output$contents <- renderTable({
myData()
})
observe({
updateSelectInput(session, "myNames",
label = "myNames",
choices = myData()$names,
selected = myData()$names[1])
})
}
shinyApp(ui, server)
Perhaps you are looking for this
library(shiny)
ui <- shinyUI(
fluidPage(
selectInput("dataset", label = NULL, choices = c("mtcars", "rock")),
uiOutput("selectvar"),
plotOutput("contents")
)
)
server <- function(input, output, session) {
myData <- reactive({get(input$dataset)})
output$selectvar <- renderUI({
selectInput("varsel", label=NULL, choices=names(myData()), multiple=T)
})
output$contents <- renderPlot({
req(input$varsel[1],input$varsel[2])
ggplot(myData(), aes(x=.data[[input$varsel[1]]], y=.data[[input$varsel[2]]] )) + geom_point()
})
}
shinyApp(ui, server)

How to update datatable on click of actionButton?

So I have a datatable in Shiny that I only want to update when the user changes the input parameters and clicks a button. Below is a minimum reproducible example of it:
library(dplyr)
library(DT)
library(shiny)
ui <- fluidPage(
fluidRow(
column(3, numericInput("num1", "Limiter1", value = 0)),
column(3, numericInput("num2", "Limiter2", value = 0))
),
fluidRow(
column(3,actionButton("button1", "Apply filters1")),
column(3,actionButton("button2", "Apply filters2"))
),
fluidRow(
column(6,dataTableOutput("testtable1")),
column(6,dataTableOutput("testtable2"))
)
)
server <- function(input, output, session) {
filteredData1 <- reactive({
req(input$num1)
iris %>%
filter(Petal.Length >= input$num1)
})
observeEvent(input$button1, {
updateNumericInput(session, "num2", value = input$num1)
output$testtable1 <- renderDataTable(datatable(filteredData1()))
})
filteredData2 <- reactive({
req(input$num2)
iris %>%
filter(Petal.Length >= input$num2)
})
observeEvent(input$button2, {
output$testtable2 <- renderDataTable(datatable(filteredData2()))
})
}
shinyApp(ui, server)
Unfortunately, in this case, the datatable first loads when the user clicks the button but after that automatically updates every time the input$num1 changes regardless of whether button1 is clicked. Is there a way to update the table with the new parameters only when button1 is clicked?
If the application is this simple you can just change the actionButton for a submitButton
library(dplyr)
library(DT)
library(shiny)
ui <- fluidPage(
fluidRow(
numericInput("num1", "Limiter", value = 0)
),
fluidRow(
submitButton("button1", "Apply filters")
),
fluidRow(
dataTableOutput("testtable")
)
)
server <- function(input, output, session) {
filteredData <- reactive({
req(input$num1)
iris %>%
filter(Petal.Length >= input$num1)
})
output$testtable <- renderDataTable(datatable(filteredData()))
}
shinyApp(ui, server)
With a reactive value:
library(dplyr)
library(DT)
library(shiny)
ui <- fluidPage(
fluidRow(
numericInput("num1", "Limiter", value = 0)
),
fluidRow(
actionButton("button1", "Apply filters")
),
fluidRow(
dataTableOutput("testtable")
)
)
server <- function(input, output, session) {
filteredData <- reactiveVal(iris)
observeEvent(input$button1, {
filteredData(iris %>% filter(Petal.Length >= input$num1))
})
output$testtable <- renderDataTable(datatable(filteredData()))
}
shinyApp(ui, server)

Shiny - Selectizeinput Update

as you know an item inside the selectizeinput can just be deleted by pressing "delete", but not by clicking on it. This reduces the user experience of my dashboards dramatically. Here is a solution which deletes ALL inputs in the input fields, however I only want to delete the one, which was clicked.
library(shiny)
library(dplyr)
library(shinyjs)
ui= fluidPage(
useShinyjs(),
sidebarLayout(
sidebarPanel(
selectizeInput(inputId= "cyl", label= "cyl",
choices= NULL,
selected= sort(unique(mtcars$cyl))[1],
multiple=T)
),
mainPanel(
tableOutput("tab")
)
)
)
server= function(input, output,session) {
updateSelectizeInput(session = session,inputId ="cyl",choices=sort(unique(mtcars$cyl)),selected=sort(unique(mtcars$cyl))[1], server = TRUE)
df_filtered= reactive({
mtcars %>%
{if (is.null(input$cyl)) . else filter(., cyl %in% input$cyl)}
})
output$tab= renderTable(df_filtered())
onclick("cyl", {
updateSelectizeInput(session, "cyl", selected = "")
})
}
shinyApp(ui, server)
Is there any option to to this? I thought about triggering a keydown event via javascript, but this did not work yet :-(. Can you guys help me?
I got it!
selectizeInput("money", "Financing", choices = data$kauf, selected = data$kauf, multiple = TRUE, options = list(plugins = list('remove_button')))
Works :-)

How to add a user defined value to the select list of values from dataset

I am new to Shiny R, and as part of a project I would have to show distinct values for selection in a selectlist, but I also need to provide an option called "All" to query with.
dataset <- read.csv("dataset.csv", header=TRUE)
fluidPage(
title = "ABC XYZ",
hr(),
fluidRow(
titlePanel("ABC XYZ"),
sidebarPanel(
selectInput("region", label = "Region",
choices = unique(dataset$region),
selected = 1)
)
)
Can anyone help me achieve the same.
Thanks in advance.
We could create an additional level or unique element 'All' in choices and update with updateSelectInput
library(shiny)
library(DT)
library(dplyr)
#using a reproducible example
dataset <- iris
allchoice <- c("All", levels(dataset$Species))
-ui
ui <- fluidPage(
title = "ABC XYZ",
hr(),
fluidRow(
titlePanel("ABC XYZ"),
sidebarPanel(
selectInput("species", label = "Species",
choices = allchoice, multiple = TRUE),
verbatimTextOutput("selected")
),
mainPanel(dataTableOutput('out')))
)
-server
server <- function(input, output, session) {
observe({
if("All" %in% input$species) {
selected <- setdiff(allchoice, "All")
updateSelectInput(session, "species", selected = selected)
}
})
output$selected <- renderText({
paste(input$species, collapse = ", ")
})
output$out <- renderDataTable({
dataset %>%
filter(Species %in% input$species)
})
-run app
shinyApp(ui, server)

Resources