首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >闪亮应用程序中的反应式条形图问题

闪亮应用程序中的反应式条形图问题
EN

Stack Overflow用户
提问于 2021-05-29 19:12:01
回答 1查看 30关注 0票数 0

我正在尝试生成一个闪亮的应用程序,它可以反应地创建这个条形图。它是一个条形图,在纵轴上显示接种疫苗的人数,在横轴上显示地区,如纽约或波士顿。

我包含了完整的代码,我不知道为什么如果我把y= df,2放在图表中:y= df,2它可以工作,但如果我放y=df,colm它就不能。有什么建议吗?

代码语言:javascript
复制
# Libraries
libraries <- c("jsonlite", "ggplot2")

for(lib in libraries){
  eval(bquote(library(.(lib))))
}

if (!require("pacman")) install.packages("pacman")
pacman::p_load(shiny, ggplot2, plotly, shinythemes, shinyWidgets, highcharter, DT)

df = data.frame(
  zone = c("Boston", "NY", "Chicago"),
  teenagers = c(533, 489, 584),
  adults = c(120, 201, 186),
  elder = c(156, 153, 246)
)


ui2 <- navbarPage(
  
  paste('Vaccination Evolution in USA'),
  
  theme = shinytheme("cerulean"),
  
  tabPanel('Evolution by age',
           
           sidebarLayout(
             
             sidebarPanel(
               selectInput('variable', h5('Select the age range to visualize'),
                           choices = c("Teens" = 2,
                                       "Adults" = 3, 
                                       "Elder" = 4), 
                           selected=2
                          ),
               helpText("Age range in USA")
                         ),
             
             mainPanel(
               textOutput("text1"),
               titlePanel(h4('Evolution of the vaccination in USA by age and zone')),
               highchartOutput('histogram'),
               br(),
               helpText("Interact with the graph")
                      )
             
                     )
          )
     )





#   Server building
server2 <- function(input, output) {
  
  output$text1 <- renderText({ 
    colm = as.numeric(input$variable)
    paste("Age group shown is:", names(df[colm]))
    
  })
  
  output$histogram <- renderHighchart({
    
    colm = as.numeric(input$variable)
    
    hchart(df, type = 'column', hcaes(x = df[, 1], y = df[, colm]), name = "Number of people vaccinated")%>%
      
      hc_title(text = paste("Number of people vaccinated with at least one dose" ),
               style = list(fontWeight = "bold", fontSize = "20px"), align = "center")%>% 
      
      hc_yAxis(title=list(text=paste("Number")))%>%
      
      hc_xAxis(title=list(text=paste('Region')))%>%
      
      hc_add_theme(hc_theme_ggplot2())
  })
  
  
}

#shiny APP
shinyApp(ui = ui2, server = server2)
EN

回答 1

Stack Overflow用户

发布于 2021-05-29 19:35:18

您应该将变量名传递给hcaes。Y变量是names(df[colm])),可以使用!!sym()将其转换为符号表达式

代码语言:javascript
复制
output$histogram <- renderHighchart({
        
        colm = as.numeric(input$variable)
        
        hchart(df, type = 'column', hcaes(x = zone, y = !!sym(names(df[colm]))), name = "Number of people vaccinated")%>%
            
            hc_title(text = paste("Number of people vaccinated with at least one dose" ),
                     style = list(fontWeight = "bold", fontSize = "20px"), align = "center")%>% 
            
            hc_yAxis(title=list(text=paste("Number")))%>%
            
            hc_xAxis(title=list(text=paste('Region')))%>%
            
            hc_add_theme(hc_theme_ggplot2())
    })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67750668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档