试图使滑块反应

时间:2017-10-26 20:59:16

标签: r shiny ggvis

我正试图第一次实现闪亮和ggvis。

我想添加一个简单的滑块来过滤mtcars数据集中的mpg。

server.R

library(shiny)
library(ggvis)
library(dplyr)

data("mtcars")


shinyServer(function(input, output){

filtercar <- reactive({

mpgs <- input$mpg

m <- mtcars %>%
  filter(
  mpg >= mpgs
  )
m <- as.data.frame(m)
})


mtcars %>%
ggvis(~mpg, ~disp, fill := "red") %>%
layer_points() %>%
bind_shiny("p", "p_ui")
})

ui.R

library(ggvis)
library(shiny)

shinyUI(bootstrapPage(
wellPanel(ggvisOutput("p"),
        uiOutput("p_ui"),
        HTML("Comparing cars by disp and mpg")),
wellPanel(HTML("CARS")),
wellPanel(
h4("Filter"),
sliderInput("mpg","miles per gallon", value = 20, min = 0, max = 100, step = 
1)
 )
))

1 个答案:

答案 0 :(得分:1)

试试这个:

ui.R:

library(ggvis)
library(shiny)

shinyUI(bootstrapPage(
  wellPanel(
    uiOutput("p_ui"),
    ggvisOutput("p"),
    HTML("Comparing cars by disp and mpg")
  )))

server.R:

library(shiny)
library(ggvis)
library(dplyr)

shinyServer(function(input, output) {
  mtcars %>%
    ggvis(~mpg, ~disp) %>%
    filter(mpg > eval(input_slider(10, 35, 10))) %>%  # this is the trick
    layer_points() %>%
    scale_numeric("x", domain = c(10, 35)) %>%  # keep axis stable
    scale_numeric("y", domain = c(0, 500)) %>%  # same
    bind_shiny("p", "p_ui") 
})

因此,诀窍是使用filter中的dplyr函数和eval以及input_slider中的ggvis函数。