Can we use tidyeval with shiny inputs as strings?

I was wondering if it's possible to use tidyeval to get the value of a shiny input when it's a string as below? In the example I would like the output to print what the user puts in the box [I realize how to do this the "normal way" just curious and creating as small a reprex as possible :blush:]

library(shiny)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            textInput("test", "TEST")
        ),
        mainPanel(
            verbatimTextOutput("debug")
        )
    )
)

server <- function(input, output) {
    output$debug <- renderText(rlang::parse_expr("input[[test]]"))
}

shinyApp(ui = ui, server = server)

I don't know what is the purpose but yes you can use rlang functions if you wish to. You just need to pass correct type in the function.

  • input[[test]] won't work because test does not exist. I think you mean input[['test']] or input$test
  • rlang::parse_expr will return an expression, not something that can be rendered as text by renderText. See help page
  • I think you need as least to eval the expression, using for example rlang::eval_tidy.

Try

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput("test", "TEST")
    ),
    mainPanel(
      verbatimTextOutput("debug")
    )
  )
)

server <- function(input, output) {
  output$debug <- renderText({
    rlang::eval_tidy(rlang::parse_expr("input[['test']]"))
  })
}

shinyApp(ui = ui, server = server)

Let's note the this is a dummy example as passing input$test directly here will work fine.

Hope it helps you dig and understand rlang.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.