繁体   English   中英

条形图的X轴在Rshiny中不会更新

[英]X-axis of Bar chart doesn't update in Rshiny

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

在此处输入图片说明

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

在此处输入图片说明

用户界面

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")
             ))
  )
))

服务器

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")
  })
})

我的猜测是导致问题的原因是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")
  })
})
})

试试这个,让我知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM