闪亮 - 在loglm中使用时未找到反应式表达式的对象

时间:2015-07-14 04:29:07

标签: r shiny mosaic-plot

我创建了一个闪亮的应用程序,我想在其中使用马赛克图显示对数线性模型的残差。我需要使用来自反应式表达式的数据并将其传递给loglm。它似乎相当紧张,但当我这样做时,我得到以下错误:“objet'mod'introuvable”。

我已经知道哪条线导致了问题,但我不知道如何解决它。运行下面的代码应该可以正常工作。 但是,在服务器中取消注释行# mod <- loglm( formula = reformulate(f), data = mod ),你应该得到同样的错误。

非常感谢任何帮助。

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 to fit:",
                                                  choices= c("(SPT)","(SP,ST,PT)","(ST,PT)","(SP,PT)","(SP,ST)")),
                                      selectInput("type", label= "Visualise the 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)

1 个答案:

答案 0 :(得分:0)

我仍然无法找到导致glm问题的原因,但我已经找到了获得我想要的结果的另一种方法。

我使用loglm来匹配模型而不是mosaic.glm,然后使用vcdExtra包中的data.frame来创建镶嵌图。代码几乎相同,只是数据是glm和列&#39; Temperature.category&#39;,&#39;期间&#39;和&#39;存在&#39;必须是与loglm一起使用的因素。

但是,我仍然不清楚为什么glm无法找到对象&#39; mod&#39;,但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 to fit:", choices= c("(SPT)","(SP,ST,PT)","(ST,PT)","(SP,PT)","(SP,ST)")), selectInput("type", label= "Visualise the 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 to 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)) }) } 的代码:

public function store(UsersRequest $request)
{
    $user = User::create($request->all());

    event(new UserWasCreated($user));

    return redirect('/users');
}