【问题标题】:R shiny selectizeInput get input valueR闪亮selectizeInput获取输入值
【发布时间】:2026-01-01 08:05:03
【问题描述】:

我有关于 selectizeInput 的问题。如果我有更多选择的输入,我怎样才能获得它们的值。存在一些像 input$selected[1],input$selected[2] ? 谢谢。

我的代码:

data<-data.frame(c("Mexico","China","Italy","Italy","Mexico"),c(120,130,125,140,145),c("Book","Table","Desk","Window","Rabbit"))

colnames(data)<-c("State","Count","Name")

    selectizeInput("mySelect",label="choose",multiple=TRUE,choices=colnames(data),
                   options =list(maxItems=2,plugins = list('remove_button', 'drag_drop')))

我需要这个来渲染

ggplot(data,aes(input$mySelect[1],input$mySelect[2]))

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您需要使用aes_string 并将geom 添加到您的绘图中。但是您是否正在寻找这样的东西:

    data<-data.frame(c("Mexico","China","Italy","Italy","Mexico"),c(120,130,125,140,145),c("Book","Table","Desk","Window","Rabbit"))
    
    colnames(data)<-c("State","Count","Name")
    
    ui <- fluidPage(
      selectizeInput("mySelect",label="choose",multiple=TRUE,choices=colnames(data),
                     options =list(maxItems=2,plugins = list('remove_button', 'drag_drop'))),
      plotOutput('my_plot')
    )
    
    server <- function(input,output,session) {
      output$my_plot <- renderPlot({
        ggplot(data,aes_string(input$mySelect[1],input$mySelect[2])) + geom_point()
      })
    }
    
    shinyApp(ui,server)
    

    【讨论】: