【问题标题】:Update Plot output on ActionButton click event in R shiny在R闪亮中更新ActionButton单击事件的绘图输出
【发布时间】:2019-07-28 16:22:31
【问题描述】:

我正在尝试使用ActionButton click 更新文本和绘图。

我的尝试-

library(shiny)
library(ggplot2)
library(shinyWidgets)

  ui <- fluidPage(
    actionGroupButtons(
      inputIds = c("Bar", "Histogram", "Line"),
      labels = list("Bar", "Histogram","Line"),
      status = "danger",
      fullwidth = T
    ),
   plotOutput('plot',height = '563px'),
   verbatimTextOutput('text')

  )

  server <- function(input, output) {
    output$plot <- renderPlot({

      if(req(input$Bar)!=0) {
      isolate({
        data <- iris
       ggplot(data, aes(Species,Petal.Length)) +
        geom_bar(stat="identity") 
      })

      } else if(req(input$Histogram)>0){
        isolate({
          data <-  iris
          ggplot(data, aes(Petal.Length)) +
            geom_histogram()

        })

      }  else if(req(input$Line)>0){
        isolate({
          data <-  iris
          ggplot(data, aes(Petal.Length,Sepal.Length)) +
            geom_line()

        })
      }

    })


    output$text <- renderText({

      if(req(input$Bar)!=0) {
        "Bar"

      } else if(req(input$Histogram)>0){

        "Histogram"

      }  else if(req(input$Line)>0){
        "Line"
      }

    })
  }

  shinyApp(ui, server)

我想在点击相应的操作按钮时更改绘图和文本。

【问题讨论】:

    标签: r shiny shinydashboard shiny-reactivity


    【解决方案1】:

    这是一种方法。 从本质上讲,RStudio 的action button example no. 3 中指出了该方法。

    library(shiny)
    library(ggplot2)
    library(shinyWidgets)
    
    ui <- fluidPage(
    
      actionGroupButtons(
        inputIds = c("Bar", "Histogram", "Line"),
        labels = list("Bar", "Histogram","Line"),
        status = "danger",
        fullwidth = T
      ),
    
      plotOutput('plot',height = '563px'),
      verbatimTextOutput('text')
    
    )
    
    server <- function(input, output) {
    
      v <- reactiveValues(data = iris,
                          plot = NULL,
                          text = NULL)
    
      observeEvent(input$Bar, {
        v$plot <- ggplot(v$data, aes(Species,Petal.Length)) +
                    geom_bar(stat="identity") 
        v$text <- "Bar"
      })
    
      observeEvent(input$Histogram, {
        data <- iris
        v$plot <- ggplot(v$data, aes(Petal.Length)) +
                     geom_histogram()
        v$text <- "Histogram"
      })  
    
      observeEvent(input$Line, {
        data <- iris
        v$plot <- ggplot(v$data, aes(Petal.Length,Sepal.Length)) +
                      geom_line()
        v$text <- "Line"
      })  
    
      output$plot <- renderPlot({
        if (is.null(v$plot)) return()
        v$plot
      })
    
    
      output$text <- renderText({
    
        if (is.null(v$text)) return()
        v$text
    
      })
    }
    
    shinyApp(ui, server)
    


    更新

    如果您在响应式数据中使用输入过滤器,则必须稍微调整上述方法:

    library(shiny)
    library(ggplot2)
    library(shinyWidgets)
    
    ui <- fluidPage(
    
        selectInput(inputId = "species", label = "Select species:",
                    choices = unique(as.character(iris$Species)),
                    selected = "setosa"),
    
        sliderInput("sepal_length", "Limit sepal length:",
                    round = 0,
                    min = range(iris$Sepal.Length)[1], max = range(iris$Sepal.Length)[2],
                    range(iris$Sepal.Length),
                    step = 0.1),
    
        actionGroupButtons(
            inputIds = c("Bar", "Histogram", "Line"),
            labels = list("Bar", "Histogram","Line"),
            status = "danger",
            fullwidth = T
        ),
    
        plotOutput('plot',height = '563px'),
        verbatimTextOutput('text')
    
    )
    
    server <- function(input, output) {
    
        data <- reactive({
    
            temp <- subset(iris, Species == input$species)
            subset(temp, Sepal.Length < input$sepal_length)
    
        })
    
        v <- reactiveValues(plot = NULL,
                            text = NULL)
    
        observeEvent(input$Bar, {
            v$plot <- ggplot(data(), aes(Species,Petal.Length)) +
                geom_bar(stat="identity") 
            v$text <- "Bar"
        })
    
        observeEvent(input$Histogram, {
            v$plot <- ggplot(data(), aes(Petal.Length)) +
                geom_histogram()
            v$text <- "Histogram"
        })  
    
        observeEvent(input$Line, {
            v$plot <- ggplot(data(), aes(Petal.Length,Sepal.Length)) +
                geom_line()
            v$text <- "Line"
        })  
    
        output$plot <- renderPlot({
            if (is.null(v$plot)) return()
            v$plot
        })
    
    
        output$text <- renderText({
    
            if (is.null(v$text)) return()
            v$text
    
        })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 如果我的data 来自reactive 函数怎么办。是否可以这样做v &lt;- reactiveValues(data = data(), plot = NULL, text = NULL)
    • @Rushabh:这行不通,因为您不能将反应式表达式放在反应式值中。在这种情况下,请参阅上面的更新答案。
    • 这很有帮助!!
    猜你喜欢
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 2021-05-19
    • 2016-02-20
    • 2020-07-02
    • 1970-01-01
    • 2021-04-30
    • 2020-08-09
    相关资源
    最近更新 更多