【问题标题】:Shiny update a table with an action buttonShiny 使用操作按钮更新表格
【发布时间】:2017-08-05 17:33:26
【问题描述】:

这是我的 Shiny App 的相关代码。让我知道是否需要任何其他代码。我可以使用相同的方法来渲染绘图,但是在尝试渲染表格时遇到了错误。

用户界面:

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 &lt;- renderTable({.}) 做一些我没有为了简洁而展示的其他约束。

【问题讨论】:

    标签: r shiny


    【解决方案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)
    

    这对你有用吗?

    【讨论】:

      猜你喜欢
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多