【问题标题】:R - Shiny - Dynamic selection bar plotR - Shiny - 动态选择条图
【发布时间】:2020-03-12 16:39:11
【问题描述】:

我想要一个条形图 i R / Shiny 有动态。 我选择了一个国家,然后我只想查看这个国家的数据。 我已经创建了一些 R 代码,但是缺少国家选择和条形图之间的关系。

server.R 文件

library(datasets)
salg <- read_excel("C:\\Users\\Tue Hellstern\\Documents\\Demo\\Demo\\data\\SalgsData.xlsx", sheet = "salgs_data")

# Define a server for the Shiny app
function(input, output) {


  output$selected_var <- renderText({ 
    paste("You have selected", input$valgtLand)
  })

  # Fill in the spot we created for a plot
  output$salgplot <- renderPlot({

    # Render a barplot
    salg %>%
      ggplot(aes(x=CompanyType, y=Total)) +
      geom_bar(stat="identity")
  })
}

ui.R 文件

library(datasets)
salg <- read_excel("C:\\Users\\Tue Hellstern\\Documents\\Demo\\Demo\\data\\SalgsData.xlsx", sheet = "salgs_data")

# Use a fluid Bootstrap layout
fluidPage(    

    # Give the page a title
    titlePanel("Salg efter kundetype"),

    # Generate a row with a sidebar
    sidebarLayout(      

        # Define the sidebar with one input
        sidebarPanel(helpText("Du har mulighed for at vaelge kun at se et bestemt land"), 

                     selectInput("valgtland", h3("Vaelg land"), 
                                 choices = salg$Country, 
                                 selected = 1)),

        # Create a spot for the barplot
        mainPanel(
            plotOutput("salgplot")  
        )

    )
)

我得到了这个布局,但是如何进行选择 //​​ 条形图关系?

enter image description here

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您需要在绘图前根据input$valgtland 过滤您的数据。

    使用 iris 数据集的模拟示例,因为您没有提供可用数据:

    library(shiny)
    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    library(ggplot2)
    
    server <- function(input, output) {
        output$selected_var <- renderText({
            paste("You have selected", input$valgtLand)
        })
    
        # Fill in the spot we created for a plot
        output$salgplot <- renderPlot({
            # Render a barplot
            dplyr::filter(iris, Species == input$valgtland) %>%
                ggplot(aes(x=cut_interval(Petal.Width, n=4), y=Sepal.Length)) +
                geom_bar(stat="identity")
        })
    }
    
    ui <- fluidPage(    
    
        # Give the page a title
        titlePanel("Salg efter kundetype"),
    
        # Generate a row with a sidebar
        sidebarLayout(      
    
            # Define the sidebar with one input
            sidebarPanel(helpText("Du har mulighed for at vaelge kun at se et bestemt land"), 
    
                selectInput("valgtland", h3("Vaelg land"), 
                    choices = unique(iris$Species), 
                    selected = "setosa")),
    
            # Create a spot for the barplot
            mainPanel(
                plotOutput("salgplot")  
            )
    
        )
    )
    
    shinyApp(ui = ui, server = server)
    #> 
    #> Listening on http://127.0.0.1:5699
    

    reprex package (v0.3.0) 于 2020-03-12 创建

    【讨论】:

    • 感谢您的回答和时间。
    猜你喜欢
    • 2019-06-14
    • 2022-01-02
    • 2013-02-27
    • 2017-04-25
    • 2019-01-19
    • 2017-05-31
    • 1970-01-01
    • 2018-05-28
    • 2016-12-21
    相关资源
    最近更新 更多