2016-03-24 6 views
3

光沢のあるアプリケーションで反応性のあるデータセットを使用して、そのデータセットを使用する他のオブジェクトをreactiveDfの値に従って再レンダリングできるようにしたいと思います。Shinyでデータセットを反応させるにはどうすればいいですか?

この例では1つのテーブルだけを出力していますが、私のアプリでは他のチャートとテーブルがあり、その考え方はサブセットreactiveDfでレンダリングをトリガーすることです。また、私はdplyrを使ってそれをしたいと思います。今

library(shiny) 
library(dplyr) 
ui <- shinyUI(fluidPage(
    sidebarLayout(
     sidebarPanel(
     checkboxGroupInput('Category', '', 
          unique(mtcars$carb), selected = unique(mtcars$carb))), 
     # Show table of the rendered dataset 
     mainPanel(
     tableOutput("df") 
    ) 
    ) 
)) 


server <- shinyServer(function(input, output) { 

    reactiveDf <- reactive({tbl_df(mtcars) %>% 
            filter(carb %in% input$Category)}) 

    output$df <- renderTable({reactiveDf}) 
}) 

shinyApp(ui = ui, server = server) 

私はこのアプリを実行すると、私が手:

Listening on http://127.0.0.1:7032 
Warning: Error in UseMethod: no applicable method for 'xtable' 
applied to an object of class "reactive" 

そしてtableOutput()は表示されません。

+0

@coryを必要とする...機能ですと 'data.frame'です。あなたは、そのオブジェクトがデータフレームだけを必要とすることを意味しますか? 元のアプリケーションでggvisも使用しています。そのため、サブセット化のためにdplyrを使用しようとしていました。 – Dambo

答えて

4

反応性は、私は `STR(tbl_df(mtcars))` `私はクラス 'tbl_df' を取得、 'TBL' を行う場合ので、あなたは括弧...

library(shiny) 
library(dplyr) 
ui <- shinyUI(fluidPage(
    sidebarLayout(
    sidebarPanel(
     checkboxGroupInput('Category', '', 
         unique(mtcars$carb), selected = unique(mtcars$carb))), 
    # Show table of the rendered dataset 
    mainPanel(
     tableOutput("df") 
    ) 
) 
)) 


server <- shinyServer(function(input, output) { 

    reactiveDf <- reactive({return(tbl_df(mtcars) %>% 
     filter(carb %in% input$Category))}) 

    output$df <- renderTable({reactiveDf()}) 
}) 

shinyApp(ui = ui, server = server) 
+0

ありがとう、ここからの私の残酷なコピー/貼り付けは、私が実際に関数を定義し、それを 'reactiveDf'に割り当てることに気付かなかった。 – Dambo

+1

それは間違いなく直感的ではありません、悪くはありません。 – cory

関連する問題