R shiny 使用来自 textInput 的输入作为 tibble 中的列名

R shiny use input from textInput to be the column name in a tibble

我想使用 textInput 中的值作为我正在构建的 tibble 中的列名称。我尝试使用 input$urgency1 作为列名,但这会产生错误。我尝试用 <- 替换 = 但它仍然不起作用。

library(shiny)

ui <- fluidPage(
  textInput("urgency1", label = "Urgency 1", value = "2WW"),
  dataTableOutput("template")
)

server <- function(input, output, session) {
  output$template = renderDataTable({
    df=tibble(input$urgency1 = rep(1, 105))
    df
  }, options = list(pageLength = 10))
}

shinyApp(ui, server)

实现您想要的结果的一个选择是像这样使用 rlang::sym(!!input$urgency1) := ...

编辑:正如@RonakShah 在评论中指出的那样,不需要 rlang::sym!!input$urgency1 := ... 足够了。

library(shiny)
library(tibble)


ui <- fluidPage(
  textInput("urgency1", label = "Urgency 1", value = "2WW"),
  dataTableOutput("template")
)

server <- function(input, output, session) {
  output$template = renderDataTable({
    df = tibble(!!input$urgency1 := rep(1, 105))
    df
  }, options = list(pageLength = 10))
}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:6488

reprex package (v2.0.0)

于 2021-05-18 创建