0

I am trying to get the suffix of an output$suffix name in R Shiny and incorporate it into the input$suffix_rows_selected function. The drilldown table is coming empty. Would someone have any idea of what am I doing wrong?

Function that I am trying to build:

f.drilldata <- function(base.summary, base.drilldown,  suffix.output, group_var){ 

group = enquo(group_var)
base.summary = base.summary %>% mutate(var = !!group)
base.drilldown = base.drilldown %>% mutate(var = !!group)

#input = expr(!!glue("input${suffix.output}_rows_selected"))
input = paste0(suffix.output,'_rows_selected')

validate(need(length(input[[input]]) > 0, ''))
selected_rows <- base.summary[as.integer(input[[input]]), ]$var

base.drilldown[base.drilldown$var %in% selected_rows, ]
}
Error Example:
library("dplyr")
library("shiny")
library("DT")

tbl.summary <- group_by(iris, Species) %>% summarise(Count = n())
tbl.drilldown <- iris

ui <- fluidPage(
DTOutput("output.summary.name")
, DTOutput("output.drilldown.name"))

server <- function(input, output){

# display the data that is available to be drilled down
output$output.summary.name <- renderDT(tbl.summary)

# subset the records to the row that was clicked through f.drilldata function
drilldata <- reactive({ f.drilldata(tbl.summary, tbl.drilldown, 'output.summary.name', Species)  })

# display the subsetted data
output$output.drilldown.name <- renderDT(drilldata())}

shinyApp(ui, server)
Example that works but out of the f.drilldata function
library("dplyr")
library("shiny")
library("DT")

tbl.summary <- group_by(iris, Species) %>% summarise(Count = n())
tbl.drilldown <- iris

ui <- fluidPage(
DTOutput("output.summary.name")
, DTOutput("output.drilldown.name"))


server <- function(input, output){

output$output.summary.name <- renderDT(tbl.summary)

drilldata <- reactive({ validate( need(length(input$output.summary.name_rows_selected) > 0, "Select rows to drill down!")) 
selected_species <- 
tbl.summary[as.integer(input$output.summary.name_rows_selected), ]$Species
tbl.drilldown[tbl.drilldown$Species %in% selected_species, ]  })

output$output.drilldown.name <- renderDT(drilldata())}

shinyApp(ui, server)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Suzy
  • 51
  • 3
  • Actually the following error appeared in the console: Listening on http://127.0.0.1:3756 Warning in `[.tbl_df`(base.summary, as.integer(input), ) : NAs introduced by coercion – Suzy Oct 16 '19 at 19:13
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. How exactly are you calling this function? – MrFlick Oct 16 '19 at 19:59
  • I added an example that works without the f.drilldata function and an example that does not work when I use the function. – Suzy Oct 17 '19 at 01:06

1 Answers1

0

I have found a simple solution by just adding the entire input (input$output.summary.name_rows_selected) as an argument of the function as below.

library("dplyr")
library("shiny")
library("DT")


f.drilldata <- function(base.summary, base.drilldown,  input, group_var){ 

group = enquo(group_var)
base.summary = base.summary %>% mutate(var = !!group)
base.drilldown = base.drilldown %>% mutate(var = !!group)

validate(need(length(input) > 0, ''))
selected_rows <- base.summary[as.integer(input), ]$var

base.drilldown[base.drilldown$var %in% selected_rows, ]
}

tbl.summary <- group_by(iris, Species) %>% summarise(Count = n())
tbl.drilldown <- iris

ui <- fluidPage(
DTOutput("output.summary.name")
, DTOutput("output.drilldown.name"))

server <- function(input, output){
output$output.summary.name <- renderDT(tbl.summary)

drilldata <- reactive({ f.drilldata(tbl.summary, tbl.drilldown, 
                                    input$output.summary.name_rows_selected, Species)  })

output$output.drilldown.name <- renderDT(drilldata())}

shinyApp(ui, server)
Suzy
  • 51
  • 3