Reputation: 552
I am trying to get one UI element to react with another, and then test to see if the first element is equal to one particular value. I seem to be able to print out the value of the reactive element, but I cannot use it in an if statement to check its value. Code is below. I have commented out the troublesome part around line 23. It runs with the lines commented out, and verifies that the reactive element input$paramRes1 does have an character value of an integer between 1 - 5, as expected, but when I un-comment out the lines to check if that integer value is 1, it tells me that input$paramRes1 is null. The exact error is:
Warning:
Error in if: argument is of length zero
Stack trace (innermost first):
59: observerFunc [#16]
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
Why would it tell me that input$paramRes1 is null, when it prints out as an char? Or am I misreading the error? Thanks for your help!
suppressWarnings(library(shiny))
suppressWarnings(library(shinyFiles))
ui <- function(request) {
fluidPage(
uiOutput("param1"),
uiOutput("param2"),
fluidRow(column(12, verbatimTextOutput("value", placeholder = T)))
)
}
server <- function(input, output, session) {
state = reactiveValues()
observe({
state$choice <- setNames(as.list(c(1, 2, 3, 4, 5)), c("First", "Second",
"Third", "Fourth", "Fifth"))
})
output$param1 <- renderUI({
selectInput("paramRes1", "Select Param 1", state$choice, selected = 1)
})
output$param2 <- renderUI({
textInput("paramNm", "Param", value = input$paramRes1, width = '150px')
})
observe({
# if (input$paramRes1 %in% 1) {
state$val <- input$paramRes1
# } else {
# state$val <- 0
# }
})
output$value <- renderText({ str(state$val) })
}
shinyApp(ui = ui, server = server)
Upvotes: 2
Views: 2715
Reputation: 7695
Here is a (slightly) simplified version of your app with the fix I suggested in the comments
library(shiny)
ui <- fluidPage(
uiOutput("param1"),
uiOutput("param2"),
fluidRow(column(12, verbatimTextOutput("value", placeholder = T)))
)
server <- function(input, output, session) {
state = reactiveValues(choice = list(
first = 1, second = 2, third = 3, fourth = 4, fifth = 5
))
output$param1 <- renderUI({
selectInput("paramRes1", "Select Param 1", state$choice, selected = 1)
})
output$param2 <- renderUI({
textInput("paramNm", "Param", value = input$paramRes1, width = '150px')
})
observeEvent(input$paramRes1,{
if (input$paramRes1 %in% 1)
state$val <- "It's 1"
else
state$val <- "It's not 1"
})
output$value <- renderText({ state$val })
}
shinyApp(ui = ui, server = server)
Upvotes: 1
Reputation: 8127
Would the following work? I'm only changing the server portion here. The difference is highlighted in between ######
.
server <- function(input, output, session) {
state = reactiveValues()
observe({
state$choice <- setNames(as.list(c(1, 2, 3, 4, 5)), c("First", "Second",
"Third", "Fourth", "Fifth"))
})
output$param1 <- renderUI({
selectInput("paramRes1", "Select Param 1", state$choice, selected = 1)
})
output$param2 <- renderUI({
textInput("paramNm", "Param", value = input$paramRes1, width = '150px')
})
#####
observeEvent(input$paramRes1, {
if (input$paramRes1 == 1) {
state$val <- input$paramRes1
} else {
state$val <- 0
}
})
######
output$value <- renderText({ str(state$val) })
}
Upvotes: 0