R 闪亮应用 ggplot 动态条形图输出错误

R shiny app ggplot dynamic bar plot output error

提问人:Nick kim 提问时间:10/20/2023 最后编辑:Nick kim 更新时间:10/20/2023 访问量:19

问:

我正在学习闪亮,并想绘制一个动态条形图,当用户选择变量和年份时,该条形图会呈现每个国家/地区每年的难民人数/计数。我的数据是一个 CSV 文件,有 4 列 country、year、refuges、asylum、idps。

data <- read.csv("/analysis.csv", header = TRUE)

# ui object
ui <- fluidPage(
  titlePanel(p("Refugee Dashboard", style = "color:#3474A7")),
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId = "variableselected",
        label = "Select variable",
        choices = c("refugees", "asylumSeekers", "IDPs")
      ),
      selectInput(
        inputId = "yearselected",
        label = "Select year",
        choices = 2000:2020
      ),
        fluidRow(
          infoBox(
            title = "Total Selected",
            textOutput("totalSelected"),
            icon = icon("users")
          )
        ),
      
      p("Made with", a("Shiny",
                       href = "http://shiny.rstudio.com"), "."),
      img(src = "imageShiny.png",
          width = "70px", height = "70px"), width = 2
    ),
    mainPanel(
      plotOutput(outputId = "barplot"),
      DTOutput(outputId = "table")
    )
  )
)

# server()
server <- function(input, output){
  output$table <- renderDT(data)
  
  output$barplot <- renderPlot({
    filtered_data <- data %>%
      filter(data$year == as.numeric(input$yearselected))
    ggplot(filtered_data, aes(x = country, y = !!as.name(input$variableselected), fill=country)) +
      geom_bar(stat = "identity") +
      labs(x = "country", y = input$variableselected) +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
  })

我收到错误

`data` must be a [34m<data.frame>[39m, or an object coercible by `fortify()`, not a <mts/ts> object.
R 亮闪 亮仪表板

评论

2赞 smartse 10/20/2023
我们需要看看你是如何生成的,因为这是问题所在,而不是绘图data

答: 暂无答案