How to enter the suffix of the name of an output in the input function$.... _rows_selected?

Asked

Viewed 38 times

0

I am building a function in R Shiny and in this function I want to take the suffix of the name of a output$sufixo and incorporate it into a function of the DT input$sufix_rows_selected. Does anyone have any idea what I’m doing wrong?

I tried to:

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

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

input = paste0(sufix.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, ]
}
Example of the error using a native R table:
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)
Original working outside 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)

1 answer

0

I found a simple solution for this function. I just added the full input as a function argument and it worked!

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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.