shiny.blueprint icon indicating copy to clipboard operation
shiny.blueprint copied to clipboard

[Bug]: `HTMLSelect.shinyInput` input initially gives `NULL`

Open dereckmezquita opened this issue 2 years ago • 0 comments

Guidelines

  • [X] I agree to follow this project's Contributing Guidelines.

Project Version

0.2.0

Platform and OS Version

macOS 13.1

Existing Issues

No response

What happened?

When I use shiny.blueprint$HTMLSelect.shinyInput on initial start of my app the input is always NULL initially and then gets set.

This doesn't happen if I use the default shiny$selectInput, this works well with no problems.

In order to avoid errors I have to do a check inside my shiny$reactive if null return default value, else return input value.

# sideBar.R
box::use(shiny)
box::use(shiny.blueprint)

supp_plot_options <- lapply(c("RSI", "MACD", "Volume"), \(opt) {
    return(list(value = opt, label = opt))
})

#' @export
ui <- function(id, width = 2) {
    ns <- shiny$NS(id)
    shiny$sidebarPanel(
        shiny$tags$h4("Side bar"),
        shiny.blueprint$HTMLSelect.shinyInput(
            inputId = ns("select_currency"),
            options = c("BTC/USDT", "XMR/USDT"),
            value = "BTC/USDT"
        ),
        shiny.blueprint$HTMLSelect.shinyInput(
            inputId = ns("supplementary_plot"),
            options = supp_plot_options,
            value = "RSI"
        ),
        width = width
    )
}

#' @export
server <- function(id, live_data) {
    shiny$moduleServer(id, \(input, output, session) {
        supplementary_plot <- shiny$reactive({
            if (is.null(input$supplementary_plot)) {
                return(supp_plot_options[[1]]$value)
            }

            return(input$supplementary_plot)
        })

        shiny::observe(print(input$supplementary_plot))

        currency <- shiny$reactive({
            input$select_currency
        })

        return(list(
            currency = currency,
            supplementary_plot = supplementary_plot
        ))
    })
}
r$> shiny::runApp(
        port = 3003,
        launch.browser = FALSE
    )

Listening on http://127.0.0.1:3003
NULL
[1] "RSI"

And if I don't do the is.null check I get this kind of error. Note I'm trying to use this input for other parts of my app this is why I need the values.

...
        supplementary_plot <- shiny$reactive({
            # if (is.null(input$supplementary_plot)) {
            #     return(supp_plot_options[[1]]$value)
            # }

            return(input$supplementary_plot)
        })
...
r$> shiny::runApp(
        port = 3003,
        launch.browser = FALSE
    )

Listening on http://127.0.0.1:3003
NULL
Warning: Error in switch: EXPR must be a length 1 vector
  168: renderPlot [/Users/work/Coding/projects/finance/R-kucoin-trading-bot-playground/R-kucoin-trading-bot/app/view/plot_panel.R#29]
  166: func [/private/var/folders/w4/_q0hj0997zqdg4zllxpwmq0r0000gp/T/RtmpET6HVq/R.INSTALLb54312084973/shiny/R/utils.R#1447]
  126: drawPlot [/private/var/folders/w4/_q0hj0997zqdg4zllxpwmq0r0000gp/T/RtmpET6HVq/R.INSTALLb54312084973/shiny/R/render-plot.R#254]
  112: <reactive:plotObj> [/private/var/folders/w4/_q0hj0997zqdg4zllxpwmq0r0000gp/T/RtmpET6HVq/R.INSTALLb54312084973/shiny/R/render-plot.R#125]
   96: drawReactive [/private/var/folders/w4/_q0hj0997zqdg4zllxpwmq0r0000gp/T/RtmpET6HVq/R.INSTALLb54312084973/shiny/R/reactives.R#870]
   83: renderFunc [/private/var/folders/w4/_q0hj0997zqdg4zllxpwmq0r0000gp/T/RtmpET6HVq/R.INSTALLb54312084973/shiny/R/render-plot.R#164]
   82: output$app-plot_panel-supplementary_plot [/private/var/folders/w4/_q0hj0997zqdg4zllxpwmq0r0000gp/T/RtmpET6HVq/R.INSTALLb54312084973/shiny/R/shinywrappers.R#133]
    1: shiny::runApp [/private/var/folders/w4/_q0hj0997zqdg4zllxpwmq0r0000gp/T/RtmpET6HVq/R.INSTALLb54312084973/shiny/R/runapp.R#388]
[1] "RSI"
^C


r$>

Steps to reproduce

Use my module in an app that uses the value returned from it.

Expected behavior

I expect the default value to be set by shiny.blueprint value argument.

Attachments

No response

Screenshots or Videos

No response

Additional Information

Thank you very much for this package very nice, I love blueprint for my nextjs projects, will definitely use it for shiny.

dereckmezquita avatar Oct 26 '23 04:10 dereckmezquita