dynamic filtering with input_select() using ggvis in R

陌路散爱 提交于 2019-12-01 18:46:57

问题


I'm using the built in "cocaine" database that comes with the ggvis package in R to visualize the potency counts of cocaine in each state. The R package dplyr was also used.

Here's the first six lines of the cocaine dataset:

 state potency weight month price
1    WA      77    217     1  5000
2    CT      51    248     1  4800
3    FL      68     43     1  3500
4    OH      69    123     1  3500
5    MS      75    118     1  3400
6    VA      73    127     1  3000

The goal was to use input_select()within the ggvis package to create a drop down menu where one could select various states and see a histogram of the potency counts for that state. We managed to do that with this code:

state <- as.vector(unique(cocaine$state))

cocaine %>%
          ggvis(~potency) %>%
            filter(state == eval(input_select(
            choices = state,
            selected = "NY",
            label = "State"))) %>%
          layer_histograms(binwidth = 2)

The question is why exactly we need the expression input_select() to be "evaluated" by eval(). A guess may be that becausefilter is a function from the dplyr package and thus is not communicating in the environment with ggvis; eval therefore initializes it within the ggvis environment. Perhaps someone can chime in with a concept that may help us visualize this?

来源:https://stackoverflow.com/questions/25891020/dynamic-filtering-with-input-select-using-ggvis-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!