是否可以从一个反应数据源创建两个ggivs图表?

时间:2022-03-06 14:26:21

For example

例如

dataReactive<-reactive({data[, c(input$selectCol)]})


dataReactive %>%
    ggvis(x=~x,y=~y) %>%
    layer_points() %>%
    bind_shiny("plot1")

dataReactive %>%
    ggvis(x=~x) %>%
    layer_histograms(width=1) %>%
    bind_shiny("plot2")

When you try to output,in the UI, it will only produce the first plot bound with bind_shiny? in the server. Is this a bug or is there a work around. I believe it might have something to do with how reactive is updating.

当你尝试输出时,在UI中,它只会产生第一个用bind_shiny绑定的图?在服务器中。这是一个错误还是有解决方法。我相信这可能与反应更新的方式有关。

1 个解决方案

#1


1  

Yes this is possible, and you should post your complete code otherwise there is no way to determine what is causing the issue for you. Here's a very simple example:

是的,这是可能的,您应该发布完整的代码,否则无法确定导致问题的原因。这是一个非常简单的例子:


ui.R

ui.R

library(shiny)
library(ggvis)
##
shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId="selectCol",
        label="y-variable",
        choices=(names(data))
      )
    ),
    mainPanel(
        ggvisOutput("plot1"),
        br(),
        ggvisOutput("plot2")   
    )
  )
))

server.R

server.R

library(shiny)
library(ggvis)
##
shinyServer(function(input, output) {
  cols <- names(data)
  colIdx <- reactive({ 
    match(c("x",input$selectCol),cols)
  })

  dataReactive <-reactive({
    df <- data[, colIdx()]
    names(df) <- c("x","y")
    df
  })

  dataReactive %>%
    ggvis(x=~x,y=~y) %>%
    layer_points() %>%
    bind_shiny("plot1")

  dataReactive %>%
    ggvis(x=~x) %>%
    layer_histograms(width=1) %>%
    bind_shiny("plot2")
})

global.R

global.R

library(shiny)
library(ggvis)
##
set.seed(123)
data <- data.frame(
  x=sample(1:50,40,replace=TRUE),
  col2=rnorm(40,1,5),
  col3=rexp(40,3))

And here is what the application looks like when running through the external browser: 是否可以从一个反应数据源创建两个ggivs图表?

以下是在外部浏览器中运行时应用程序的外观:

#1


1  

Yes this is possible, and you should post your complete code otherwise there is no way to determine what is causing the issue for you. Here's a very simple example:

是的,这是可能的,您应该发布完整的代码,否则无法确定导致问题的原因。这是一个非常简单的例子:


ui.R

ui.R

library(shiny)
library(ggvis)
##
shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId="selectCol",
        label="y-variable",
        choices=(names(data))
      )
    ),
    mainPanel(
        ggvisOutput("plot1"),
        br(),
        ggvisOutput("plot2")   
    )
  )
))

server.R

server.R

library(shiny)
library(ggvis)
##
shinyServer(function(input, output) {
  cols <- names(data)
  colIdx <- reactive({ 
    match(c("x",input$selectCol),cols)
  })

  dataReactive <-reactive({
    df <- data[, colIdx()]
    names(df) <- c("x","y")
    df
  })

  dataReactive %>%
    ggvis(x=~x,y=~y) %>%
    layer_points() %>%
    bind_shiny("plot1")

  dataReactive %>%
    ggvis(x=~x) %>%
    layer_histograms(width=1) %>%
    bind_shiny("plot2")
})

global.R

global.R

library(shiny)
library(ggvis)
##
set.seed(123)
data <- data.frame(
  x=sample(1:50,40,replace=TRUE),
  col2=rnorm(40,1,5),
  col3=rexp(40,3))

And here is what the application looks like when running through the external browser: 是否可以从一个反应数据源创建两个ggivs图表?

以下是在外部浏览器中运行时应用程序的外观: