简体   繁体   English

错误:如果为正,列索引必须最多为 6 - R 中的闪亮应用错误

[英]Error in : Column indexes must be at most 6 if positive - Shiny App Error in R

I am trying to build shiny app using shiny library and gapminder as my dataset.我正在尝试使用shiny库和gapminder作为我的数据集来构建闪亮的应用程序。 I am building small app, where user can select specific continent from side panel and the histogram on population vs year of the selected continent gets displayed on the mainpanel of shiny App.我正在构建小应用程序,用户可以从侧面板中选择特定的大陆,并且所选大陆的人口与年份的直方图显示在闪亮的应用程序的主面板上。

I am using below code to generate the histogram.我正在使用下面的代码来生成直方图。 However, when I run the below code I am getting an error in R that say's但是,当我运行下面的代码时,我在 R 中收到一个错误,说是

Column indexes must be 6 if positive...如果为正数,列索引必须为 6...

I am not sure what it means.Also, I have attached my expected output.我不确定这意味着什么。另外,我附上了我的预期输出。

My Rcode:我的代码:

library(shiny)
library(gapminder)

ui<-fluidPage(
  titlePanel("Population Analysis"),
  sidebarLayout(sidebarPanel(
    selectInput(inputId = 'cont',
                label = "Select Continent:",
                choices = gapminder$continent),
    selectInput(inputId = 'year',
                label = "Select Year:",
                choices = gapminder$year)
  ),

  mainPanel(
    paste('Aggregate population by year'),
   textOutput("txtoutput"),
   plotOutput("continentplot")
  )
)
)

server<-function(input,output){
  output$txtoutput<-renderText({
    paste(input$cont)
  })
  output$continentplot<-renderPlot({
    req(gapminder$year)
  hist(gapminder[gapminder$pop])
  })
}
shinyApp(ui=ui, server=server)

Error:错误:

warning: Error in : Column indexes must be at most 6 if positive, not 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12881816, 13867957, 16317921, 22227415, 25268405, 31889923, 1282697, 1476505, 1728137, 1984060, 2263554, 2509048, 2780097, 3075321, 3326498, 3428038, 3508512, 3600523, 9279525, 10270856, 11000948, 12760499, 14760787, 17152804, 20033753, 23254956, 26298373, 29072015, 31287142, 33333216, 4232095, 4561361, 4826015, 5247469, 5894858, 6162675, 7016384, 7874230, 8735988, 9875024, 10866106, 12420476, 17876956, 19610538, 21283783, 22934225, 24779799, 26983828, 29341374, 31620918, 33958947, 36203463, 38331121, 40301927, 8691212, 9712569, 10794968, 11872264, 13177000, 14074100, 15184200, 16257249, 17481977, 18565243, 19546792, 20434176, 6927772, 6965860, 7129864, 7376998, 7544201, 7568430, 7574613, 7578903, 7914969, 8069876, 8148312, 8199783, 120447, 138655, 171863, 202182, 230800, 297410, 377967, 454612, 529491, 598561, 656397, 708573, 46886859, 51365468, 56839289, 62821884, 70 [... truncated]
  179: <Anonymous>
Warning: Error in : Column indexes must be at most 6 if positive, not 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12881816, 13867957, 16317921, 22227415, 25268405, 31889923, 1282697, 1476505, 1728137, 1984060, 2263554, 2509048, 2780097, 3075321, 3326498, 3428038, 3508512, 3600523, 9279525, 10270856, 11000948, 12760499, 14760787, 17152804, 20033753, 23254956, 26298373, 29072015, 31287142, 33333216, 4232095, 4561361, 4826015, 5247469, 5894858, 6162675, 7016384, 7874230, 8735988, 9875024, 10866106, 12420476, 17876956, 19610538, 21283783, 22934225, 24779799, 26983828, 29341374, 31620918, 33958947, 36203463, 38331121, 40301927, 8691212, 9712569, 10794968, 11872264, 13177000, 14074100, 15184200, 16257249, 17481977, 18565243, 19546792, 20434176, 6927772, 6965860, 7129864, 7376998, 7544201, 7568430, 7574613, 7578903, 7914969, 8069876, 8148312, 8199783, 120447, 138655, 171863, 202182, 230800, 297410, 377967, 454612, 529491, 598561, 656397, 708573, 46886859, 51365468, 56839289, 62821884, 70 [... truncated]
  179: <Anonymous>

Expected output:预期输出:

在此处输入图片说明

I am new to R and Shiny我是 R 和 Shiny 的新手

  1. req(gapminder$year) makes continue the code because gapminder$year is not NULL . req(gapminder$year)使代码继续,因为gapminder$year不是NULL You need to use input$year .您需要使用input$year Also, you need to require that a continent is specified: req(gapminder$continent)此外,您需要要求指定一个大陆: req(gapminder$continent)
  2. You need another req before pasting textOuput because input$cont is empty at the beginning在粘贴textOuput之前你需要另一个req因为input$cont一开始是空的
  3. To plot a basic histogram you only can pass the vector of data to plot ( pop ) First, for sure, you need to filter for your year and continent selected要绘制基本直方图,您只能将数据向量传递给 plot ( pop ) 首先,当然,您需要过滤所选年份和大陆

library(shiny)图书馆(闪亮)

library(gapminder)图书馆(gapminder)

library(dplyr)图书馆(dplyr)

ui<-fluidPage(
  titlePanel("Population Analysis"),
  sidebarLayout(sidebarPanel(
    selectInput(inputId = 'cont',
                label = "Select Continent:",
                choices = gapminder$continent),
    selectInput(inputId = 'year',
                label = "Select Year:",
                choices = gapminder$year)
  ),

  mainPanel(
    paste('Aggregate population by year'),
    textOutput("txtoutput"),
    plotOutput("continentplot")
  )
  )
)

server<-function(input,output){
  output$txtoutput<-renderText({
    req(input$cont)
    paste(input$cont)
  })
  output$continentplot<-renderPlot({
    req(input$year)
    req(input$cont)
    dataHist <- gapminder %>% 
      filter(continent==input$cont) %>% 
      filter(year==as.integer(input$year)) %>%
      select(pop)
    hist(dataHist$pop)
  })
}
shinyApp(ui=ui, server=server)

For your second question, you can use this code:对于您的第二个问题,您可以使用以下代码:

library(shiny)
library(ggplot2)
library(gapminder)
library(dplyr)
ui<-fluidPage(
  titlePanel("Population Analysis"),
  sidebarLayout(sidebarPanel(
    selectInput(inputId = 'cont',
                label = "Select Continent:",
                choices = gapminder$continent)
  ),

  mainPanel(
    paste('Aggregate population by year'),
    textOutput("txtoutput"),
    plotOutput("continentplot")
  )
  )
)

server<-function(input,output){
  output$txtoutput<-renderText({
    req(input$cont)
    paste(input$cont)
  })
  output$continentplot<-renderPlot({
    req(input$cont)
    dataHist <- gapminder %>% 
      filter(continent==input$cont)  %>%
      group_by(year)%>%
      select(pop,year)

    ggplot(data=dataHist, aes(x=year, y=pop)) + 
      labs(y="Population") +
      geom_bar(stat="identity", fill="purple") + 
      scale_x_continuous(name ="Year",breaks = seq(1952,2007,5))+
      theme(
        panel.background = element_rect(fill = "plum1",
                                        colour = "plum1",
                                        size = 0.5, linetype = "solid"),
        panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                                        colour = "white"), 
        panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
                                        colour = "white")
      )

  })
}
shinyApp(ui=ui, server=server)

Yo can play with the colour and fill parameters ( http://sape.inf.usi.ch/quick-reference/ggplot2/colour ) to customize your plot!您可以使用colourfill参数 ( http://sape.inf.usi.ch/quick-reference/ggplot2/colour ) 来自定义您的绘图! Best!最好的事物!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM