简体   繁体   中英

R Shiny One actionButton for multiple Output

I'm trying to display two tables separately (dt2 and dt3) on Shiny mainPanel after clicking on an actionButton. dt2 involves the first five cars for the chosen type (mpg dataset), while dt3 calculates the mean cty for a chosen year. Unfortunately it doesn't work as I get only this tiny table:

在此处输入图像描述

How can I display both table on the main panel separately? (eg dt2 on the left side, dt3 on the right)

Note: dt2 and dt3 are related in a sense that dt3 derived from dt2

UI side:

library(shiny)
    shinyUI(fluidPage(
        sidebarLayout(
            sidebarPanel(
    
              selectInput("manufacturer", "Car Type:", c("audi","chevrolet")),
              selectInput("year", "Year:", c("1999","2008")),
              actionButton("action", "Go!")
                         ),
    
            mainPanel(tableOutput("cty_mean")) 
                     ) 
                     ))

Server side:

shinyServer(function(input, output) {

  mydata <- eventReactive(input$action, {  
    
     library(ggplot2)
     library(dplyr)
    
     dt <- mpg
     dt2 <- dt %>% 
       filter(manufacturer==input$manufacturer) %>% 
       mutate(mean = mean(cty)) %>% slice(1:5)
     dt2
    
     dt3 <- dt2 %>% group_by(input$year) %>% 
       summarise(mean = mean(cty))
     dt3

    })
   
   output$cty_mean <- renderTable({       mydata()           })


})

In your code, mydata() is a reactive function with output dt3 (the last line of the function), this is why you only get one table as result.

You could use reactiveValues combined with observeEvent :

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

ui <-shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      
      selectInput("manufacturer", "Car Type:", c("audi","chevrolet")),
      selectInput("year", "Year:", c("1999","2008")),
      actionButton("action", "Go!")
    ),
    
    mainPanel(tableOutput("dt2"),
              tableOutput("dt3")) 
  ) 
))


server <-shinyServer(function(input, output) {
  
  mydata <- reactiveValues()
  
  observeEvent(input$action, {  
    dt <- mpg
    mydata$dt2 <- dt %>% 
      filter(manufacturer==input$manufacturer) %>% 
      mutate(mean = mean(cty)) %>% slice(1:5)
    
    mydata$dt3 <- mydata$dt2 %>% group_by(input$year) %>% 
      summarise(mean = mean(cty))
  })
  
  output$dt2 <- renderTable({       mydata$dt2           })
  output$dt3 <- renderTable({       mydata$dt3           })
  
})

shinyApp(ui,server) 

I would split the mydata() into two different reactive events:

library(shiny)
shinyUI(fluidPage(
    sidebarLayout(
        sidebarPanel(
            selectInput("manufacturer", "Car Type:", c("audi","chevrolet")),
            selectInput("year", "Year:", c("1999","2008")),
            actionButton("action", "Go!")
        ),
        mainPanel(tableOutput("cty_mean1"),
                  tableOutput("cty_mean2")) 
    ) 
))

shinyServer(function(input, output) {
        
        library(ggplot2)
        library(dplyr)
        
        mydata1 <- eventReactive(input$action, {  
            dt <- mpg
            dt2 <- dt %>% 
                dplyr::filter(manufacturer==input$manufacturer) %>% 
                dplyr::mutate(mean = mean(cty)) %>% slice(1:5)
            dt2
        })
        
        mydata2 <- eventReactive(input$action, {  
            dt3 <- mydata1() %>% group_by(input$year) %>% 
                summarise(mean = mean(cty))
            dt3
        })

        output$cty_mean1 <- renderTable({       mydata1()           })
        output$cty_mean2 <- renderTable({       mydata2()           })
    })

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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