首页 > 解决方案 > R Shiny:varSelectInput 仅来自数据帧的数字变量

问题描述

在一个闪亮的应用程序中,我有一个 varSelectInput 小部件来从 iris 数据集中选择变量。有没有一种方法可以将 varSelectInput 中的变量限制为仅限数字变量。我尝试使用 is.numeric(iris),但没有成功。谢谢。

我的代码:


  library(shiny)
  library(ggplot2)
  
  # single selection
  shinyApp(
    ui = fluidPage(
      varSelectInput("variable", "Variable:", is.numeric(iris),
                     selected = NULL),
      
      plotOutput("data")
    ),
    server = function(input, output) {
      output$data <- renderPlot({
        ggplot(iris, aes(!!input$variable)) + geom_histogram()
      })
    }
  )

标签: rshiny

解决方案


您可以尝试Filter

library(shiny)
library(ggplot2)

shinyApp(
  ui = fluidPage(
    varSelectInput("variable", "Variable:", Filter(is.numeric, iris),
                   selected = NULL),
    
    plotOutput("data")
  ),
  server = function(input, output) {
    output$data <- renderPlot({
      ggplot(iris, aes(.data[[input$variable]])) + geom_histogram()
    })
  }
)

推荐阅读