【问题标题】:X-axis of Bar chart doesn't update in Rshiny条形图的 X 轴在 R Shiny 中不更新
【发布时间】:2017-11-27 06:49:55
【问题描述】:

这是我的示例数据,我想编写一个闪亮的应用程序来在我的页面上显示一个反应图,但它有问题。

我的问题是当我在条形图中选择 x 轴时,闪亮的图不会更新,但 y 轴可以更新,所以我不知道问题出在哪里以及如何解决这个问题?

ui.R

library(shiny)
library(dplyr)
library(ggplot2)

#Data Input# 
Sample <- read.table("D:/Sample.csv", sep = ",", header = T) %>%
  subset(., select = c(2, 3, 4, 5, 6))

#Data Manipulation#
Sample[, c(3:5)] <- sapply(Sample[, c(3:5)], as.factor)
Catego.x <- colnames(Sample[, 3:5])
Catego.y <- colnames(Sample[, 3:5])
Conti <- colnames(Sample[, 1:2])

#ui.R#
shinyUI(navbarPage(
  "Recommendation System",
  navbarMenu(
    "Plot",
    tabPanel("Boxplot", 
             sidebarPanel(
               selectInput(inputId = "Conti",
                           label = "Continuous Variables:",
                           choices = Conti),
               selectInput(inputId = "Catego.x",
                           label = "Categories Variables:",
                           choices = Catego.x)
             ),
             mainPanel(
               plotOutput("boxplot")
             )),
    tabPanel("Barchart",
             sidebarPanel(
               selectInput(inputId = "Catego.x",
                           label = "Categories Variables.x:",
                           choices = Catego.x),
               selectInput(inputId = "Catego.y",
                           label = "Categories Variables.y:",
                           choices = Catego.y)
             ),
             mainPanel(
               plotOutput("barchart")
             ))
  )
))

服务器.R

library(shiny)

#Data Input#
Sample <- read.table("D:/Sample.csv", sep = ",", header = T) %>%
  subset(., select = c(2, 3, 4, 5, 6))

#Data Manipulation#
Sample[, c(3:5)] <- sapply(Sample[, c(3:5)], as.factor)
Catego.x <- colnames(Sample[, 3:5])
Catego.y <- colnames(Sample[, 3:5])
Conti <- colnames(Sample[, 1:2])

#server.R#
shinyServer(function(input, output){
  output$boxplot <- renderPlot({
    ggplot(data = Sample, aes_string(x = input$Catego.x, y = input$Conti, group = input$Catego.x)) +
      geom_boxplot()
  })
  output$barchart <- renderPlot({
    ggplot(data = Sample, aes_string(x = input$Catego.x, y = input$Catego.y, fill = input$Catego.y)) + 
      geom_bar(stat = "identity")
  })
})

【问题讨论】:

  • 您可以发布您的数据的 dput() 吗?否则我们无法重现您的问题

标签: r rstudio shiny


【解决方案1】:

我的猜测是导致问题的原因是eventHandler

您应该在 UI 中添加 actionButton,在服务器部分添加 eventHandler

用户界面:

shinyUI(navbarPage(
  "Recommendation System",
  navbarMenu(
    "Plot",
    tabPanel("Boxplot", 
             sidebarPanel(
               selectInput(inputId = "Conti",
                           label = "Continuous Variables:",
                           choices = Conti),
               selectInput(inputId = "Catego.x",
                           label = "Categories Variables:",
                           choices = Catego.x)
             ),
             mainPanel(
               plotOutput("boxplot")
             )),
    tabPanel("Barchart",
             sidebarPanel(
               selectInput(inputId = "Catego.x",
                           label = "Categories Variables.x:",
                           choices = Catego.x),
               selectInput(inputId = "Catego.y",
                           label = "Categories Variables.y:",
                           choices = Catego.y),
actionButton(
          inputId = "submit_loc",
          label = "Submit"),
             ),
             mainPanel(
               plotOutput("barchart")
             ))
  )
))

服务器:

shinyServer(function(input, output){
  observeEvent(
    eventExpr = input$submit_loc,
    handlerExpr = 
    {
  output$boxplot <- renderPlot({
    ggplot(data = Sample, aes_string(x = input$Catego.x, y = input$Conti, group = input$Catego.x)) +
      geom_boxplot()
  })
  output$barchart <- renderPlot({
    ggplot(data = Sample, aes_string(x = input$Catego.x, y = input$Catego.y, fill = input$Catego.y)) + 
      geom_bar(stat = "identity")
  })
})
})

试试这个,然后告诉我。

【讨论】:

  • 你可以为你的数据框做一个dput()吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 1970-01-01
相关资源
最近更新 更多