R:阴谋而闪亮的条形图

R: plotly and shiny barplot

我正在用 plotly based on the World Phones example 制作条形图。

侧边栏面板不起作用,你能告诉我为什么吗?

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})
})

我不能完全理解你想看什么

但是例如,如果您只想按年绘制一个字母

1) 你有不好的 df 作为例子 -- A 只存在于 2011

2) 我用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) 尝试 p <- plot_ly(data[data$x==input$letter,], x=group, y=y,type = "bar")

不要忘记将 data 声明为 uiserver

而且只能在浏览器中工作,不能在 rstudion 查看器中工作(对我来说)

Batanicheck 给出的每件事都是正确的,但对情节稍作修改即可获得准确的输出。

替换

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

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

对我来说,它可以在浏览器和 Rstudio 查看器中使用...