簡體   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