【问题标题】:How to include reactive output text Shiny如何包含响应式输出文本 Shiny
【发布时间】:2021-08-14 07:56:28
【问题描述】:

我正在使用多文件设置,因此我同时拥有 ui.r 和 server.r 文件。我正在使用下面的数据集(小样本),它查看了 2016 年美国/加拿大不同州/省的 UFO 目击事件。

        Date        Time           AM.PM   Country       City   State   Shape
1   12/21/2016     7:15:00          PM     USA      Waynesboro    VA    Sphere
2   12/21/2016     12:00:00         AM     USA      Louisville    KY    Unknown
3   12/20/2016     10:30:00         PM     USA      Santa Rosa    CA    Sphere
4   12/20/2016     7:00:00          PM     USA      Fresno        CA    Circle
5   12/19/2016     9:53:00          PM     USA      Reymert       AZ    Circle
6   1/11/2016      8:15:00          PM     CANADA   Mississauga   ON    Circle

到目前为止,在我的应用程序中,我创建了一个主面板,它显示一个条形图,其中包含在 x 轴上的“形状”列中观察到的所有不同形状以及在 y 轴上观察到的数量。我在侧面有一个小部件,允许用户选择是否要查看美国或加拿大的数据,并且绘图会相应更改。我想要做的是在绘图下的主面板中输出文本,该文本将显示“具有最多观察的形状:”,然后打印具有最高 y 值的形状的名称。对我来说困难的部分是打印的形状的名称需要根据图表上当前显示的数据进行更改。任何帮助表示赞赏!

用户界面文件:

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

shinyUI(fluidPage(

    # Application title
    titlePanel("Exploring UFO Sightings"),

    sidebarLayout(
        sidebarPanel(
            selectInput("Country",
                        "Country to Display:",
                        choices = list("USA" = 'USA',
                                       "Canada" = 'CANADA'),
                        ),
            checkboxGroupInput("type",
                               "Select Desired Shapes Observed:",
                               choices = unique(ufo_data$Shape),
                               selected = unique(ufo_data$Shape)),
        ),

        # Show a plot of the generated distribution
        mainPanel(
            plotOutput("ufoPlot"),
           # textOutput("Shape with the most observations:")
        )
    )
))

服务器文件:

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


shinyServer(function(input, output) {

    output$ufoPlot <- renderPlot({
        
        ufo_data_filter <- filter(ufo_data, Country == input$Country & Shape %in% input$type)
        ggplot(data = ufo_data_filter) +
            geom_bar(mapping = aes(x = Shape, fill = Shape))+
            labs(
                title = "Number of Different Shaped UFO Observations in Selected Country",
                x = "Shape of UFO Sighted",
                y = "Number of Observations in 2016"
            )
    })

})

【问题讨论】:

    标签: r ggplot2 dplyr shiny


    【解决方案1】:

    对于选择的数据子集,您可以使用count 查找每个Shape 的计数并选择出现次数最多的一个。

    library(shiny)
    library(ggplot2)
    library(dplyr)
    
    ui <- fluidPage(
      # Application title
      titlePanel("Exploring UFO Sightings"),
      sidebarLayout(
        sidebarPanel(
          selectInput("Country",
                      "Country to Display:",
                      choices = list("USA" = 'USA',
                                     "Canada" = 'CANADA'),
          ),
          checkboxGroupInput("type",
                             "Select Desired Shapes Observed:",
                             choices = unique(ufo_data$Shape),
                             selected = unique(ufo_data$Shape)),
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
          plotOutput("ufoPlot"),
          textOutput("text")
        )
      )
    )
    
    server <- function(input, output) {
      
      output$ufoPlot <- renderPlot({
        
        ufo_data_filter <- filter(ufo_data, Country == input$Country & Shape %in% input$type)
        ggplot(data = ufo_data_filter) +
          geom_bar(mapping = aes(x = Shape, fill = Shape))+
          labs(
            title = "Number of Different Shaped UFO Observations in Selected Country",
            x = "Shape of UFO Sighted",
            y = "Number of Observations in 2016"
          ) 
      })
      
      output$text <- renderText({
        text_data <- ufo_data %>%
          filter(Country == input$Country & Shape %in% input$type) %>%
          count(Shape, sort = TRUE)
        paste0('Shape with the most observations: ', text_data$Shape[1])
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 2015-09-23
      • 2019-02-07
      • 2015-12-15
      • 2018-07-14
      • 2020-01-02
      • 1970-01-01
      相关资源
      最近更新 更多