我正在熟悉ggvis,我正试图在闪亮中使用它.我无法理解ggvis如何从反应性Shiny表达式中获取数据.这是ggvis GitHub存储库的基本应用程序:
ui.R:
shinyUI(pageWithSidebar(
div(),
sidebarPanel(
sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),
value = 10, step = 1),
uiOutput("plot_ui")
),
mainPanel(
ggvisOutput("plot"),
tableOutput("mtc_table")
)
))
Run Code Online (Sandbox Code Playgroud)
server.R:
library(ggvis)
shinyServer(function(input, output, session) {
# A reactive subset of mtcars
mtc <- reactive({ mtcars[1:input$n, ] })
# A simple visualisation. In shiny apps, need to register observers
# and tell shiny where to put the controls
mtc %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
bind_shiny("plot", "plot_ui")
output$mtc_table <- renderTable({
mtc()[, c("wt", "mpg")]
})
})
Run Code Online (Sandbox Code Playgroud)
现在mtc
是反应式表达式,它实际上是一个函数(或者是它?),它的结果是一个data.frame.然而,它作为一个功能用管道传输给ggvis.如果您尝试传递结果data.frame之类的
mtc() %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
bind_shiny("plot", "plot_ui")
Run Code Online (Sandbox Code Playgroud)
Shiny会开始抱怨"没有活跃的反应环境就不允许操作".那么究竟发生了什么?
我问的原因是我想返回我想在ggvis中使用的其他对象.更确切地说,我想更改x和y轴标签,其中标签是在反应式表达式内部计算的,如下所示:
mtc <- reactive({ list(data=mtcars[1:input$n, ],
labx = "Computed x axis label",
laby = "Computed y axis label")
})
mtc %>% ggvis(data=data,~wt,~mpg) %>%
layer_points() %>%
add_axis("x",title=labx) %>%
add_axis("y",title=laby) %>%
bind_shiny("plot", "plot_ui")
Run Code Online (Sandbox Code Playgroud)
是否有可能以某种方式利用mtc()
ggvis调用中的输出结构?或者只能传递data.frame,然后将数据放入data.frame中?
或者是否有另一种注册ggvis对象的方法?在这个问题中,ggvis输出是在observe_ggvis
函数中注册的,但它似乎在当前的ggvis版本(0.3)中不存在.
我在R 3.1.1上使用ggvis版本0.3.0.1和闪亮0.10.0
div*_*ero 18
ggvis可以传递给数据集的"裸反应".当你这样做时,ggvis会在数据发生变化时自动重新绘制数据,但不需要重新绘制整个图:
get_rct_df = reactive({ #code to change dataset from shiny input )}
get_rct_df %>% ggvis() #rest of plotting code %>% bind_shiny("plot")
Run Code Online (Sandbox Code Playgroud)
每次get_rct_df中的数据发生变化时,这将更新绘图中的数据点(但不会重绘整个绘图).这也意味着如果不重新绘制整个绘图(绘图标签,layer_model_predictions的等式),某些内容无法更新.
您可以执行其他建议并将整个图表包含在被动反应中:
reactive({
get_rct_df %>%
ggvis() #rest of plotting code %>%
add_axis("x", title = input$title)
}) %>% bind_shiny("plot")
Run Code Online (Sandbox Code Playgroud)
这将允许您更新绘图标题和绘图的其他部分.但它也迫使ggvis重新绘制整个情节,而不仅仅是重新绘制数据.如果你测试两种方法,方法1看起来会"更平滑"; 当数据发生变化时,ggvis内置了过渡动画.
归档时间: |
|
查看次数: |
3748 次 |
最近记录: |