r - table command fails with shiny input variable -
i'm creating first shiny app, works fantastic when using ggplot2 using other base r or vcd plots has me stuck. i'd user able select tabling variable , view resulting mosaic or association plot. server code fails @ table command. things i've tried commented out below.
thanks help.
library(shiny) library(shinydashboard) library(vcd) header = dashboardheader(title = 'min reproducible example') sidebar = dashboardsidebar() body = dashboardbody( fluidrow(plotoutput('plot'), width=12), fluidrow(box(selectinput('factor', 'select factor:', c('os', 'gender')))) ) ui = dashboardpage(header, sidebar, body) server = function(input, output){ set.seed(1) df = data.frame(condition = rep(c('a','b','c','d'), each = 300), conversion = c(sample(c('convert','not-convert'), 300, replace = true, prob = c(0.9, 0.1)), sample(c('convert','not-convert'), 300, replace = true, prob = c(0.7, 0.3)), sample(c('convert','not-convert'), 300, replace = true, prob = c(0.5, 0.5)), sample(c('convert','not-convert'), 300, replace = true, prob = c(0.2, 0.8))), gender = sample(c('m','f'), 1200, replace = true), os = rep(sample(c('web','ios','android'), 1200, replace = true), times = 2)) #tried #table1 = reactive({ # with(df, table(condition, conversion, input$factor)) #}) output$plot = renderplot({ #fails here: table1 = with(df, table(condition, conversion, input$factor)) #also tried these #table1 = with(df, table(condition, conversion, as.character(isolate(reactivevaluestolist(input$factor))))) #also tried table1 = with(df, table(condition, conversion, input$factor)) #also tried table1 = table(df$condition, df$conversion, paste0('df$', input$factor)) #then want categorical plots assoc(table1, shade=true) #or mosaicplot(table1, shade=true) }) } shinyapp(ui, server)
an easy fix use 'starts_with' dplyr in select() statement on input variable
library('dplyr') output$plot = renderplot({ df <- select(df, condition, conversion, tmp_var = starts_with(input$factor)) table1 = with(df, table(condition, conversion, tmp_var)) mosaicplot(table1, shade=true) }) }
Comments
Post a Comment