簡體   English   中英

ggvis情節隨意消失

[英]ggvis plot disappears at random Shiny

我有一個奇怪的問題在閃亮。 我的閃亮應用程序有一個帶有layer_points() ggvis圖和幾個操作圖的選項。 當我運行我的應用程序時,即使我更改所有選項,有時一切都很好,但有時(我認為沒有具體的規則)情節消失。 當我更改其中一個選項時,Plot會回來,但它並不酷。 我研究這個問題,但我真的不知道它是否是我的問題的解決方案。 當情節消失時,我的Shiny應用程序看起來像:

在此輸入圖像描述

這是我的代碼:

ui.R

library(ggvis)
library(markdown)
library(shiny)
library(dplyr)
library(magrittr)

shinyUI(
    fluidPage(
      h3("Title"),
      fluidRow(
        column(3,
               wellPanel(
                 radioButtons("radio",h5("Select"),choices=list("All values","Selected values"),
                              selected="All values"),

                 conditionalPanel(
                   condition = "input.radio != 'All values'",
                   checkboxGroupInput("checkGroup",label = "",
                                      choices,
                                      selected = c("AT1","AT2"))
                 ),
                 hr(),
                 radioButtons("dataset", label = h5("Drilldown"),
                              choices = list("2 Level" = "df1", "3 Level" = "df2")

                 ),
                 hr(),
                 h5("Choice"),
                 selectInput("xvar", h6(""), 
                             axis_vars_x,
                             selected = "value"),
                 selectInput("yvar", h6(""), 
                             axis_vars_y,
                             selected = "number2"),
                 hr(),
                 uiOutput("slider")
               )
        ),
        column(9,
               ggvisOutput("plot")
        )
      )

    )
  )

server.R

library(shiny)

shinyServer(function(input, output,session) {

  datasetInput <- reactive({
    switch(input$dataset,
           df2 = df2,
           df1 = df1)
  })

  axis_vara_y <- reactive({
    switch(input$yvar,
           number = 2,
           number2 = 3)
  }) 


  output$slider <- renderUI({
    sliderInput("inslider",h5(""), min   = round(min(datasetInput()[,axis_vara_y()]),0)-1, 
                max   = round(max(datasetInput()[,axis_vara_y()]),0)+1,
                value = c(round(min(datasetInput()[,axis_vara_y()]),0)-1, 
                          round(max(datasetInput()[,axis_vara_y()]),0)+1),
                step  = 0.5)
  })

  data <- reactive({
    filteredData <- datasetInput()
    axisData <- axis_vara_y()

    if(!is.null(input$inslider)){
      if(input$radio == "All values"){
        filteredData <- filteredData %>%
          filter(filteredData[,axisData] >= input$inslider[1],
                 filteredData[,axisData] <= input$inslider[2])
      }
      else {
        filteredData <- filteredData %>%
          filter(value %in% input$checkGroup,
                 filteredData[,axisData] >= input$inslider[1],
                 filteredData[,axisData] <= input$inslider[2])
      }
    }
    return(filteredData)
  })  

  data_point <- reactive({
    data() %>%
      mutate(id = row_number())

  })

  xvar <- reactive(as.symbol(input$xvar))
  yvar <- reactive(as.symbol(input$yvar))

  dotpoint_vis <- reactive({

      xvar_name <- names(axis_vars_x)[axis_vars_x == input$xvar]
      yvar_name <- names(axis_vars_y)[axis_vars_y == input$yvar]


    data_point_detail <- data_point()

    plot <- data_point_detail %>%
      ggvis(x = xvar(),y =  yvar()) %>%
      layer_points(size := 120,fill = ~value) %>%
      add_axis("x", title = xvar_name) %>%
      add_axis("y", title = yvar_name) %>%
      set_options(width = 750, height = 500, renderer = "canvas") 

  })
  dotpoint_vis %>% bind_shiny("plot")

})

global.R

choices <- list("Value1" = "AT1", "Value2" = "AT2",
                "Value3" = "AT3", "Value4" = "AT4",
                "Value5" = "AT5", "Value6" = "RT1",
                "Value7" = "AT6", "Value8" = "AT7",
                "Value9" = "AT8", "Value10" = "AT9",
                "Value11" = "AT10", "Value12" = "RT2")

levele <- c("AT1","AT2","AT3","AT4","AT5","RT1","AT6","AT7","AT8","AT9","AT10","RT2")

df1 <- data.frame(value = levele,number = seq(2,46,4), number2 = seq(2,24,2),order = 1:12) 

df2 <- data.frame(value = levele,number = rep(4:15), number2 = rep(4:9,each = 2),order = 1:12) 

df1$value <- factor(df1$value, levels = levele)
df2$value <- factor(df2$value, levels = levele)

axis_vars_y <- c("number","number2")
axis_vars_x <- c("value", "order","number","number2")

更新

我也不知道ggvis中動畫發生了什么。

這個問題最初難以重現,但我發現我可以通過在All Values和Selected Values之間來回點擊來重現它。 在兩個單選按鈕之間進行一些切換后,圖表會消失或重新出現,但它看似隨機變化 - 有時需要4次點擊才能使圖表消失或重新出現,有時需要2次點擊或其他一些點擊次數。

bind_shiny()或ggvisOutput()中必定存在錯誤 ,因為以下更改會創建一個似乎不會消失的圖形:

在ui.R中,進行以下更改:

   # ggvisOutput("plot")
   plotOutput('plot')

在server.R中,進行以下更改:

plot(data_point_detail[ , c(input$xvar, input$yvar)], xlab=xvar_name, ylab=yvar_name)
#     plot <- data_point_detail %>%
#       ggvis(x = xvar(),y =  yvar()) %>%
#       layer_points(size := 120,fill = ~value) %>%
#       add_axis("x", title = xvar_name) %>%
#       add_axis("y", title = yvar_name) %>%
#       set_options(width = 750, height = 500, renderer = "canvas") 
#     plot

output$plot <- renderPlot(dotpoint_vis())
# dotpoint_vis %>% bind_shiny("plot")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM