R 根据用户输入更改闪亮应用程序使用的数据

R 根据用户输入更改闪亮应用程序使用的数据,r,shiny,R,Shiny,这是我第一次体验闪亮,我遇到了反应性的绊脚石。我也知道这是一种非常笨拙的运行分析的方法,我将在以后重做 我正在尝试获取它,以便直方图根据用户输入进行更改 ui.R是: library(shiny) shinyUI(pageWithSidebar( headerPanel("United States Population Pyramid 2015-2090"), sidebarPanel( radioButtons("iradio", label = h3("Select ye

这是我第一次体验闪亮,我遇到了反应性的绊脚石。我也知道这是一种非常笨拙的运行分析的方法,我将在以后重做

我正在尝试获取它,以便直方图根据用户输入进行更改

ui.R是:

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel("United States Population Pyramid 2015-2090"),
  sidebarPanel(
    radioButtons("iradio", label = h3("Select year"),
             choices = list("2015" = 2015, "2040" = 2040, "2065" = 2065, "2090" = 2090), selected = 2015))
  ,
  mainPanel(
  plotOutput('hist')
  )   
))
而服务器.R是:

library(shiny)
library(plotrix)
xx.2015<-c(3.0, 3.1, 3.1, 3.1, 3.5, 3.3, 3.4, 3.0, 3.2, 3.2, 3.6, 3.5, 3.0, 2.6, 1.9, 1.4, 1.1, 0.8, 0.4, 0.1, 0.0)
xy.2015<-c(3.1, 3.3, 3.3, 3.3, 3.7, 3.5, 3.5, 3.1, 3.2, 3.2, 3.5, 3.4, 2.8, 2.4, 1.6, 1.1, 0.8, 0.5, 0.2, 0.0, 0.0)
xx.2040<-c(2.9, 2.9, 2.9, 3.0, 3.0, 3.0, 3.1, 3.1, 3.0, 3.2, 3.0, 2.9, 2.5, 2.6, 2.4, 2.5, 2.1, 1.4, 0.7, 0.2, 0.0)
xy.2040<-c(3.0, 3.0, 3.1, 3.1, 3.1, 3.1, 3.2, 3.1, 3.1, 3.4, 3.1, 2.9, 2.5, 2.5, 2.3, 2.2, 1.7, 1.0, 0.4, 0.1, 0.0)
xx.2065<-c(2.8, 2.8, 2.8, 2.9, 2.9, 2.9, 3.0, 3.0, 3.0, 3.0, 2.8, 2.9, 2.8, 2.6, 2.7, 2.3, 2.0, 1.4, 0.9, 0.4, 0.1)
xy.2065<-c(2.9, 2.9, 3.0, 3.0, 3.0, 3.0, 3.1, 3.1, 3.1, 3.0, 2.9, 2.9, 2.8, 2.6, 2.7, 2.2, 1.8, 1.2, 0.7, 0.2, 0.1)
xx.2090<-c(2.7, 2.7, 2.7, 2.7, 2.8, 2.8, 2.9, 2.9, 2.9, 2.8, 2.8, 2.8, 2.8, 2.7, 2.6, 2.4, 2.2, 1.7, 1.2, 0.6, 0.2)
xy.2090<-c(2.8, 2.8, 2.8, 2.9, 2.9, 2.9, 3.0, 3.0, 2.9, 2.9, 2.9, 2.9, 2.8, 2.7, 2.6, 2.3, 2.0, 1.5, 0.9, 0.4, 0.1)

agelabels<-c("0-4","5-9","10-14","15-19","20-24","25-29","30-34",
         "35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74",
         "75-79","80-44","85-89", "90-94", "95-99", "100+")

shinyServer(
  function(input, output) {

  if (input$iradio == 2015) {
    xx.pop <- xx.2015
    xy.pop <- xy.2015
  }

  if (input$iradio == 2040) {
    xx.pop <- xx.2040
    xy.pop <- xy.2040
  }

  if (input$iradio == 2065) {
    xx.pop <- xx.2065
    xy.pop <- xy.2065
  }
})

output$hist<-renderPlot({
  func()
  par(mar=pyramid.plot(xy.pop,xx.pop,labels=agelabels,
                       lxcol="lightblue",rxcol="pink",
                       gap=0.5,show.values=TRUE))
})
库(闪亮)
库(plotrix)

xx.2015有几件事要做:

  • 将您的
    输出$hist
    移动到
    shinyServer
    功能中
  • output$hist
    函数中移动
    if
    语句

    shinyServer(function(input, output) {
        output$hist<-renderPlot({
          if (input$iradio == 2015) {
            xx.pop <- xx.2015
            xy.pop <- xy.2015
          }
    
          if (input$iradio == 2040) {
            xx.pop <- xx.2040
            xy.pop <- xy.2040
          }
    
          if (input$iradio == 2065) {
            xx.pop <- xx.2065
            xy.pop <- xy.2065
          }
    
    
          par(mar=pyramid.plot(xy.pop,xx.pop,labels=agelabels,
                               lxcol="lightblue",rxcol="pink",
                               gap=0.5,show.values=TRUE))
        })
      })
    
    shinyServer(功能(输入、输出){
    输出$hist