I want to create a selectInput()
within a modalDialog()
. And also create a reactive dataset based on that selectInput()
. However i get the error message:
Warning: Error in : Problem with `filter()` input `..1`.
i Input `..1` is `name == input$selectName`.
x Input `..1` must be of size 87 or 1, not size 0.
[No stack trace available]
Whereas I can create the input of my numericInput()
reactively within the renderText()
. Where is the difference there? Is there a possibilty to create reactive Data within a modalDialog()
?
library(shiny)
library(tidyverse)
data = starwars
ui = fluidPage(
div(style = "margin-top: 50px; display: flex; justify-content: center;",
actionButton(inputId = "openModal", label = "Open"))
)
server = function(input, output, session){
observeEvent(input$openModal, {
reactiveData = reactive({
data %>% filter(name == input$selectName)
})
output$added = renderText({input$add})
showModal(
modalDialog(
title = NULL, easyClose = T, size = "m", footer = modalButton("back"),
tagList(
div(selectInput(inputId = "selectName", label = NULL, choices = data$name)),
div(numericInput(inputId = "add", label = NULL, value = 0)),
div(reactiveData()%>%pull(height)),
div(textOutput(outputId = "added"))
))
)
})
}
shinyApp(ui, server)
CodePudding user response:
Replace ==
with %in%
in:
reactiveData = reactive({ data %>% filter(name == input$selectName)})
to
reactiveData = reactive({ data %>% filter(name %in% input$selectName)})
Explanation: from answer of d.b here Difference between the == and %in% operators in R
%in% is value matching and "returns a vector of the positions of (first) matches of its first argument in its second" (See help('%in%')) This means you could compare vectors of different lengths to see if elements of one vector match at least one element in another. The length of output will be equal to the length of the vector being compared (the first one).
== is logical operator meant to compare if two things are exactly equal. If the vectors are of equal length, elements will be compared element-wise. If not, vectors will be recycled. The length of output will be equal to the length of the longer vector.