Home > database >  How to display outputs based on radio button in R Shiny?
How to display outputs based on radio button in R Shiny?

Time:12-15

Hello i have a problem with R Shiny. I prepared 3 "outputs" on the server side - one to display dataset table, one to display summary() and one to display str(). Then I prepared radio buttons preview, str and summary. And what I want to achieve is reactive change of output based on radio button choice.

SERVER:

.
.
.
    output$contents =
      renderTable({
      
      
      req(input$file1)
      
      df <- read.csv(input$file1$datapath,
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote)
      
      if(input$disp == "head") {
        return(head(df))
      }
      else {
        return(df)
      }
      
    })
    
    output$summary = 
      renderPrint({
        req(input$file1)
        
        df <- read.csv(input$file1$datapath,
                       header = input$header,
                       sep = input$sep,
                       quote = input$quote)
        
      summary(df)
    })
    
    output$str = 
      renderPrint({
        req(input$file1)
        
        df <- read.csv(input$file1$datapath,
                       header = input$header,
                       sep = input$sep,
                       quote = input$quote)
        
        str(df)
      })
    

UI:

.
.
.

sidebarPanel(
  radioButtons(
    "dman_preview", "Display:",
    c("preview", "str", "summary"),
      selected = "preview",
      inline = TRUE
   ),
),                                   
                                  
mainPanel(
  tableOutput("contents"),
  verbatimTextOutput("summary"),
  verbatimTextOutput("str")
)

CodePudding user response:

Perhaps you are looking for this

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV file to upload", accept = ".csv"),
      radioButtons(
        "dman_preview", "Display:",
        c("preview", "str", "summary"),
        selected = "preview",
        inline = TRUE
      ),
    ),                                   
    
    mainPanel(
      uiOutput("myoutput")
    )
  )
)

server <- function(input, output){
  df1 <- reactive({
    # req(input$file1)
    # df <- read.csv(input$file1$datapath,
    #                header = input$header,
    #                sep = input$sep,
    #                quote = input$quote)
    # df
    cars ## temporary dataframe for testing purpose
  })
  
  output$contents <- renderTable({
    
    # if(input$disp == "head") {
    #   return(head(df1()))
    # }
    # else {
    #   return(df1())
    # }
    
    df1()
  })
  
  output$summary <- renderPrint({
    summary(df1())
  })
  
  output$str <- renderPrint({ str(df1()) })
  
  output$myoutput <- renderUI({
    switch(input$dman_preview, 
           "preview" = tableOutput("contents"),
           "str" = verbatimTextOutput("summary"),
           "summary" = verbatimTextOutput("str")
           )
  })
}

shinyApp(ui = ui, server = server)
  • Related