English 中文(简体)
在Shiny app实施实时并取消纽芬兰语
原标题:Implement real time and cancel button in Shiny app

我有这份载有两份备选办法手册和《Tiempo Real》的资料。

备选办法手册:在选择手册时,你将选择Fecha Inicio和Fecha Fin,你可以提出两种选择:Lanzar Consulta或Cancelar Consulta。 第一是建立一个数据框架,有两栏“Fcha”和“valor”,第二栏则在你想要第一个纽扣时使用(例如,你错了日期)。

选择Tiempo Real:在你选择时,你有材料Switch(Switch)(每5秒)在你点首点(Lanzar Consulta)时重新找回,而Cancelar Consulta Button则将被用来阻止材料S withch的实时复读。

而且,当选择手册改写时,我要说的是真实的,反之亦然,结果是干净的。

因此,我希望达到这一条件,但光辉的法典却看不到。 我不得不质疑改变选择和执行取消的纽特。

library(shiny)
library(shinyjs)
library(shinyWidgets)
library(lubridate)

ui <- fluidPage(
  
  useShinyjs(),
  sidebarPanel(
    selectInput("fecha", "Fecha", choices = c("Manual", "Tiempo Real")),
    
    conditionalPanel("input.fecha ==  Manual ",
                     airDatepickerInput("ini_manual", label = "Fecha Inicio", value = Sys.time() - 3 * (60 * 60), 
                                        addon =  none ,  timepicker = TRUE,  firstDay = 1,
                                        width = "200px", autoClose = TRUE,  timepickerOpts = timepickerOptions(timeFormat = "hh:ii")),
                     airDatepickerInput("fin_manual", label = "Fecha Fin", value = Sys.time(), 
                                        addon =  none , timepicker = TRUE, firstDay = 1, 
                                        width = "200px", autoClose = TRUE, maxDate = NULL,  timepickerOpts = timepickerOptions(timeFormat = "hh:ii"))
    ),
    
    conditionalPanel("input.fecha ==  Tiempo Real ",
                     materialSwitch(inputId = "refresh", label = "Tiempo Real", value = TRUE, status = "success")
                     
    ),
    actionButton("btnConsulta", "Lanzar Consulta"),
    actionButton("btnCancelarConsulta", "Cancelar Consulta")
  ),
  
  mainPanel(
    tableOutput("tablaResultado")
    
  )
)


server <- function(input, output, session) {
  
  # Reactive variable to store the last update time in real time
  last_refresh_time <- reactiveVal(NULL)
  
  # Reactive event for query button and real time
  consulta_evento <- eventReactive(input$btnConsulta,
                                   {
                                     if (input$fecha == "Manual") {
                                       # Get fecha_ini and fecha_fin
                                       fecha_ini <- input$ini_manual
                                       fecha_fin <- input$fin_manual
                                       
                                       # Make data frame 
                                       resultado <- data.frame(
                                         fecha = as.character(seq.POSIXt(from = fecha_ini, to = fecha_fin, by = "hour")),
                                         Valor = rnorm(length(seq.POSIXt(from = fecha_ini, to = fecha_fin, by = "hour")))
                                       )
                                       
                                       resultado
                                       
                                     } else {
                                       
                                       last_refresh_time(Sys.time())
                                       # Realizar la lógica de la consulta para tiempo real aquí
                                       consulta_tiempo_real <- data.frame(
                                         fecha = as.character(seq.POSIXt(from = Sys.time(), by = "hour", length.out = 5)),
                                         Valor = rnorm(5)
                                       )
                                       
                                       consulta_tiempo_real
                                       
                                       
                                     }
                                   }
  )
  
  
  
  # Update real time with JS
  observe({
    if (input$fecha == "Tiempo Real" && input$refresh == TRUE) {
      shinyjs::runjs(
         consultaInterval = setInterval(function() { Shiny.setInputValue("btnConsulta", Math.random(), {priority: "event"}); }, 5000); 
      )
    }
    
    else if (input$fecha == "Tiempo Real" && input$refresh == FALSE){
      shinyjs::runjs( clearInterval(consultaInterval); )
      output$tablaResultado <- renderTable({
        data.frame(NULL)
      })
    }
    else{
      consulta_evento()
    }
    
    
  })
  
  
  output$tablaResultado <- renderTable({
    req(input$btnConsulta)
    
    consulta_evento()
  })
  
  
  
}

shinyApp(ui, server) 

我要就如何处理这一问题获得指导。

问题回答

我并不完全理解你的问题和你的意见,因此我的答复可能不完整,而且(或)不是你所期望的。 在该案中,请作一评论,告诉我我我我什么是错的,我要更新我的回答。

首先,有几句。 这不能奏效:

      shinyjs::runjs(
         consultaInterval = setInterval(function() { Shiny.setInputValue("btnConsulta", Math.random(), {priority: "event"}); }, 5000); 
      )
    }
......
      shinyjs::runjs( clearInterval(consultaInterval); )

因为 Java印有两种不同的<密码>光js:runjs的指令相互之间没有沟通。

此外,<条码>btnConsulta 是Shiny action button的背书,其价值在每点击时都得到了提高,因此,我对<条码>输入$btnConsulta的行为没有想法。 如果将行动纽吨价值和<代码>Shiny.setInputValue(“btnConsulta”结合起来。

在您的观察员结束时,您:

    else{
      consulta_evento()
    }

consulta_evento()> 回归代码<>consulta_evento<>code>。 因此,该法典没有任何规定。 我的猜测是,你想要填写consulta_evento(data.frame(NUL)

现在,这是我的解决办法。 Save the Javacast Code below in the file refresh.js 网址:www。 附录:

$(document).ready(function() {
  let intervalId;
  let $switch = $("#refresh");
  let $select = $("#fecha");
  let $cancel = $("#btnCancelarConsulta");
  function refresh() {
    intervalId = setInterval(function() {
      Shiny.setInputValue("observe", true, {priority: "event"});
    }, 5000);
  }
  function stopRefresh() {
    if(intervalId) {
      clearInterval(intervalId);
      intervalId = null;
      Shiny.setInputValue("clear", true, {priority: "event"});
      $switch.prop("checked", false);
    }
  }
  if($select.val === "Tiempo Real") {
    if($switch.prop("checked")) {
      refresh();
    } else {
      stopRefresh();
    }
  } else {
    stopRefresh();
  }
  $cancel.on("click", function() {
    stopRefresh();
  });
  $switch.on("change", function() {
    if($select.val() === "Tiempo Real") {
      if($switch.prop("checked")) {
        refresh();
      } else {
        stopRefresh();
      }
    }
  });
  $select.on("change", function() {
    if($select.val() === "Manual") {
      stopRefresh();
    }
  });
});

这里指的是:

library(shiny)
library(shinyWidgets)
library(lubridate)

ui <- fluidPage(
  tags$head(tags$script(src = "refresh.js")),
  
  sidebarPanel(
    selectInput("fecha", "Fecha", choices = c("Manual", "Tiempo Real")),
    
    conditionalPanel(
      "input.fecha ==  Manual ",
      airDatepickerInput("ini_manual", label = "Fecha Inicio", value = Sys.time() - 3 * (60 * 60), 
                         addon =  none ,  timepicker = TRUE,  firstDay = 1,
                         width = "200px", autoClose = TRUE,  timepickerOpts = timepickerOptions(timeFormat = "hh:ii")),
      airDatepickerInput("fin_manual", label = "Fecha Fin", value = Sys.time(), 
                         addon =  none , timepicker = TRUE, firstDay = 1, 
                         width = "200px", autoClose = TRUE, maxDate = NULL,  timepickerOpts = timepickerOptions(timeFormat = "hh:ii"))
    ),
    
    conditionalPanel(
      "input.fecha ==  Tiempo Real ",
      materialSwitch(
        inputId = "refresh", label = "Tiempo Real", value = TRUE, status = "success"
      )
    ),
    
    actionButton("btnConsulta", "Lanzar Consulta"),
    actionButton("btnCancelarConsulta", "Cancelar Consulta")
  ),
  
  mainPanel(
    tableOutput("tablaResultado")
  )
)


server <- function(input, output, session) {
  
  # Reactive variable to store the last update time in real time
  last_refresh_time <- reactiveVal(NULL)
  
  consulta_evento <- reactiveVal(data.frame(NULL))
  
  # Reactive event for query button and real time
  observeEvent(
    list(input$btnConsulta, input$observe),
    {
      if(input$fecha == "Manual") {
        # Get fecha_ini and fecha_fin
        fecha_ini <- input$ini_manual
        fecha_fin <- input$fin_manual
        # Make data frame 
        resultado <- data.frame(
          fecha = as.character(seq.POSIXt(from = fecha_ini, to = fecha_fin, by = "hour")),
          Valor = rnorm(length(seq.POSIXt(from = fecha_ini, to = fecha_fin, by = "hour")))
        )
        
        consulta_evento(resultado)
        
      } else {
        
        last_refresh_time(Sys.time())
        # Realizar la lógica de la consulta para tiempo real aquí
        consulta_tiempo_real <- data.frame(
          fecha = as.character(seq.POSIXt(from = Sys.time(), by = "hour", length.out = 5)),
          Valor = rnorm(5)
        )
        
        consulta_evento(consulta_tiempo_real)
        
      }
    }
  )
  
  observeEvent(input$clear, {
    consulta_evento(data.frame(NULL))
  })

  
  output$tablaResultado <- renderTable({
    consulta_evento()
  })
  
}

shinyApp(ui, server) 




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签