简体   繁体   English

R Shiny中的动作按钮

[英]action button in R shiny

I would like to create a dashboard in shiny. 我想创建一个闪亮的仪表板。 The purpose of the dashboard is to show KPI for several mobile app in several platforms. 仪表板的目的是显示在多个平台上的多个移动应用程序的KPI。 I would like to create a template and just give filtered data to this template and observe my KPI for this app on this platform. 我想创建一个模板,然后将过滤后的数据提供给该模板,并在此平台上观察此应用程序的KPI。

The data are on sql, so instead of maintaining a connection between shiny and the sql server, I prefer to create a function that extract the data once on the time range selected by the user and collect it. 数据是基于sql的,所以我宁愿创建一个函数来在用户选择的时间范围内提取一次数据并收集数据,而不是在Shiny和sql server之间保持连接。 I want this function to extract again only if date range change and the user validate by clicking on an action button. 我希望仅当日期范围更改并且用户通过单击操作按钮进行验证时才再次提取此函数。

This a very simple toy example to illustrate my goal 这个非常简单的玩具示例可以说明我的目标

  ui = dashboardPage(
    dashboardHeader(),
    dashboardSidebar(sidebarMenu( dateRangeInput("daterange", "Date   Range"), actionButton("do", "Do it !", width="100%"))),
    dashboardBody(fluidPage(plotlyOutput("plot")))
  )

  data = function(start, end)
  {
      return(data.frame(x = c(start, end), y = runif(2)))
  }

  outputTemplate = function(input, output, data, instanceName)
  {
    output[[instanceName]] = renderPlotly({
      plot_ly(data = data) %>% add_bars(x~x, y~y)
     })
  }


  server = function(input, output){


    dt = eventReactive(input$do,{
      return(data(input$daterange[1], input$daterange[2]))
    })

    outputTemplate(input, output, dt(), "plot")

  }

  shinyApp(ui, server)

So, when I click the "Do it !" 因此,当我单击“执行!”时 button, I load data base on the dateRange input, and then use this Data in one Template. 按钮,我基于dateRange输入加载数据库,然后在一个模板中使用此数据。

The first time I click on the button, the plot appear but then when I change the date and click again the plot is never updated. 第一次单击该按钮时,将出现该图,但是当我更改日期并再次单击时,该图永远不会更新。

What I have missed about reactive in shiny ? 我错过了有关闪亮的反应性的东西?

EDIT 1 : 编辑1:

If I replace by 如果我替换为

  output[["plot"]] = renderPlot({
    plot(dt()$x, dt()$y, type = "b")
  })

instead of the outputTemplate function it works perfectly 而不是outputTemplate函数,它可以完美地工作

I found the problem but I don't understand why. 我找到了问题,但我不明白为什么。

If you do not put the parenthesis on dt it works. 如果您不将括号放在dt它将起作用。 The solution is 解决方案是

  outputTemplate(input, output, dt, "plot")

  outputTemplate = function(input, output, data, instanceName)
  {
      output[[instanceName]] = renderPlotly({
        plot_ly(data = data()) %>% add_bars(x~x, y~y)
      })
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM