I have a CVS file similar to:
Country | Category | Value |
---|---|---|
Canada | A | 3 |
Canada | B | 4 |
Canada | C | 5 |
USA | A | 3 |
USA | C | 2 |
Mexico | B | 5 |
Japan | A | 6 |
Japan | B | 3 |
Japan | C | 2 |
I would like to create a bar chart which has the Countries on the X axis and the Values on the Y axis. I would also like to have a side panel dropdown filter with the Categories listed and only show the data which corresponds to the selected category. So if the A category was selected the Value of the Canada bare would be 3. I am having a lot of trouble apply filters to my X and Y axis. This is what I have so far:
library(shiny)
library(ggplot2)
library(scales)
library(data.table)
library(plotly)
library(dplyr)
df=read.csv("name.cvs")
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(
sidebarPanel(
selectInput("Category", "Select Category", choices = df$Category)
),
mainPanel(
plotlyOutput("chart")
)
)
)
server <- function(input, output, session) {
output$chart <- renderPlotly({
p <- plot_ly(
x = df$Country,
y = df$VALUE,
name = "Important Data",
type = "bar" ,
)
})
}
shinyApp(ui, server)
You can probably tell I am new to R shiny and I am really struggling with this project. Any help would really be appreciated!
CodePudding user response:
You can refer to dropdown value using input$Category
and a simple filtering using base R subset
or dplyr::filter
should be enough.
library(shiny)
library(plotly)
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(
sidebarPanel(
selectInput("Category", "Select Category", choices = df$Category)
),
mainPanel(
plotlyOutput("chart")
)
)
)
server <- function(input, output, session) {
output$chart <- renderPlotly({
sub_df <- subset(df, Category == input$Category)
p <- plot_ly(
x = sub_df$Country,
y = sub_df$Value,
name = "Important Data",
type = "bar" ,
)
})
}
shinyApp(ui, server)