首页 > 解决方案 > 在闪亮的应用程序中将轴值作为输入时不显示条形图

问题描述

我试图在下面闪亮的应用程序中创建一个条形图,我将在其中将y值作为输入,但作为回报,我的图中没有条形图。可能是什么问题?

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
library(dplyr)
library(plotly)
shinyApp(
    ui = dashboardPagePlus(
        header = dashboardHeaderPlus(title = "Social Media Metrics", titleWidth = 320
                                     
        ),
        sidebar = dashboardSidebar(width = 320,
                                   
                                   uiOutput("value")
                                   
        ),
        body = dashboardBody(
            plotlyOutput("plot") 
        )
        
        
    ),
    server = function(input, output) {
        
        page<-c("ONE","TWO","THREE")
        network<-c("INSTAGRAM","FACEBOOK","FACEBOOK")
        av<-c(3.5,4.2,8.7)
        growth<-c(4,7,9)
        av2<-c(3.5,7.2,4.7)
        growth2<-c(4,7,9)
        id <- rep("df1",3)
        df1<-data.frame(page,network,av,growth,av2,growth2,id)
        
        
        
        
        output$value<-renderUI({
            
                pickerInput(
                    inputId = "val"
                    ,
                    label = "Select Absolut Value" 
                    ,
                    choices = c("growth","growth2") #all rows of selected column
                    ,
                    multiple = F, options = list(`actions-box` = TRUE)
                    
                )
            
        })
        output$plot<-renderPlotly({
            fig <- plot_ly(df1, x = ~page, y = input$val, type = 'bar', name = 'SF Zoo')
        })
        
        
    }
)

标签: rshinyplotly

解决方案


尝试这个。以您想要的样式调用变量的最佳方法是使用索引df[,var],这样您的输入将被正确呈现。这里的代码:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
library(dplyr)
library(plotly)
shinyApp(
  ui = dashboardPagePlus(
    header = dashboardHeaderPlus(title = "Social Media Metrics", titleWidth = 320
                                 
    ),
    sidebar = dashboardSidebar(width = 320,
                               
                               uiOutput("value")
                               
    ),
    body = dashboardBody(
      plotlyOutput("plot") 
    )
    
    
  ),
  server = function(input, output) {
    
    page<-c("ONE","TWO","THREE")
    network<-c("INSTAGRAM","FACEBOOK","FACEBOOK")
    av<-c(3.5,4.2,8.7)
    growth<-c(4,7,9)
    av2<-c(3.5,7.2,4.7)
    growth2<-c(4,7,9)
    id <- rep("df1",3)
    df1<-data.frame(page,network,av,growth,av2,growth2,id)
    
    
    
    
    output$value<-renderUI({
      
      pickerInput(
        inputId = "val"
        ,
        label = "Select Absolut Value" 
        ,
        choices = c("growth","growth2") #all rows of selected column
        ,
        multiple = F, options = list(`actions-box` = TRUE)
        
      )
      
    })
    output$plot<-renderPlotly({
      fig <- plot_ly(x = df1[,'page'], y = df1[,input$val], type = 'bar', name = 'SF Zoo')
    })
    
    
  }
)

输出:

在此处输入图像描述


推荐阅读