r - Shiny - object from reactive expression not found when used in loglm -
i created shiny app, in want display residual of log-linear model using mosaic plot. need use data reactive expression , pass loglm
. seem pretty strait forward, when following error : "objet 'mod' introuvable".
i've figured line causing problem, don't know how fix it. running code below should work fine. however, uncomment line # mod <- loglm( formula = reformulate(f), data = mod )
, in server , should same error get.
any appreciated.
ui <- fluidpage( titlepanel("shiny viz!"), fluidrow( class= "r1", tabsetpanel(type= "pills", tabpanel("log-linear model", fluidrow( column(3, offset=1, selectinput("model", label= "choose model fit:", choices= c("(spt)","(sp,st,pt)","(st,pt)","(sp,pt)","(sp,st)")), selectinput("type", label= "visualise expected or observed values?", choices = c("observed", "expected")), sliderinput("n_breaks", label = "degree celcius per bin:", min = .5, max = 5, value = 1, step = .5)), column(8, plotoutput("loglinear.mosaic", height= "600px") ) )))) ) library(ggplot2) library(data.table) library(vcd) library(vcdextra) server <- function(input, output) { # create data df <- data.table( temp = runif(5000, 0, 30), presence = factor(rbinom(5000, 1, runif(20, 0.1, 0.60))), period = factor(as.integer(runif(5000, 1, 9))) ) # reactive expression loglinear <- reactive({ df[ , temperature.category := cut_interval(temp, length= input$n_breaks)] tab <- xtabs(formula= ~ period + temperature.category + presence, data = df) return(tab) }) # mosaic plot output$loglinear.mosaic <- renderplot({ mod <- loglinear() f <- switch(input$model, "(spt)"= c("presence*period*temperature.category"), "(sp,st,pt)" = c("presence*period","presence*temperature.category","period*temperature.category"), "(st,pt)" = c("presence*temperature.category","period*temperature.category"), "(sp,pt)" = c("presence*period","period*temperature.category"), "(sp,st)" = c("presence*period","presence*temperature.category")) # mod <- loglm( formula = reformulate(f), data = mod ) mosaic(mod, gp= shading_hcl, spacing = spacing_highlighting, type= input$type, labeling_args= list(offset_varnames = c(right = 1, left=.5), offset_labels = c(right = .1), set_varnames = c(temperature.category="temperature", period="period", presence="status")), set_labels=list(presence = c("ab","pr")), margins = c(right = 5, left = 3, bottom = 1, top =3)) }) } shinyapp(ui = ui, server = server)
i still haven't found causing problem loglm
, i've figured way of getting result wanted.
i used glm
fit model instead of loglm
, used mosaic.glm
vcdextra
package create mosaic plot. code pretty same except data data.frame
, column 'temperature.category', 'period' , 'presence' must factor used glm
.
however, still clueless why loglm
can't find object 'mod', glm
can? i'd realy want know reason. since answers doesn't answer question, i'll accept other answer if has explanation.
here's code using glm
:
ui <- fluidpage( titlepanel("shiny viz!"), fluidrow( class= "r1", tabsetpanel(type= "pills", tabpanel("log-linear model", fluidrow( column(3, offset=1, selectinput("model", label= "choose model fit:", choices= c("(spt)","(sp,st,pt)","(st,pt)","(sp,pt)","(sp,st)")), selectinput("type", label= "visualise expected or observed values?", choices = c("observed", "expected")), sliderinput("n_breaks", label = "degree celcius per bin:", min = .5, max = 5, value = 1, step = .5)), column(8, plotoutput("loglinear.mosaic", height= "800px") ) )))) ) library(ggplot2) library(data.table) library(vcd) library(vcdextra) server <- function(input, output) { df <- data.table( temp = runif(5000, 0, 30), presence = factor(rbinom(5000, 1, runif(20, 0.1, 0.60))), period = factor(as.integer(runif(5000, 1, 9)) ) ) # data data.frame format loglinear <- reactive({ df[ , temperature.category := cut_interval(temp, length= input$n_breaks)] # add 'freq' column dat <- data.frame(as.table(xtabs(formula= ~ period + temperature.category + presence, data = df)), stringsasfactors = t) return(dat) }) # mosaic plot output$loglinear.mosaic <- renderplot({ mod <- loglinear() f <- switch(input$model, "(spt)"= c("presence*period*temperature.category"), "(sp,st,pt)" = c("presence*period","presence*temperature.category","period*temperature.category"), "(st,pt)" = c("presence*temperature.category","period*temperature.category"), "(sp,pt)" = c("presence*period","period*temperature.category"), "(sp,st)" = c("presence*period","presence*temperature.category")) # fit model using glm mod.glm <- glm(formula = reformulate(f, response = "freq"), data= mod, family= poisson) mosaic.glm(mod.glm, formula = ~ temperature.category + period + presence, gp= shading_hcl, spacing = spacing_highlighting, type= input$type, labeling_args= list(rot_labels = c(left = 0, right = 0), offset_varnames = c(left=1.5, right = 1), offset_labels = c(left=.5, right = .1), set_varnames = c(temperature.category="temperature", period="period", presence="status")), set_labels=list(presence = c("ab","pr")), margins = c(right = 5, left = 4, bottom = 1, top =3)) }) }
Comments
Post a Comment