在R/Shiny中如何使输入与actionButton同名

trnvg8h3  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(129)

我有一段代码,当我单击selectInput小部件的选项时,输入值就是选项上显示的名称。
我想用我的actionLink按钮做同样的事情,但是在这种情况下输入是点击的总和。是否可以更改输入值?
这是我的代码:

library(shiny)
library(dplyr)
library(purrr)

ui <- fluidPage(

  tags$div(
    id = "sidebar",
    class = "sidebar",

    selectInput(
      inputId = "custom_select",
      label = "Clubs",
      choices = names(mtcars),
      selectize = F,
      size = 5,

      width = "300px"
    ),

    div(
      names(mtcars) %>% map(~.x %>% actionLink(inputId = .x)))
  ),

  h1(htmlOutput(outputId = 'title')),

  h1(htmlOutput(outputId = 'title2')))

server <- function(input, output, session) {

  output$title <- renderUI({

    input$custom_select

  })
  output$title2 <- renderUI({

    input[[names(mtcars)[1]]]

  })

}

shinyApp(ui, server)

如您所见,输出是点击次数。
对于selectInput小部件,它工作正常。
有人帮忙吗?

2ic8powd

2ic8powd1#

不确定我是否理解正确,但使用observeEvent可以执行以下操作:

library(shiny)
library(dplyr)
library(purrr)

ui <- fluidPage(
  tags$div(
    id = "sidebar",
    class = "sidebar",
    selectInput(
      inputId = "custom_select",
      label = "Clubs",
      choices = names(mtcars),
      selectize = F,
      size = 5,
      width = "300px"
    ),
    div(
      names(mtcars) %>% map(~ .x %>% actionLink(inputId = .x))
    )
  ),
  h1(htmlOutput(outputId = "title")),
  h1(htmlOutput(outputId = "title2"))
)

server <- function(input, output, session) {
  output$title <- renderUI({
    input$custom_select
  })
  lapply(names(mtcars), function(x) {
    observeEvent(input[[x]], {
      output$title2 <- renderUI({
        paste(x, input[[x]], sep = ": ")
      })  
    })  
  })
}

shinyApp(ui, server)

相关问题