【问题标题】:Unable to display table in R shiny无法在 R 闪亮中显示表格
【发布时间】:2023-04-06 14:43:02
【问题描述】:

我和我的朋友刚接触闪亮。

我们正在努力自学闪亮。除了一个大大小小的问题,一切都很好。

最大的问题是:为什么我们不能用renderTable() 显示表格?我们只能显示年份范围。

小问题是:我们可以在相关图中显示侧边栏标签而不是在 csv 中分配的标签吗?

非常感谢。

library(shiny)
library(shinydashboard)


ui <- fluidPage(sidebarLayout(
  sidebarPanel
  (
    checkboxGroupInput(
      "feature",
      "Feature",
      c(
        "No of case" = "no_case",
        "Minor Case" = "minor_case",
        "All Non Fatal Case" = "all_non_fatl",
        "Fatal Case" = "fatal_case"
      )
    ),
    sliderInput(
      "year",
      "Year",
      min = 2015,
      max = 2021,
      value = c(2015, 2021)
    )
  ),
  
  mainPanel(tabsetPanel(
    tabPanel("Plot", plotOutput("correlation_plot")),
    tabPanel("Table", tableOutput("ecd"))
  ))
))



server <- function(input, output) {
  yearrange <- reactive({
    input$year[1]:input$year[2]
  })
  output$correlation_plot <- renderPlot({
    validate(need(input$feature, 'Check one of these items.'))
    plot(ecd$year,
         ecd[[input$feature]],
         xlab = "Year",
         ylab = input$feature) #how not to show tab name but show the side bar name
  })
  output$ecd <- renderTable({
    yearrange()
  })
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny reactive


    【解决方案1】:

    您尚未共享数据 (ecd),因此我无法对其进行测试,但您可以尝试一下。

    关于大问题

    Shiny 显示您告诉它显示的内容。在reactive 函数中,您使用了input$year[1]:input$year[2],因此它显示了从起始滑块值到结束滑块值的序列。我猜这个,但我认为ecd 数据有一个名为year(或其他)的列,它具有年份值。您可以使用subset(或filter 中的dplyr)对数据中这些特定年份的数据进行子集化。

    小问题

    创建一个命名向量 (vec),以便您可以轻松地在 checkboxGroupInput 中使用它,以及在 ylab 中使用它来命名 Y 轴。

    library(shiny)
    library(shinydashboard)
    
    c(
      "No of case" = "no_case",
      "Minor Case" = "minor_case",
      "All Non Fatal Case" = "all_non_fatl",
      "Fatal Case" = "fatal_case"
    ) -> vec
    
    ui <- fluidPage(sidebarLayout(
      sidebarPanel
      (
        checkboxGroupInput(
          "feature",
          "Feature",
          vec
        ),
        sliderInput(
          "year",
          "Year",
          min = 2015,
          max = 2021,
          value = c(2015, 2021)
        )
      ),
      
      mainPanel(tabsetPanel(
        tabPanel("Plot", plotOutput("correlation_plot")),
        tabPanel("Table", tableOutput("ecd"))
      ))
    ))
    
    
    
    server <- function(input, output) {
      yearrange <- reactive({
        subset(ecd, year %in% input$year[1]:input$year[2])
      })
      
      output$correlation_plot <- renderPlot({
        validate(need(input$feature, 'Check one of these items.'))
        plot(ecd$year,
             ecd[[input$feature]],
             xlab = "Year",
             ylab = names(vec[vec == input$feature])) 
      })
      output$ecd <- renderTable({
        yearrange()
      })
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 它适用于表格标签!先谢谢了!让我消化一下。将要求您从您的代码中获得进一步的指导。 :)
    猜你喜欢
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多