2017-08-05 78 views
0

這是我的Shiny應用程序的相關代碼。讓我知道是否需要額外的代碼。我能夠使用相同的方法來渲染一個圖,但是當我嘗試渲染一個表時,我遇到了一個錯誤。閃亮的更新表格與動作按鈕

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({.})做一些其他的約束,我沒有表現爲一個簡短的嘗試。

回答

0

我認爲在調用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) 

這是否爲你工作?