繁体   English   中英

使用反应功能作为ggvis输入

[英]Use reactive function as ggvis input

我有一个Shiny应用程序,使用CSV作为输入并在按下按钮时加载。

 shinyServer(function(input, output) {

  dataInput <- reactive({
    if(is.null(input$file)) {
      return(NULL)
    }
    butt$a
  })

  butt <- reactiveValues()

  observe({
    if (input$goButton == 0) {
      butt$a <- return(NULL)
    }
    if (input$goButton > 0) {
      butt$a <- read.csv(input$file$datapath, 
                         header = input$header, 
                         sep = input$sep, quote = input$quote)
    }
  })

我想使用dataInput()作为ggvis图的输入:

  output$ggvisplot_ui <- renderUI({
    if(is.null(butt$a)) {
      return(NULL)
    }
    ggvisOutput("ggvisplot")
  })

  reactive({
  dl <- dataInput()
  dl %>%
    ggvis(~mpg, ~wt) %>% 
    layer_points() %>% 
    bind_shiny("ggvisplot")
  })

在这里我的CSV输入mtcars.csv所以我用~mpg~wt为列。 如果我拿出dl <- dataInput() reactive({ })部分,将dl <- dataInput()替换为dl <- mtcars它就可以正常工作:

  output$ggvisplot_ui <- renderUI({
    if(is.null(butt$a)) {
      return(NULL)
    }
    ggvisOutput("ggvisplot")
  })

  dl <- mtcars
  dl %>%
    ggvis(~mpg, ~wt) %>% 
    layer_points() %>% 
    bind_shiny("ggvisplot")

这有效:

  output$ggvisplot_ui <- renderUI({
    if(is.null(butt$a)) {
      return(NULL)
    }
    ggvisOutput("ggvisplot")
  })

  observe({
    if (!is.null(butt$a)) {
      dl <- dataInput()
      dl %>%
        ggvis(~mpg, ~wt) %>% 
        layer_points() %>% 
        bind_shiny("ggvisplot")
    }
  })

暂无
暂无

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

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