【问题标题】:Unable to render googlevis charts twice through R shiny dynamic UI无法通过 R 闪亮的动态 UI 两次渲染 googlevis 图表
【发布时间】:2016-08-28 12:08:36
【问题描述】:

有这段代码。我无法让它绘制图表两次。 尝试按以下顺序输入文本框的值:

  1. 1、2 和 6
  2. 1、6 和 2

例如 1,它只绘制一次图表。例如2,它只绘制了两次(因为它们是不同的图表) 有什么提示可以让我根据用户的需要多次工作吗?

ui <- fluidPage(   
tagList( textInput(inputId = "textbox", label = NULL,value = ""),actionButton("go", "Go"),uiOutput("ui")))

server <- function(input, output) {     

observeEvent(input$go, {
output$plot1 <- renderGvis({
  df <- data.frame(country=c("US", "GB", "BR"),
                   val1=c(10,33,44),
                   val2=c(23,122,342))

  Sys.sleep(0.3)
  gvisBarChart(df, xvar="country", yvar=c("val1", "val2"),
               options=list(isStacked=TRUE, height = 300, width = 400))

})

output$plot2 <- renderGvis({
  df <- data.frame(country=c("IND", "RUS", "BR"),
                   val1=c(10,3333,244),
                   val2=c(2344,122,342))

  Sys.sleep(0.3)
  gvisBarChart(df, xvar="country", yvar=c("val1", "val2"),
               options=list(isStacked=TRUE, height = 300, width = 400))
  })
      output$ui <- renderUI({

        if(isolate(as.numeric(input$textbox)) %in% c(1,2,3)){
          box(title = "ABC", width = 10, height = 550, htmlOutput("plot1",height = 500))    
        }else{
          box(title = "DEF", width = 4, height = 550, htmlOutput("plot2",height = 500))    

        }


      })
    })  
  }

【问题讨论】:

    标签: r shiny googlevis dynamic-ui


    【解决方案1】:

    在专家的帮助下,我能够理解为什么会发生这种情况。问题在于 html 试图找出正在呈现的图表。只有当我们有动态 UI 并且调用相同的 googlevis 图表时,它才会出现。 由于某种原因,它不知道正在引用哪个图表,因此不会填充。我当然不是很精通 HTML,我的陈述可能不是结论性的。 不过,它通过在渲染图表时添加图表 ID 来工作。

    粘贴下面的代码,希望对以后的人也有帮助。

    server <- function(input, output) {     
    
    observeEvent(input$go, {
    output$plot1 <- renderGvis({
    df <- data.frame(country=c("US", "GB", "BR"),
                   val1=c(10,33,44),
                   val2=c(23,122,342))
    
    Sys.sleep(0.3)
    gvisBarChart(df, xvar="country",chartid = "plot1",
    yvar=c("val1", "val2"),
               options=list(isStacked=TRUE, height = 300, width = 400))
    
    })
    
    output$plot2 <- renderGvis({
    df <- data.frame(country=c("IND", "RUS", "BR"),
                   val1=c(10,3333,244),
                   val2=c(2344,122,342))
    
    Sys.sleep(0.3)
    gvisBarChart(df, xvar="country", chartid = "plot2",
                 yvar=c("val1", "val2"),
                 options=list(isStacked=TRUE, height = 300, width = 400))
    })
      output$ui <- renderUI({
    
        if(isolate(as.numeric(input$textbox)) %in% c(1,2,3)){
          box(title = "ABC", width = 10, height = 550, htmlOutput("plot1",height = 500))    
        }else{
          box(title = "DEF", width = 4, height = 550, htmlOutput("plot2",height = 500))    
    
        }
    
    
      })
    })  
    }
    

    虽然我已经解决了 googlevis 图表的问题,但我仍在努力找出如何对传单包中的地图执行相同的操作。任何指针都非常受欢迎。 谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-11
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多