【问题标题】:How to display outputs based on radio button in R Shiny?如何根据 R Shiny 中的单选按钮显示输出?
【发布时间】:2022-01-17 18:03:50
【问题描述】:

您好,我对 R Shiny 有疑问。我在服务器端准备了 3 个“输出”——一个用于显示数据集表,一个用于显示 summary(),一个用于显示 str()。然后我准备了单选按钮previewstrsummary。而我想要实现的是基于单选按钮选择的输出的反应性变化。

服务器:

.
.
.
    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)
      })
    

用户界面:

.
.
.

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

【问题讨论】:

    标签: r shiny shinyapps shiny-reactivity


    【解决方案1】:

    也许你正在寻找这个

    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)
    

    【讨论】:

    • 它可以按我的需要工作,谢谢
    猜你喜欢
    • 2015-11-26
    • 2021-04-04
    • 2019-10-18
    • 2014-10-27
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多