Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: HTMLSelect.shinyInput input initially gives NULL #94

Open
1 task done
dereckmezquita opened this issue Oct 26, 2023 · 0 comments
Open
1 task done

[Bug]: HTMLSelect.shinyInput input initially gives NULL #94

dereckmezquita opened this issue Oct 26, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@dereckmezquita
Copy link

Guidelines

  • 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 dereckmezquita added the bug Something isn't working label Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant