Archive

Tag Archives: data-analysis

I’m very new to the R/RStudio environment, and while working on a project, I couldn’t find anything on google/stack overflow about a Shiny issue I was having, so I decided to write about it myself!

My goal was to create a Shiny app that presents a dynamic data table with sliders and multipliers for the values within the table. I started with referencing the Shiny Gallery for code snippets that I could use to guide my R code.

This is an example of code from the based on the Shiny gallery:

output$table = DT::renderDataTable(DT::datatable({
    data = metrics
    if(input$device != 3) {
    data = data[data$device_type_id == input$device,]
    }
    if(input$days != 30) {
    data  = data[data$days_between <= input$days]
    }
}))

 

For some reason, this was not working out for me, and none of the examples discuss editing data table values dynamically. Unfortunately, I’m on a somewhat tight deadline, so I decide to replicate the methods I would use to look at this data on my own computer. If I’m using RStudio to play with data, dplyr is heavenly. Coming from a SQL/Excel analyst background, dplyr is intuitive and easy to use for filtering, examining, and manipulating data.

I plug in my dplyr version of the DataTable code, and it works!

output$table = DT::renderDataTable(DT::datatable({
    data = metrics
    if(input$device != 3) {
        data = filter(data, device_type_id == input$device)
    }
    if(input$days != 30) {
        data = filter(data, days_between <= input$days)
    presentation_data = data %>%
        group_by() %>%
        summarise(
            clicks = sum(clicks),
            sales = sum(sales)
        ) %>%
    arrange(desc(clicks))
})


Additionally, I was able to  modify values in the DataTable dynamically with dplyr:

if(input$percent != 1){
    data$clicks = portfolio_metrics$clicks * input$percent
    data$sales = portfolio_metrics$sales * input$percent
}

In the end, I was able to use dplyr functions in my server.R file to present a DataTable that could be filtered, manipulated, and understood easily by a non-technical user.

If anyone stumbles upon this while googling about how to present DataTables in Shiny apps, please don’t hesitate to leave a comment if this helps at all, and if I’m completely wrong, please let me know!