有光泽的条形图

Plotly barplot in shiny

我不熟悉 R 中的情节和闪亮,运行 遇到了麻烦。我想创建一个提供交​​互式控件的网络应用程序。 当我用 plotly 创建条形图时,它按预期工作:

dframe <- structure(list(year = c(2001L, 2015L, 2008L, 1998L, 2010L, 2014L, 
2011L, 2000L, 1999L, 2002L, 2009L, 2004L, 2002L, 2001L, 2000L, 
2005L, 1999L, 2010L, 2008L, 2009L), group = c("g1", "g1", "g3", 
"g4", "g1", "g2", "g3", "g1", "g4", "g4", "g4", "g1", "g1", "g2", 
"g4", "g2", "g3", "g2", "g4", "g1"), a = c(1.52607997416485, 
1.04759480943941, 0.107493758781699, 0.324240601814991, 1.21690191039983, 
0.692957144550164, 0.100093012488437, 1.71665754922973, 0.240105539757669, 
0.301547489991739, 0.302357540012235, 1.33122876748703, 1.38077154171675, 
1.04945558620643, 0.473695236959259, 0.932153022210017, 0.187716106613411, 
0.77994812624845, 0.305121503398045, 1.22562181179695), b = c(0.958208435975803, 
0.711807102412401, 0.004286146047254, 0.860752045150301, 0.609128805282551, 
0.001735185869065, 0.004860211078283, 1.41344172542771, 0.627298616424719, 
0.680489683211967, 0.323901911737874, 0.722214510491074, 0.689447036840275, 
0.002003929941981, 1.40342075327254, 0.002056570032725, 0.00582939081292, 
0.001721659850972, 0.480673786834984, 0.330424319020086), c = c(0.002302648415411, 
0.001580495440423, 0.000254331658462, 0.001184698767457, 0.002020079296312, 
0.000718353074191, 0.000319003844453, 0.002960677807283, 0.000715716710628, 
0.000655635163186, 0.00089830508902, 0.00260764960546, 0.00194129091791, 
0.001031050981244, 0.00131517281002, 0.000936907281673, 0.000542640688632, 
0.000789227706725, 0.000777242962328, 0.001952474894292)), row.names = c(5L, 
18L, 30L, 59L, 13L, 55L, 33L, 4L, 60L, 63L, 69L, 7L, 6L, 43L, 
61L, 46L, 22L, 51L, 68L, 12L), class = "data.frame")  

library(plotly) 
library(shiny)

dframe %>%
  plot_ly(x = ~year,
          y = ~a,
          type = 'bar',
          color = ~group)  

然而,当我尝试用它制作应用程序时,情节看起来完全不同。

nms = names(dframe[3:5])

ui <- fluidPage(

  sidebarPanel(
    selectInput('y', 'INPUT_Y', choices = nms, selected = 'a')
  ),
  mainPanel(
    plotlyOutput('plot', height = "700px", width = "600px")
  )
)

server <- function(input, output) {

  output$plot <- renderPlotly({
    plot_ly(data = dframe,
            x = ~year,
            y = input$y,
            type = 'bar',
            color = ~group)
  })
}

shinyApp(ui, server)

您忘记了 y 变量前的“~”符号并使用了 get() 函数。
请尝试以下操作:

server <- function(input, output) {
    output$plot <- renderPlotly({
        plot_ly(
            data = dframe,
            x = ~ year,
            y = ~ get(input$y),
            type = 'bar',
            color = ~ group
        )
    })
}

您也可以明确引用该专栏。不是最好的,但它有效。

nms = names(dframe[3:5])

ui <- fluidPage(

  sidebarPanel(
    selectInput('y', 'INPUT_Y', choices = nms, selected = 'a')
  ),
  mainPanel(
    plotlyOutput('plot', height = "700px", width = "600px")
  )
)

server <- function(input, output) {

  output$plot <- renderPlotly({

    print(input$y)

    plot_ly(data = dframe,
            x = ~year,
            y = dframe[,input$y],
            type = 'bar',
            color = ~group)
  })
}

shinyApp(ui, server)