I would like to use a reactive dataset in my shiny app, such that any other objects that uses that dataset can be re-rendered according to the values in reactiveDf
.
In this example I am outputting only one table, but in my app I have other charts and tables, and the idea is to trigger the rendering by subsetting reactiveDf
only. Also, I would like to do that using 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)
Right now, when I run this app I get:
Listening on http://127.0.0.1:7032
Warning: Error in UseMethod: no applicable method for 'xtable'
applied to an object of class "reactive"
And tableOutput()
is not displayed.
A reactive expression is an R expression that uses widget input and returns a value. The reactive expression will update this value whenever the original widget changes. To create a reactive expression use the reactive function, which takes an R expression surrounded by braces (just like the render* functions).
Description. The reactiveVal function is used to construct a "reactive value" object. This is an object used for reading and writing a value, like a variable, but with special capabilities for reactive programming.
Data is loaded into shinyapps.io in a few different ways: The simplest way to get data into an application is by uploading a CSV, RData or other data file directly with the application source code. In this model, the application author includes the data files as part of the application.
renderPlot is an reactive function that can take input data from the ui. R script and feed it into the server. R script. It then actively updates the information within its function.
A reactive is a function... so you need parens...
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With