闪亮更新带有操作按钮的表格

时间:2017-08-05 17:33:26

标签: r shiny

以下是我的Shiny App的相关代码。如果需要任何其他代码,请告诉我。我可以使用相同的方法渲染绘图,但在尝试渲染表时遇到错误。

UI:

actionButton("do", "Test!"),

fluidPage(
      tableOutput('table1')
),

服务器

   globals <- reactiveValues(
    mydf = dat
   )

   count <- eventReactive(input$do, {  
    filter_mk <- input$mk
    filter_date <- input$date
    filter_universe <- input$universe

    dat_f <- globals$mydf %>% filter(date >= filter_date & universe %in% filter_universe & mrkcp_buckets %in% filter_mk)

    count <- dat_f %>%
      group_by(date) %>%
      count()  %>%
      rename(st = n) %>%
      group_by(strftime(date, "%Y-%m")) %>%
      filter(date == max(date)) %>%
      ungroup() %>%
      select(`strftime(date, \"%Y-%m\")`, st) %>%
      spread(`strftime(date, \"%Y-%m\")`, st)  %>%
      .[seq(1, length(.), 3)]

})

output$table1 <- renderTable({
  count()
}, caption= "Test",
caption.placement = getOption("xtable.caption.placement", "top")
)

然而,当我按下“测试”按钮时,我收到以下错误:计数错误:未使用的参数(。)。想知道我是否遗漏了一些简单的东西。

我也不能observeEvent(input$do, {output$table2 <- renderTable({.})对其他一些我没有表现出来作为简洁尝试的约束做。

1 个答案:

答案 0 :(得分:1)

我认为你必须在调用count函数时指定包,因为R可能会将它与你的被动计数对象混淆。

一个完整的工作示例就是这个(我希望它足够接近你,但是因为你没有提供一个完整的最小工作例子,我不得不做出一些猜测):

ui <- fluidPage(
 sidebarLayout(
  sidebarPanel(
   actionButton("do", "Test!"),
   # as a substitute for mk etc
   numericInput("numbers", "Max Group_n", min = 1, max = 1000, value = 100)
  ),
  tableOutput('table1')
 )
)


server <- function(input, output) {
 library(dplyr)

 globals <- reactiveValues(
  mydf = data.frame(x = 1:1000,
                    y = rnorm(1000),
                    group = sample(LETTERS[1:10], 1000, T))
 )

 count <- eventReactive(input$do, {  
  filter_n <- input$numbers

  dat_f <- globals$mydf

  count <- dat_f %>%
   group_by(group) %>%
   # make sure that we use dplyr's count and not the reactive count...
   dplyr::count() %>% 
   filter(n >= filter_n)

 })


 output$table1 <- renderTable({
  count()
 }, caption = "Test", caption.placement = getOption("xtable.caption.placement", "top"))
}

shinyApp(ui = ui, server = server)

这对你有用吗?