多个group_by闪亮的应用程序进行绘制

问题描述 投票:1回答:1

\

我是R Shiny的真正初学者。

我在下面的链接中有类似的问题。multiple group_by in shiny app

而不是按照上面的链接中的说明制作一张工作表/我进行管理的表。

我想作图,最好是用hchart。由于分组依据,我将在其中切换信息。困难的部分/或者不起作用的是将group_by放在x轴上。


## hier de tabel versie
df2 <- readRDS("Data.rds")


library(shiny)
library(DT)
library(dplyr)

ui <- fluidPage(

  titlePanel("Dashboard"),

  sidebarLayout(
    sidebarPanel(

      uiOutput("groups")

    ),


    mainPanel(

      DT::dataTableOutput("summary")
    )
  )
)


server <- function(input, output) {
  mydata <- reactive({
    data <- df2 
    data
  })




  output$groups <- renderUI({
    df <- mydata()
    selectInput(inputId = "grouper", label = "Group variable", choices = c("L","Lt","Lp"), selected = "L")
  })




  summary_data <- reactive({
    req(input$grouper)
    mydata() %>%
      dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
      dplyr::summarise(aantal = n()) %>% 
      dplyr::arrange(desc(aantal)) 
  })

  output$summary <- DT::renderDataTable({
    DT::datatable(summary_data())
  })



}


shinyApp(ui, server)



上面的代码有效,但是我试图绘制这样的图:

df2 <- readRDS("Data.rds")


library(shiny)
library(highcharter) 
library(dplyr)

ui <- fluidPage(

  titlePanel("Dashboard"),

  sidebarLayout(
    sidebarPanel(

      uiOutput("groups")

    ),


    mainPanel(

      highchartOutput("plotje")
    )
  )
)


server <- function(input, output) {
  mydata <- reactive({
    data <- df2  
    data
  })




  output$groups <- renderUI({
    df <- mydata()
    selectInput(inputId = "grouper", label = "Group variable", choices = c("L","Lt","Lp"), selected = "L")
  })




  summary_data <- reactive({
    req(input$grouper)
    mydata() %>%
      dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
      dplyr::summarise(aantal = n()) %>% 
      dplyr::arrange(desc(aantal))
  })

  output$plotje <- renderHighchart({
    data <- summary_data()
    hchart(data, "column", hcaes(x = "grouper" , y = aantal)) # --> de plot zelf komt in het output deel van de UI
  })



}


shinyApp(ui, server)





有人可以帮我吗?!

提前感谢!

亲切的问候,

Steffie

r highcharts shiny dplyr
1个回答
1
投票

您在input$grouper变量中有grouper列。这只是取消报价的问题。

hchart(data, "column", hcaes(x = "grouper" , y = aantal))行应为:hchart(data, "column", hcaes(x = !!input$grouper , y = aantal))

完整示例(使用iris数据,因为您没有提供自己的数据示例):

library(shiny)
library(DT)
library(highcharter)
library(dplyr)

ui <- fluidPage(titlePanel("Dashboard"),

                sidebarLayout(
                  sidebarPanel(uiOutput("groups")),

                  mainPanel(DT::dataTableOutput("summary"),
                            highchartOutput("plot"))
                ))


server <- function(input, output) {
  mydata <- reactive({
    iris
  })

  output$groups <- renderUI({
    df <- mydata()
    selectInput(
      inputId = "grouper",
      label = "Group variable",
      choices = c("Petal.Length", "Species"),
      selected = "Species"
    )
  })

  summary_data <- reactive({
    req(input$grouper)
    mydata() %>%
      dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
      dplyr::summarise(aantal = n()) %>%
      dplyr::arrange(desc(aantal))
  })

  output$summary <- DT::renderDataTable({
    DT::datatable(summary_data())
  })

  output$plot <- renderHighchart({
    req(input$grouper)
    data <- summary_data()
    hchart(data, "column", hcaes(x = !!input$grouper, y = aantal))
  })

}

shinyApp(ui, server)

screencapture of working shiny app

© www.soinside.com 2019 - 2024. All rights reserved.