试图使滑块反应

Trying to make a slider reactive

我第一次尝试实现 shiny 和 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)
 )
))

试试这个:

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

所以诀窍是将 dplyr 中的 filter 函数与 evalggvis 中的 input_slider 函数一起使用。