【问题标题】:Plotly barplot in shiny以闪亮的方式绘制条形图
【发布时间】:2019-09-25 19:21:12
【问题描述】:

我不熟悉 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)

【问题讨论】:

    标签: r shiny r-plotly


    【解决方案1】:

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

    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)
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2020-08-10
        • 1970-01-01
        • 2020-05-08
        • 1970-01-01
        • 2018-04-26
        • 2020-10-31
        • 1970-01-01
        • 2020-02-04
        • 2016-03-25
        相关资源
        最近更新 更多