Interactive visuals

r
datavisualization

#1

Hi dears,
I have a data set with 4 variables, below is how my data set looks.

image
m8 m7 q1 percent
2007 Kabul right 43.60567
2007 Kapisa right 37
2007 Parwan right 62.68935
2007 Wardak right 38.57143

I am using below codes in R to visualize it interactively, i would very much appreciate if someone could guide me what is wrong in these code, i am not getting any error message but at the final stage i am not able to visualize it.

library(readxl)
data <- read_excel(“province.xlsx”)
head(data)

data <- data[,c(1,2,3,4)]
names(data) <- c(“year”, “province”, “indicator”, “value”)

ui <- shinyUI(
fluidPage(
titlePanel(“Direction of the country”),
sidebarLayout(
sidebarPanel(
selectInput(“province”, “Select province:”, choices = as.character(levels(factor(data$province))), selected = “Kabul”),
selectInput(“indicator”, “Select indicator:”, choices = as.character(levels(factor(data$indicator))), selected = “right”),
sliderInput(“year”, “Select Years:”, min = min(data$year), max = max(data$year), value = c(min(data$year), max(data$year)))
),
mainPanel(
plotlyOutput(“myplot”)
)
)
)
)

server <- shinyServer(function(input, output) {
myplot <- renderPlotly({
g <- data[c(data$province %in% input$province & data$indicator %in% input$value & data$year %in% input$year[1]:input$year[2]), ] %>%
ggplot() + geom_line(mapping = aes(x=year, y=value, color = province)) + labs(x = “Year”, y = “value”) + theme_bw()
ggplotly(g)
})
})

shinyApp(ui = ui, server = server)

With thanks,
Fahim


#2

couple of changes fixed the code.

output$myplot <- renderPlotly({

g <- data[c(data$province %in% input$province & data$indicator %in% input$indicator & data$year %in% input$year[1]:input$year[2]), ] %>%

ggplot() + geom_line(mapping = aes(x=year, y=value, color = province)) + labs(x = “Year”, y = “value”) + theme_bw()

ggplotly(g)

})