r - Shiny App中反应式条形图的问题
问题描述
我正在尝试生成一个闪亮的应用程序,以响应式创建此条形图。它是一个条形图,纵轴显示接种疫苗的人数,横轴显示纽约或波士顿等地区。
我包含了完整的代码,我不知道为什么如果我输入 hchart: y = df [, 2] 它可以工作,但如果我输入 y = df [, colm] 它不会。有什么建议吗?
# 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)
解决方案
您应该将变量名称传递到hcaes
. y 变量是names(df[colm]))
,可以使用 将其转换为符号表达式!!sym()
:
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())
})
推荐阅读
- html - 这两个怎么分开从一条线?
- c# - 在 C# 的不同页面中使用/调用页面的方法/函数
- javascript - 在 Promise 完成后让 React 重新渲染
- javascript - eval() 和 eval`` 之间的区别(带反引号)
- java - 由 `mvn package` 创建的 jar 归档文件是否既不包含其依赖项也不指示哪个 .class 文件包含 main()?
- php - Wordpress 有条件地更改 WP_Query 参数
- python - Python for循环没有遍历所有元素
- ansible - ansible:如何更改 $HOME 目录
- vue.js - 如何导入打包成 JavaScript 库的 Vue 组件?
- java - Java,使用compareTo方法在ToString中按字母顺序打印arraylist