带有两个滑块的闪亮应用程序:第二个滑块只选择端点,而不是范围 [英] Shiny app with two sliders: second slider is only selecting the endpoints, not range
本文介绍了带有两个滑块的闪亮应用程序:第二个滑块只选择端点,而不是范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经编写了以下R Shiny
应用程序。用户从下拉菜单中选择farm
,并从slider
中选择单个值year
。我希望用户能够为month
选择range
值,但第二个slider
仅选择month
的终点,而不是range
。
以下是month
slider
从以下app
中创建的data.frame fruit
中选择的内容,以farm == 1
和year == 2
为例:
> fruit[fruit$farm == 1 & fruit$year == 2 & fruit$month %in% c(4,9),]
farm year month apples cherries
16 1 2 4 70.23774 82.18512
21 1 2 9 38.99675 66.07546
以下是我希望month
slider
选择的内容:
> fruit[fruit$farm == 1 & fruit$year == 2 & fruit$month %in% c(4:9),]
farm year month apples cherries
16 1 2 4 70.23774 82.18512
17 1 2 5 37.17340 83.47027
18 1 2 6 36.00925 73.27322
19 1 2 7 31.20337 98.30440
20 1 2 8 33.93355 63.92046
21 1 2 9 38.99675 66.07546
如何修改此代码以使所有选定月份都包含在生成的plot
中?以下是我的Shiny
应用程序:
library(shiny)
set.seed(1234)
n.farms <- 5
n.years <- 3
fruit <- data.frame(
farm = rep(1:n.farms, each = 12*n.years),
year = rep(rep(1:n.years, each = 12), n.farms),
month = rep(1:12,n.farms * n.years),
apples = runif(n.farms*12*n.years, 20, 80),
cherries = runif(n.farms*12*n.years, 0, 100)
)
ui <- fluidPage(
titlePanel("Subset and Plot Fruit Data"),
sidebarLayout(
sidebarPanel(
selectInput("codeInput1", label = "Choose Farm", choices = unique(fruit$farm)),
sliderInput("codeInput2", label = "Select Year",
min = min(fruit$year), max = max(fruit$year),
value = median(fruit$year), step = 1),
sliderInput("codeInput3", label = "Select Month Range",
min = min(fruit$month), max = max(fruit$month),
value = unique(fruit$month)[c(4,9)], step = 1)
),
mainPanel(
plotOutput("view")
)
)
)
server <- function(input, output) {
dataset <- reactive({
return(subset(fruit, (farm == input$codeInput1 &
year == input$codeInput2 &
month %in% input$codeInput3)))
})
output$view <- renderPlot(plot(dataset()$apples, dataset()$cherries))
}
shinyApp(ui = ui, server = server)
推荐答案
如果选择一个范围,sliderInput
将返回该范围内的最小值和最大值,但不是所有值。因此,您必须从两个返回值生成范围:
month %in% (input$codeInput3[1]:input$codeInput3[2])
编辑
正如Mark指出的,我的解决方案只适用于整数;测试范围(包含边框)的正确方法是:
month >= input$codeInput3[1] && month <= input$codeInput3[2]
这篇关于带有两个滑块的闪亮应用程序:第二个滑块只选择端点,而不是范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文