R Shiny的滤镜选项中是否有适用于R的功能

时间:2019-07-05 05:22:40

标签: r shiny

我有以下代码。我需要在mainpanel中放置一个主过滤器,以便我可以选择应依次更改数字(摘要)的类别

# faithful is the dataset
# Iris is the dataset
 iris$New <- ifelse(iris$Sepal.Width>2.5,"greater than 2.5","Not Greater 
 than 2.5")
library(shiny)

sample1 <- 1:3

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(
        "x",
        "Operations",
        choices = c("summary","stem","typeof","mode","birth"), 
        multiple = TRUE,
        selectize = TRUE
      )
    ),
    mainPanel(
      h6("Here it is"),
      verbatimTextOutput("message")
    )
  )
)

server <- function(input, output, session) {
  output$message <- renderPrint({
    if(input$x == "summary"){
      summary(iris$Petal.Width)
    } else if (input$x == "stem"){
      print(stem(faithful$eruptions))
    } else if (input$x == "typeof"){
      typeof(sample1)
    } else if (input$x == "mode"){
      mode(sample1)
    } 
  }) 
}

shinyApp(ui, server)

我可以从虹膜数据集中获取“种类”的主过滤器吗?当我选择“ setosa”时,摘要应作相应更改

1 个答案:

答案 0 :(得分:1)

如果我们需要基于“种类”的值来更改summary,请将renderUIuiOutput一起使用

sample1 <- 1:3
library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(selectInput("x","Operations",choices = 
                               c("summary","stem","typeof","mode","birth"),
    multiple=FALSE,selectize = TRUE)),
    mainPanel(h6("Here it is"),
              verbatimTextOutput("message"),
              uiOutput("Species")
    )
  )
)
server <- function(input, output, session) {

  r1 <- reactive({

    if(input$x == "summary")
    {
      summary(iris$Petal.Width[iris$Species == input$Species])
    } else if (input$x == "stem")
    {
      print(stem(faithful$eruptions))
    } else if (input$x == "typeof")
    {
      typeof(sample1)
    } else if (input$x == "mode")
    {
      mode(sample1)
    } 
  }) 

output$message <- renderPrint({r1()})

  output$Species <- renderUI({

    selectInput("Species", "species", 
      choices = as.character(unique(iris$Species)), multiple = FALSE)
  })
}
shinyApp(ui, server)

-输出

enter image description here