我可以使用多个过滤器吗?我在虹膜数据集中创建了一个名为“ New”的新列。我还想要“ New”列过滤器以及“ Species”过滤器。下面是供参考的代码
sample1 <- 1:3
library(shiny)
iris$New <- ifelse(iris$Sepal.Width>2.5,"greater than 2.5","Not Greater
than 2.5")
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)
答案 0 :(得分:1)
您需要添加uiOutput
,比较iris$New == input$New
到iris$Petal.Width
中的summary
和output$New
的子集中。您最终会得到这个,我想这就是您想要的:
编辑:我已按照OP在此答案下方的注释中所要求的种类输入中添加了另一个选项“全部”。如果input$Species
为“全部”,则有条件返回基于“新”的仅仅的子集,否则返回基于物种和新的子集。 req
函数修复了if
中的“长度为0”错误。
这是代码。我在更改内容的地方添加了评论:
sample1 <- 1:3
library(shiny)
iris$New <- ifelse(iris$Sepal.Width>2.5,"greater than 2.5","Not Greater than 2.5")
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"),
uiOutput("New") # <- ADD THIS
)
)
)
server <- function(input, output, session) {
r1 <- reactive({
if(input$x == "summary")
{
#### MODIFY ALL OF THIS ###########################################
req(input$Species) # <- REQUIRE INPUT BEFORE CONTINUING
if(input$Species == "all"){
summary(iris$Petal.Width[iris$New == input$New])
} else {
summary(iris$Petal.Width[iris$Species == input$Species &
iris$New == input$New]) # <- ADD THIS
}
###################################################################
} 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 = c("all", as.character(unique(iris$Species))), multiple = FALSE)
})
#### ADD ALL OF THIS ######################################################
output$New <- renderUI({
selectInput("New", "new",
choices = as.character(unique(iris$New)), multiple = FALSE)
})
###########################################################################
}
shinyApp(ui, server)