在R shiny中,点击ActionButton时更新Plot输出

3

我正在尝试通过ActionButton点击更新文本和图表。

我的尝试-

最初的回答-

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)

当适当的动作按钮被点击时,我希望能够更改情节和文本。

最初的回答:

在单击适当的操作按钮时,您可以更改情节和文本。

1个回答

13

以下是一种实现方法。 本质上,这种方法在RStudio的动作按钮示例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 <- reactiveValues(data = data(), plot = NULL, text = NULL) - undefined
1
@Rushabh:这样做是行不通的,因为你不能在reactiveValue中放置一个reactive expression。在这种情况下,请参考我上面更新的答案。 - undefined
这很有帮助!! - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接