【问题标题】:Subsetting in r shinyr闪亮中的子集
【发布时间】:2018-06-07 12:13:02
【问题描述】:

我一直在做一个闪亮的可视化项目。我正在尝试通过给定的输入过滤数据集 - 滑块的状态数和范围。不幸的是,r“省略”了代码部分并输出了整个数据集。我也收到警告:“数据”不是图形参数。

library(shiny)
library(Ecdat)


u <- shinyUI(pageWithSidebar(
  headerPanel("Social benefits"),
  sidebarPanel(

    selectInput("variable", "Variable:",
                list("Unemployment", 
                     "Max benefit" 
               )),
    #Specification of state
    textInput("state", "State:", value = "93"),
    # Specification of range within an interval
    sliderInput("range", "Range:",
                min = 1, max = 100, value = c(20,100))
  ),
  mainPanel(
    plotOutput("mpgPlot")
  )

))

s <- shinyServer(function(input, output) 
{
  #filter by state -ERROR
  p <- reactive({ Benefits[Benefits$state == input$state,]})

  #filter by slider range - ERROR
  dataX <- reactive({ p()[input$range[1]:input$range[2],,drop = FALSE] })

  variable <- reactive({
    switch(input$variable,
           "Unemployment" = stateur,
           "Max benefit" = statemb
           )
    })

  caption <- reactive({
    paste(input$variable)
  })

  output$mpgPlot <- renderPlot({
    plot(variable(), data = dataX(), type = "l",ylab = caption())
  })

})
shinyApp(u,s)

【问题讨论】:

  • 为什么dataX ,- 行中的P 后面有()?也不要调用 var ,因为它是函数的名称。 plot(var() 是什么意思?
  • 我总是在 'reactive' 定义的变量后使用方括号,在这种情况下是子集的子集。 Plot(var()) - 绘制选定变量的时间序列。
  • stateurstatemb(用于第 35 和 36 行)定义在哪里?
  • stateur - 州失业。速率状态 - 状态最大值。收益 - 来自数据集的变量
  • var() 将尝试计算方差。

标签: r shiny


【解决方案1】:

真正需要的只是在变量之前指定数据集名称,因为来自环境的数据集掩盖了过滤后的数据集。

output$urPlot <- renderPlot({
    plot(dataX()$stateur, data = dataX(), type = "l",ylab = "Unemployment")
  })
  output$mbPlot <- renderPlot({
    plot(dataX()$statemb, data = dataX(), type = "l",ylab = "Max benefit")
  })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2018-12-27
    • 1970-01-01
    • 2015-09-01
    相关资源
    最近更新 更多