簡體   English   中英

錯誤:如果為正,列索引必須最多為 6 - R 中的閃亮應用錯誤

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

我正在嘗試使用shiny庫和gapminder作為我的數據集來構建閃亮的應用程序。 我正在構建小應用程序,用戶可以從側面板中選擇特定的大陸,並且所選大陸的人口與年份的直方圖顯示在閃亮的應用程序的主面板上。

我正在使用下面的代碼來生成直方圖。 但是,當我運行下面的代碼時,我在 R 中收到一個錯誤,說是

如果為正數,列索引必須為 6...

我不確定這意味着什么。另外,我附上了我的預期輸出。

我的代碼:

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)

錯誤:

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>

預期輸出:

在此處輸入圖片說明

我是 R 和 Shiny 的新手

  1. req(gapminder$year)使代碼繼續,因為gapminder$year不是NULL 您需要使用input$year 此外,您需要要求指定一個大陸: req(gapminder$continent)
  2. 在粘貼textOuput之前你需要另一個req因為input$cont一開始是空的
  3. 要繪制基本直方圖,您只能將數據向量傳遞給 plot ( pop ) 首先,當然,您需要過濾所選年份和大陸

圖書館(閃亮)

圖書館(gapminder)

圖書館(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)

對於您的第二個問題,您可以使用以下代碼:

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)

您可以使用colourfill參數 ( http://sape.inf.usi.ch/quick-reference/ggplot2/colour ) 來自定義您的繪圖! 最好的事物!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM