【问题标题】:Shiny breaks if dynamically change datasets如果动态更改数据集,闪亮的中断
【发布时间】:2015-11-17 11:09:20
【问题描述】:

我正在尝试创建一个闪亮的应用程序,根据数据集,ggvis 将创建一个散点图。该应用程序在开始时运行良好。但是如果我尝试将数据集更改为 mtcars,那么闪亮就会消失。

我的 ui.R -

library(ggvis)
library(shiny)
th.dat <<- rock

shinyUI(fluidPage(


  titlePanel("Reactivity"),

  sidebarLayout(
    sidebarPanel(

      selectInput("dataset", "Choose a dataset:", 
                  choices = c("rock", "mtcars")),
      selectInput("xvar", "Choose x", choices = names(th.dat), selected = names(th.dat)[1]),
      selectInput("yvar", "Choose y", choices = names(th.dat), selected = names(th.dat)[2]),
    selectInput("idvar", "Choose id", choices = names(th.dat), selected = names(th.dat)[3])

    ),


    mainPanel(
ggvisOutput("yup")



    )
  )
))

server.R -

  library(ggvis)
library(shiny)
library(datasets)

shinyServer(function(input, output, session) {

  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "mtcars" = mtcars)

  })


  obs <- observe({
    input$dataset
    th.dat <<- datasetInput()
    s_options <- list()
    s_options <- colnames(th.dat)

    updateSelectInput(session, "xvar",
                      choices = s_options,
                      selected = s_options[[1]]
    )
    updateSelectInput(session, "yvar",
                      choices = s_options,
                      selected = s_options[[2]]
    )
    updateSelectInput(session, "idvar",
                      choices = s_options,
                      selected = s_options[[3]]
    )
  })

  xvarInput <- reactive({
    input$dataset
    input$xvar

    print("inside x reactive," )
    print(input$xvar)

    xvar <- input$xvar
  })

  yvarInput <- reactive({
    input$dataset
    input$yvar

    print("inside y reactive,")
    print(input$yvar)

    yvar <- input$yvar
  })


  dat <- reactive({

    dset <- datasetInput()
    xvar <- xvarInput()
#    print(xvar)
    yvar <- yvarInput()
#    print(yvar)

    x <- dset[, xvar]
    y <- dset[,yvar]
    df <- data.frame(x = x, y = y)
  })

  dat %>%
    ggvis(~x, ~y) %>%
    layer_points() %>%
    bind_shiny("yup")
})

我尝试了很多方法,但仍然卡住了。任何帮助将不胜感激。

【问题讨论】:

  • 它坏了,因为没有 dset[,xvar],而且 th.dat 仍然很摇滚。当数据集更改时,您应该响应式填充下拉列表。请注意,如果您“接受”之前问题的良好答案,则获得答案的机会更高。
  • 如果您省略 ggvis 代码然后更改数据集,您将看到下拉列表相应更改。在我看来,问题是 input$xvar 和 input$yvar 的值在 dat 响应式中没有更新,我不知道如何强制。另外,我之前已经接受了我之前问题的解决方案。
  • ...解决您以前的问题之一 ....

标签: r ggplot2 shiny shiny-server ggvis


【解决方案1】:

我在 cmets 中留下了一些指示,但似乎 ggvis 很早就评估了所有内容,因此需要一些测试用例。

rm(list = ls())
library(shiny)
library(ggvis)

ui <- fluidPage(
  titlePanel("Reactivity"),
  sidebarPanel(
    selectInput("dataset", "Choose a dataset:", choices = c("rock", "mtcars")),
    uiOutput("xvar2"),uiOutput("yvar2"),uiOutput("idvar2")),
    mainPanel(ggvisOutput("yup"))
)

server <- (function(input, output, session) {

  dataSource <- reactive({switch(input$dataset,"rock" = rock,"mtcars" = mtcars)})

  # Dynamically create the selectInput
  output$xvar2 <- renderUI({selectInput("xvar", "Choose x",choices = names(dataSource()), selected = names(dataSource())[1])})
  output$yvar2 <- renderUI({selectInput("yvar", "Choose y",choices = names(dataSource()), selected = names(dataSource())[2])})
  output$idvar2 <- renderUI({selectInput("idvar", "Choose id",choices = names(dataSource()), selected = names(dataSource())[3])})

  my_subset_data <- reactive({        

    # Here check if the column names correspond to the dataset
    if(any(input$xvar %in% names(dataSource())) & any(input$yvar %in% names(dataSource())))
    {
      df <- subset(dataSource(), select = c(input$xvar, input$yvar))
      names(df) <- c("x","y")
      return(df)
    }
  })

  observe({
    test <- my_subset_data()
    # Test for null as ggvis will evaluate this way earlier when the my_subset_data is NULL
    if(!is.null(test)){
      test %>% ggvis(~x, ~y) %>% layer_points() %>% bind_shiny("yup")
    }
  })
})

shinyApp(ui = ui, server = server)

岩石的输出 1 mtcars 的输出 2

【讨论】:

  • 非常感谢您的回答,它解决了我的问题。但是有一个切向的问题。我在绘图中添加了一个链接画笔,如果我刷一个点,那么所有具有相同 id 的点也会被刷成相同的颜色。但它只在第一次起作用。如果我更改 x 或 y 变量或数据集,它就不再起作用了。你知道为什么会这样吗?
  • 很高兴它有效。 :) 老实说,我对ggvis 了解不多。我相信您可以使用if 语句实现一个测试用例,从而解决您的其他问题。您可以发布一个新问题,详细概述您的问题,以免使问题过于混乱。
猜你喜欢
  • 1970-01-01
  • 2019-07-16
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
  • 2017-08-21
  • 2017-03-21
  • 1970-01-01
  • 2022-11-15
相关资源
最近更新 更多