Shiny renderUI selectInput returned NULL
This might work if you use is.null() and return your "default" for input$A:
A.r <- reactive({
if(is.null(input$A)) return (1)
input$A
})
sliderInput with renderUI returns NULL in modularized R Shiny app
As stated in this question, you have to use session$ns()
in the renderUI()
:
test_module_server <- function(input, output, session) {
output$ui <- renderUI({
sliderInput(session$ns("dynamic"), "The slider:",
min = 1,
max = 20,
value = 10)
})
Resetting selectInput to NULL in R Shiny
Up front, use selected=character(0)
(or selected=""
, as @starja commented):
updateSelectInput("descrxaxis", label = "Choose x axis variable",
choices = colnames(descrDataMelted_selVars$df), multiple = TRUE,
selected = character(0))
Some key components from your other (duplicate) question that need to be fixed, and are relevant to the concept of updating inputs (but not obvious based on the current state of this question):
First, do not nest
observe
orobserveEvent
within another one. This might be a typo in your code there, but make sure that allobserve*
(and any reactive function, for that matter) functions are effectively on the "top-level" of theserver
function.Change from
observeEvent(input$descrBtnPlot,{
...
output$descrSummaryStatsPlot <- renderPlotly(p)
observeEvent(input$descrXaxisResetBtn, {
updateSelectInput("descrxaxis", selected = character(0))
})
})to
observeEvent(input$descrBtnPlot,{
...
output$descrSummaryStatsPlot <- renderPlotly(p)
})
observeEvent(input$descrXaxisResetBtn, {
updateSelectInput("descrxaxis", selected = character(0))
})(though this is incomplete, see the next point).
Second, the first argument of any
update*
function is thesession
. This is not optional. The code in bullet 1 above should really beobserveEvent(input$descrBtnPlot,{
...
output$descrSummaryStatsPlot <- renderPlotly(p)
})
observeEvent(input$descrXaxisResetBtn, {
updateSelectInput(session, "descrxaxis", selected = character(0))
# ^^^^^^^^ the only difference
})This leads me to the last bullet:
Third, your
server <- function(input, output)
is fine when you don't use anyupdate*
functions, but in line with the second point above, you must havesession
, which means you need to change your server definition toserver <- function(input, output, session) {
...
}
I'll use the example from ?updateSelectInput
to demonstrate how to find the right answer, but injecting a single browser()
before the actual call.
library(shiny)
ui <- fluidPage(
p("The checkbox group controls the select input"),
checkboxGroupInput("inCheckboxGroup", "Input checkbox",
c("Item A", "Item B", "Item C")),
selectInput("inSelect", "Select input",
c("Item A", "Item B", "Item C"))
)
server <- function(input, output, session) {
observe({
x <- input$inCheckboxGroup
# Can use character(0) to remove all choices
if (is.null(x))
x <- character(0)
browser()
# Can also set the label and select items
updateSelectInput(session, "inSelect",
label = paste("Select input label", length(x)),
choices = x,
selected = tail(x, 1)
)
})
}
shinyApp(ui, server)
When you run this the first time, it'll start with an empty selector, so we can just
c
ontinue out of that debugger.Select an item. The call to
updateSelectInput
setsselected = tail(x, 1)
, so if we run that on the console, we'll seeBrowse[2]> tail(x, 1)
[1] "Item A"c
ontinue out of this debugger, notice the selector now saysItem A
.Deselect the "A" checkbox, and you'll be in the debugger again.
Browse[4]> tail(x, 1)
character(0)c
ontinue out of this debugger, and you'll see the selector is now empty (deselected).
Why? Internally, looking at the source for updateSelectInput
(as of 1.4.0.2),
updateSelectInput
# function (session, inputId, label = NULL, choices = NULL, selected = NULL)
# {
# choices <- if (!is.null(choices))
# choicesWithNames(choices)
# if (!is.null(selected))
# selected <- as.character(selected)
# options <- if (!is.null(choices))
# selectOptions(choices, selected)
# message <- dropNulls(list(label = label, options = options,
# value = selected))
# session$sendInputMessage(inputId, message)
# }
# <bytecode: 0x00000000b8f06030>
# <environment: namespace:shiny>
see that all it checks is !is.null(selected)
, so your thought to use selected=NULL
is both its default value and it is used as the choice to "do not change the selected value".
As a more specific example, I'll start with the (non-debugged) version of that app, and add a single "Reset!" button that will clear the selection in the selectInput
:
ui <- fluidPage(
p("The checkbox group controls the select input"),
checkboxGroupInput("inCheckboxGroup", "Input checkbox",
c("Item A", "Item B", "Item C")),
selectInput("inSelect", "Select input",
c("Item A", "Item B", "Item C")),
actionButton("resetsel", "Reset!")
)
server <- function(input, output, session) {
observe({
x <- input$inCheckboxGroup
# Can use character(0) to remove all choices
if (is.null(x))
x <- character(0)
# Can also set the label and select items
updateSelectInput(session, "inSelect",
label = paste("Select input label", length(x)),
choices = x,
selected = tail(x, 1)
)
})
observeEvent(input$resetsel, {
### either
updateSelectInput(session, "inSelect", selected = character(0))
### or
# updateSelectInput(session, "inSelect", selected = "")
### both do the same thing here
})
}
shinyApp(ui, server)
Now, the basic operation of the shiny demo works, but when you click on the reset button, the selection is cleared. FYI, if you don't have changes for choices=
, you don't need to include that in your call to updateSelectInput
. It doesn't hurt, but it is only necessary if you believe the list of choices may need to be updated/changed otherwise.
Detecting a selectInput value change to NULL in R Shiny
By default observeEvent
is set to ignore NULL
. Adding ignoreNULL = FALSE
to observeEvent
will fix that. You may also wish to add ignoreInit = TRUE
to stop the observeEvent
from triggering on startup.
Here is the full code:
library(shiny)
ui <- fluidPage(
selectInput(inputId = "var", label = "Select a variable:", choices = c("A", "B", "C"), selected = NULL, multiple = T),
textOutput("selected_var")
)
server <- function(input, output) {
observeEvent(input$var, {
if(is.null(input$var)) {
showNotification("var changed to null")
}
}, ignoreInit = TRUE, ignoreNULL = FALSE)
}
shinyApp(ui = ui, server = server)
Shiny renderUI SelectInput won't update
Is this what you want?
rm(list = ls())
library(shiny)
names <- list("A"=1, "B"=2, "C"=3, "D"=4, "E"=5, "F"=6, "G"=7, "H"=8, "I"=9)
ui =(pageWithSidebar(
headerPanel("Test Shiny App"),
sidebarPanel(
selectInput("viewTopic", "View Topic:", choices = names, selected = 1),
#display dynamic UI
uiOutput("text")),
mainPanel()
))
server = function(input, output, session){
output$text <- renderUI({
textInput("docTitle", label = "Topic Name:", value = as.character(input$viewTopic))
})
}
runApp(list(ui = ui, server = server))
R shiny: how to set selectInput that depends on the updateVarSelectInput()
You have many syntax issues here. In the future, please ensure that you have matching brackets and it is a MRE. The following should work.
library(shiny)
ui = fluidPage(
fileInput(inputId = "rawFile", label="File"), # Read the dataframe
varSelectInput(inputId = "getColumn", label="Get Column", data = ""),
selectInput(inputId = "levels", label="Levels", choices = "")
)
server = function(input, output, session){
df <- reactive({
inFile <- input$rawFile
if (is.null(inFile)){return(NULL)}
read.csv(inFile$datapath, header = T)
})
observeEvent(df(),{
updateVarSelectInput(session, inputId = "getColumn",data = df(),selected = "")
})
observeEvent(input$getColumn, {
updateSelectInput(session,inputId = "levels", choices = levels(as.factor(df()[[input$getColumn]])),selected = NULL)
})
}
shinyApp(ui, server)
Related Topics
Rlang::Sym in Anonymous Functions
Passing Data Within Shiny Modules from Module 1 to Module 2
Current Time in Iso 8601 Format
Plot Background Colour in Gradient
Assign a Value, If a Number Is in Between Two Numbers
What Type of Graph Is This? and Can It Be Created Using Ggplot2
How to Use Cast or Another Function to Create a Binary Table in R
R::Ggplot2::Geom_Points: How to Swap Points with Pie Charts
How to Suppress the Creation of a Plot While Calling a Function in R
Convert Factor to Integer in a Data Frame
(Igraph) Grouped Layout Based on Attribute
Why I Get This Error Writing Data to a File
Image Not Showing in Shiny App R
How to Combine Scales for Colour and Size into One Legend
R V3.4.0-2 Unable to Find Libgfortran.So.3 on Arch