简体   繁体   中英

R slider range input shiny

This is the data input

because the original data is so big so I filter some country

total_data <-read.csv('https://raw.githubusercontent.com/owid/co2-data/master/owid-co2-data.csv', stringsAsFactors = F)

data_select <- total_data %>% 
  filter(country %in% c("Canada", "Albania", "China", "Egypt", "Cyprus"))

This is my server

the errors consoles shows that I have bugs with my filter

server <- function(input, output) { 
  output$plot <- renderPlot({
    data <- data_select %>%
      filter(year > input$year[1], year < input$year[2])
    
   ggplot(
      data = plot_data,
      mapping = aes_string(x = "year", y = input$y_var, color = "country")
    ) +
      geom_point() +
      labs(x = "year", y = input$y_var, title = "data")
  })
}

This is my ui

I setup a year range for slider

year_range <- range(data_select$year)

sidebar <- sidebarPanel(
  selectInput(
    "y",
    label = "Y Variable",
    choices = colnames(total_data),
    selected = "co2"
  ),
  sliderInput(
    "year",
    label = "Year", 
    min = year_range[1], 
    max = year_range[2], 
    value = year_range
  )
)

I cant find obvious errors, but when I run the app, no graph shows and it has tips likes this

Listening on http://127.0.0.1:6581
Warning in normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="C:/Users/29061/OneDrive/??": ????????????????
Warning in normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="C:/Users/29061/OneDrive/??": ????????????????
Warning in normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="C:/Users/29061/OneDrive/??": ????????????????

在此处输入图像描述

Some names where misplaced, input$year_range should be input$year and input$y_var should be input$y . The first argument of selectInput() ( inputId ) is for accessing it's value inside the server.

Here is the code for the app:

library(shiny)
library(tidyverse)

total_data <-read.csv('https://raw.githubusercontent.com/owid/co2-data/master/owid-co2-data.csv', stringsAsFactors = F)

data_select <- total_data %>% 
    filter(country %in% c("Canada", "Albania", "China", "Egypt", "Cyprus", 
                          "Japan", "Russia", "United States", "United Kingdom"))

year_range <- range(total_data$year)


# UI ----------------------------------------------------------------------

ui <- fluidPage(
   sidebarLayout(
   sidebarPanel(
        selectInput(
            "y",
            label = "Y Variable",
            choices = colnames(total_data),
            selected = "co2"
        ),
        sliderInput(
            "year",
            label = "Year", 
            min = year_range[1], 
            max = year_range[2], 
            value = year_range
        )), mainPanel = mainPanel(
            plotOutput('plot')
        )
    ))


# SERVER ------------------------------------------------------------------



server <- function(input, output) { 
    output$plot <- renderPlot({
        data <- data_select %>%
            filter(year > input$year[1], year < input$year[2])
        
        ggplot(
            data = data,
            mapping = aes_string(x = "year", y = input$y, color = "country")
        ) +
            geom_point() +
            labs(x = "year", y = input$y, title = "data")
    })
}




shinyApp(ui, server)


在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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