提问人:Hack-R 提问时间:1/5/2015 最后编辑:Hack-R 更新时间:8/30/2018 访问量:6116
在 R Shiny 中:如何分离滑块输入值并用它们进行筛选
In R Shiny How to Separate Slider Input Values and Filter with them
问:
在 R 的包中,您可以使用一个具有上限和下限的滑块小部件。shiny
sliderInput
我希望能够将上限值和下限值分开,并在将数据传递给之前用它们过滤一列数据。UI.R
例如:
服务器.R
library(shiny)
data <- read.csv('some_data_to_filter.csv')
# Define server logic for slider examples
shinyServer(function(input, output) {
# Reactive expression to compose a data frame containing all of
# the values
sliderValues <- reactive({
# Compose data frame
data.frame(
Name = c("Range"),
Value = as.character(c(paste(input$range, collapse=' '),)),
stringsAsFactors=FALSE)
})
# Show the values using an HTML table
output$values <- renderTable({
sliderValues()
})
})
用户界面。R
library(shiny)
# Define UI for slider demo application
shinyUI(fluidPage(
# Application title
titlePanel("Sliders"),
sidebarLayout(
sidebarPanel(
# Specification of range within an interval
sliderInput("range", "Range:",
min = 1, max = 1000, value = c(200,500))
),
# A table summarizing the values entered
mainPanel(
tableOutput("values")
)
)
))
在上面的示例中,我读取了数据,并且有一个滑块,但是没有像我想要的那样对数据进行过滤。
这是我拆分和过滤的最佳尝试,但它抛出了一个错误:
myrange <- reactive({ data.frame(strsplit(Value = input$slider))})
data <- data[data$colname <= as.numeric(myrange[2]),]
data <- data[data$colname >= as.numeric(myrange[2]),]
答:
6赞
Pork Chop
1/9/2015
#1
如果我理解您的问题正确,您希望根据滑块输入权限的两个限制对数据进行子集化。如果是这样,请将以下解决方案与示例数据一起使用
library(shiny)
# Sample Data
area <- seq(from=10,to=100, by=10);peri <- seq(from=2710.1,to=2800.1, by=10)
shape <- seq(from=0.1,to=1, by=0.1);perm <- seq(from=1,to=100, by=10)
my_data <- as.data.frame(cbind(area,peri,shape,perm))
ui = fluidPage(
sidebarPanel(sliderInput("range", "Select Range",min = 1, max = 10, value = c(2, 5)),width = 3),
mainPanel(tableOutput("view"))
)
server = function(input, output) {
output$view <- renderTable({
test <- my_data[input$range[1]:input$range[2],]
test},include.rownames=FALSE)
}
runApp(list(ui = ui, server = server))
评论
strsplit
input$slider
"50 100"
data.frame(unlist(strsplit()))
input$slider
input$range
num [1:2] 200 614
input$range[1]
input$range[2]
data <- reactive({data[data$somecolumn <= input$range[2],]})
Error in x[subset & !is.na(subset)] : object of type 'closure' is not subsettable
data
data <- read.csv('some_data_to_filter.csv')
data <- reactive(...)
data.subset <- reactive(...)