Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.
.
.

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading