r - 如何使用 plotly 创建条形图?
问题描述
我正在尝试使用用户输入生成条形图。我尝试~
在输入之前使用,但它不起作用。我看到的是带有轴标签的空白画布。
代码
library(shiny)
library(plotly)
ui <- fluidPage(
titlePanel(tags$h4("Bar Chart")),
sidebarLayout(
sidebarPanel(selectInput("input1", label = NULL, choices = names(mtcars)),
selectInput("input2", label = NULL, choices = names(mtcars))),
mainPanel(plotlyOutput("bar"))
)
)
server <- function(input, output, session) {
output$bar <- renderPlotly(
mtcars %>%
plot_ly(
x = ~input$input1,
y = ~input$input2,
type = "bar"
)
)
}
shinyApp(ui, server)
解决方案
您可以使用get()
将输入变量转换为 plotly 可以使用的东西。在你的情况下试试这个:
output$bar <- renderPlotly(
mtcars %>%
plot_ly(
x = ~get(input$input1),
y = ~get(input$input2),
type = "bar"
)
推荐阅读
- asp.net-core - 如何使用 OpenIdConnect 身份验证登录 ASP.NET Core 身份用户?
- ckan - 是否可以在 CKAN Data Explorer 默认预览中隐藏 _id 列?
- jquery - JqueryUI 数据表设置记录总数
- c - 为什么 C 文件流的开头称为“SEEK_SET”?
- php - jQuery ajax 响应返回格式化的 HTML
- amazon-web-services - 在 Terraform 中使用已创建资源的最佳实践是什么?
- ruby-on-rails - Rails 6 使用 bundler 2 部署到 Heroku,但仍然卡顿
- python - 动画 x 和 y 数组
- excel - 宏只运行第一个结果
- java - 如何从 React 前端使用 Spring 后端将文件上传到 S3 存储桶?