使用Shiny实现回归中的反应值

时间:2016-08-10 15:46:47

标签: shiny regression reactive-programming

我目前正致力于一个旨在创建可以进行统计分析的界面的项目。对我的目标的一个很好的参考将类似于以下网站:https://rich.shinyapps.io/regression/

我认为问题是反应性回归。用户应该选择数据输入,然后在回归中使用。不幸的是,由于数据的概念,我必须在能够处理输入之前创建数据帧的子集...

此输入存储在变量X,Y和Z中,可以使用“粘贴”显示,但回归不起作用。enter code here

有什么建议吗?

library("shiny")

ui< - fluidPage((pageWithSidebar(         headerPanel(“动态分析”),

  sidebarPanel(

  selectInput (
  inputId = "Country", label = "Choose a country", choices = c(levels(eurostat$GEO))
  ),

  selectInput (
  inputId = "Indice1", label = "Choose a dependent variable X", choices = c(levels(eurostat$INDIC_NA), 1)
  ),
  selectInput (
  inputId = "Indice2", label = "Choose an independent variable Y", choices = c(levels(eurostat$INDIC_NA), 1)
  ),
  selectInput (
  inputId = "Indice3", label = "Choose an independent variable Z", choices = c(levels(eurostat$INDIC_NA), 1)
  ),
  selectInput (
  inputId = "Unit1", label = "Choose a Unit", choices = c(levels(eurostat$UNIT), 1)
  )
  ),

  mainPanel(tableOutput("regTab"),

  textOutput("test")
  )

   # mainPanel("Table",tableOutput="table")
  )))

  #This function is used to subset the desired data from the dataset
  subsetting_num = function(Country, Indice, Unit, npar=TRUE,print=TRUE){
  as.numeric(gsub(",", "" ,droplevels(subset(subset(subset(ES2, GEO== Country, Value!=0), INDIC_NA== Indice), UNIT == Unit)$Value)))
  }

server< - shinyServer(function(input,output){

X = reactive({subsetting_num(input$Country, input$Indice1, input$Unit1)})
Y = reactive({subsetting_num(input$Country, input$Indice2, input$Unit1)})
Z = reactive({subsetting_num(input$Country, input$Indice3, input$Unit1)})


# regression formula
runRegression <- reactive({
lm(X ~ Y + Z)
})

#Summary Regression
output$regTab <- renderTable({
if(!is.null(X)){
  summary(runRegression())$coefficients
} else {
  print(data.frame(Warning="Please select Model Parameters."))
}

})

#Depict the reactive values
output$test <- renderText({
paste("Subset", X())

})

})

shinyApp(ui = ui,server = server)

0 个答案:

没有答案