【问题标题】:R Shiny: varSelectInput only numeric variables from dataframeR Shiny:varSelectInput 仅来自数据帧的数字变量
【发布时间】:2020-12-10 08:32:58
【问题描述】:

在一个闪亮的应用程序中,我有一个 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()
      })
    }
  )

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你可以试试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()
        })
      }
    )
    

    【讨论】:

    • 而不是names(Filter(is.numeric, iris)),不是吗?
    • 是的,这也是我所期待的,但 varSelectInput 需要输入作为数据框,它会自动从中获取列名。
    • 啊,是吗?不知道那个。我实际上从未使用过它。
    • 谢谢 Ronak 和 Stéphane。
    猜你喜欢
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2015-09-06
    • 2021-03-19
    • 2015-01-21
    • 1970-01-01
    相关资源
    最近更新 更多