简体   繁体   English

在R Shiny中如何分离滑块输入值并使用它们进行过滤

[英]In R Shiny How to Separate Slider Input Values and Filter with them

In R's shiny package you can have a slider widget using sliderInput that has an upper and lower bound. 在R的shiny包中,您可以使用具有上限和下限的sliderInput创建滑块小部件。

I would like to be able to separate the upper and lower bound values and filter a column of data with them, before passing that data to UI.R . 在将数据传递给UI.R之前,我希望能够分离上限和下限值并使用它们过滤一列数据。

For example: 例如:

Server.R Server.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()
  })
})

UI.R UI.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")
    )
  )
))

In the example above I read in the data and I have a slider, but there's no filtering of the data like I'd like there to be. 在上面的例子中我读了数据,我有一个滑块,但没有像我想要的那样过滤数据。

This was my best attempt to split and filter, but it throws an error: 这是我分割和过滤的最佳尝试,但它会抛出一个错误:

myrange <- reactive({ data.frame(strsplit(Value = input$slider))})
data <- data[data$colname <= as.numeric(myrange[2]),]
data <- data[data$colname >= as.numeric(myrange[2]),]

If I understand your question right you want to subset the data based on the two limits of the slider input right. 如果我理解你的问题,你想根据滑块输入权限的两个限制对数据进行子集化。 If so, use the solution below with sample data 如果是这样,请使用下面的解决方案和样本数据

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)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM