简体   繁体   English

如何将数据从反应性Shiny表达式传递到ggvis图?

[英]How is data passed from reactive Shiny expression to ggvis plot?

I am getting acquainted with ggvis and I am trying to use it in shiny. 我正在熟悉ggvis,我正试图在闪亮中使用它。 I am having trouble understanding how ggvis gets the data from the reactive Shiny expression. 我无法理解ggvis如何从反应性Shiny表达式中获取数据。 Here is the basic app from ggvis GitHub repository: 这是ggvis GitHub存储库的基本应用程序:

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

server.R: 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")]
  })
})

Now mtc is the reactive expression which is actually a function (or is it?), Its result is a data.frame. 现在mtc是反应式表达式,它实际上是一个函数(或者是它?),它的结果是一个data.frame。 However it is piped to ggvis as a function. 然而,它作为一个功能用管道传输给ggvis。 If you tried to pass the resulting data.frame like 如果您尝试传递结果data.frame之类的

mtc() %>%  ggvis(~wt, ~mpg) %>%
layer_points() %>%
        bind_shiny("plot", "plot_ui")

Shiny would start complaining along the lines that "Operation not allowed without an active reactive context". Shiny会开始抱怨“没有活跃的反应环境就不允许操作”。 So what is actually going on? 那么究竟发生了什么?

The reason I am asking is that I would like to return additional objects which I want to use in ggvis. 我问的原因是我想返回我想在ggvis中使用的其他对象。 To be more precise I want to change x and y axis labels, where the labels are computed inside in the reactive expression, something like this: 更确切地说,我想要更改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")

Is it possible to somehow exploit the structure of mtc() output inside of ggvis call? 是否有可能以某种方式利用ggvis调用中的mtc()输出结构? Or it is only possible to pass the data.frame and then put your data in the data.frame? 或者只能传递data.frame,然后将数据放入data.frame中?

Or is there another way to register ggvis object? 或者是否有另一种注册ggvis对象的方法? In this question the ggvis output is registered with observe_ggvis function, but it seems that it is not present in current ggvis version (0.3). 在这个问题中 ,ggvis输出使用observe_ggvis函数注册,但似乎它在当前的ggvis版本(0.3)中不存在。

I am using ggvis version 0.3.0.1 and shiny 0.10.0 on R 3.1.1 我在R 3.1.1上使用ggvis版本0.3.0.1和闪亮0.10.0

ggvis can be passed "bare reactives" for a dataset. ggvis可以传递给数据集的“裸反应”。 When you do this ggvis will automatically replot the data when it changes but it doesn't need to replot the entire plot: 当你这样做时,ggvis会在数据发生变化时自动重新绘制数据,但不需要重新绘制整个图:

get_rct_df = reactive({  #code to change dataset from shiny input  )}
get_rct_df %>% ggvis() #rest of plotting code %>% bind_shiny("plot")

This will update the data points in the plot (but not redraw the entire plot) each time the data from get_rct_df changes. 每次get_rct_df中的数据发生变化时,这将更新绘图中的数据点(但不会重绘整个绘图)。 This also means that certain things cannot be updated without redrawing the entire plot (plot labels, equations for layer_model_predictions). 这也意味着如果不重新绘制整个绘图(绘图标签,layer_model_predictions的等式),某些内容无法更新。

You can do the other suggestion and wrap the entire plot in a reactive: 您可以执行其他建议并将整个图表包含在被动反应中:

reactive({
   get_rct_df %>% 
      ggvis() #rest of plotting code %>% 
      add_axis("x", title = input$title)
}) %>% bind_shiny("plot")

This will allow you to update plot titles and other parts of the plot. 这将允许您更新绘图标题和绘图的其他部分。 But it also forces ggvis to replot the entire plot when something changes as opposed to just replotting the data. 但它也迫使ggvis在发生变化时重新绘制整个绘图,而不仅仅是重新绘制数据。 If you test out both methods, method 1 will look "smoother"; 如果你测试两种方法,方法1看起来会“更平滑”; ggvis has built in transition animations when you data changes. 当数据发生变化时,ggvis内置了过渡动画。

Ok I found the solution in this answer . 好的,我在这个答案中找到了解决方案。

mtc <- reactive({ list(data=mtcars[1:input$n, ],
    labx = "Computed x axis label",
    laby = "Computed y axis label")
})

reactive({
    dl <- mtc()
    dl$data %>% ggvis(data=data,~wt,~mpg) %>% 
    layer_points() %>%
    add_axis("x",title=dl$labx) %>%
    add_axis("y",title=dl$laby) 
 }) %>% bind_shiny("plot", "plot_ui")

The trick is that the call to reactive is evaluated hence you can separate the process of computing data for ggvis and actual ggvis plotting. 诀窍在于评估对被动的调用,因此您可以分离ggvis和实际ggvis绘图的计算数据的过程。

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

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