闪亮的反跳不会呈现初始情节

时间:2019-04-01 20:14:56

标签: r shiny debounce

如果我在get_data()反应式表达式中添加去抖动,则第一次获取数据时不会绘制图。但是,更改数据(通过选择新的mpg)会导致绘制图。为什么是这样?有解决方法吗? 这是一个演示问题的简单示例。尝试删除%>% debounce(500),看看它是否可以按预期工作:

if (interactive()) {
  options(device.ask.default = FALSE)

  library(shiny)
  library(magrittr)

  ui <- fluidPage(
    selectInput("select", label = "select mpg", choices = c(mtcars$mpg, ""), selected = ""),
    plotOutput("plot")
  )

  server <- function(input, output, session) {
    get_data <- reactive({
      req(input$select)
      mtcars[mtcars$mpg == input$select,]
      }) %>% debounce(500)

    get_plot <- reactive({
      data <- get_data()
      print(data)
      plot(get_data())
      }) 

      output$plot <- renderPlot({
        get_plot()
      })
  }

  shinyApp(ui, server)
}

1 个答案:

答案 0 :(得分:0)

这里发生的事情。我认为我们不允许在选择输入中包含重复项。 export class DataService { constructor(protected http: HttpClient, protected logger: NGXLogger, protected translate: TranslateService) {} get<T>(id?: string, options?: HttpOptions): Observable<T> { const caller = findCaller(new Error()); const uriWithId = id ? `/${id}` : ''; const requestUrl = this.addGlobalQueryParams(`${this.url}${uriWithId}`); this.logger.debug(`DataService - GET method [${caller}] - START - URL:: ${requestUrl}`); return this.http.get<T>(requestUrl, options).pipe(map((response: any) => { this.logger.debug(`DataService - GET method [${caller}] - END - URL:: ${requestUrl} - Response:: `, response); return response; })); } getAll<T>(url?: string, options?: HttpOptions): Observable<T> { const caller = findCaller(new Error()); if (url) { this.logger.debug(`DataService - POST method [${caller}] - START - URL:: `, url); return this.http.post<T>(url).pipe(map((response: any) => { this.logger.debug(`DataService - POST method [${caller}] - END - URL:: ${url} - Response:: `, response); return response; })); } } } 中包含重复的值。将初始值设置为mtcars$mpg也会引起奇怪的行为。如果您确实希望初始图连同去抖动一起为空,则可以将其设置为""。这就是它的样子。

" "

否则,如果您可以进行初始绘图,可以进行以下工作。注意使用if (interactive()) { options(device.ask.default = FALSE) library(shiny) library(magrittr) ui <- fluidPage( selectInput("select", label = "select mpg", choices = c(" ",unique(mtcars$mpg)),selected = " "), plotOutput("plot") ) server <- function(input, output, session) { get_data <- reactive({ req(input$select) if(!is.na(as.numeric(input$select))) mtcars[mtcars$mpg == input$select,] else NULL }) %>% debounce(500) get_plot <- reactive({ req(get_data()) data <- get_data() print(data) plot(get_data()) }) output$plot <- renderPlot({ get_plot() }) } shinyApp(ui, server) }

很重要
unique()

我什至尝试用if (interactive()) { options(device.ask.default = FALSE) library(shiny) library(magrittr) ui <- fluidPage( selectInput("select", label = "select mpg", unique(mtcars$mpg)), plotOutput("plot") ) server <- function(input, output, session) { get_data <- reactive({ req(input$select) mtcars[mtcars$mpg == input$select,] }) %>% debounce(500) get_plot <- reactive({ req(get_data()) data <- get_data() print(data) plot(get_data()) }) output$plot <- renderPlot({ get_plot() }) } shinyApp(ui, server) } 替换选择输入。这仍然引起问题。