简体   繁体   中英

R: plotly and shiny barplot

I'm making a barplot with plotly based on the World Phones example .

The sidebarPanel does not work, can you tell me why?

data <- data.frame(x=rep(c('A','B','C'), each = 1, times = 3),
                   y=runif(9, 5, 10),
                   group=rep(c('2011','2012','2013'), each = 1, times= 3))

ui.R:

library(shiny)
library(plotly)
# Define the overall UI
shinyUI(
# Use a fluid Bootstrap layout
fluidPage(    
# Give the page a title
titlePanel("Barchart"),
# Generate a row with a sidebar
sidebarLayout(      
# Define the sidebar with one input
sidebarPanel(
selectInput("letter", "Letter:", levels(data[,1])),
hr()),
# Create a spot for the barplot
mainPanel(
plotlyOutput("dataPlot")  
))))

server.R:

library(shiny)
library(plotly)
shinyServer(function(input, output) {
# Fill in the spot we created for a plot
output$dataPlot <- renderPlotly({
# Render a barplot
p <- plot_ly(data, x = input$letter, y=y, group=group,type = "bar")
p})
})

Every thing given by Batanicheck is correct, but a little modification to the plot get your exact output.

Replace

> p <- plot_ly(data[data$x==input$letter,], x=group, y=y,type = "bar")

WITH

> p <- plot_ly(data[data$x==input$letter,], x=x, y=y, group=group,type = "bar")

For me it works in browser as well as Rstudio viewer...

I cant fully understan what you wnat to see

but for example if you want to plot only one letter by years

1) You have bad df as example -- A exist only into 2011

2) I use data <- data.frame(x=rep(c('A','B','C'), each = 1, times = 3), y=runif(9, 5, 10), group=rep(c('2011','2012','2013'), each = 3, times= 1))

3) try p <- plot_ly(data[data$x==input$letter,], x=group, y=y,type = "bar")

Dont forget declare data into ui and server

And work only in browser not into rstudion viewer ( for me)

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