【问题标题】:How to customize fluidRow in Shiny如何在 Shiny 中自定义fluidRow
【发布时间】:2019-02-10 01:45:04
【问题描述】:

所以我正在尝试创建此输出,但使用 Shiny 以仪表板形式。需要注意的是,这是我使用 Shiny 的第二周,所以我非常缺乏经验。

Figure

这是我的代码:

library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(gapminder)


gm <- gapminder 
gm <- transform(gm, pop = as.numeric(pop))

cgm <- gm %>% group_by(continent,year) %>% summarise(totpop=sum(pop),avglifeExp=sum(pop*lifeExp)/totpop,avggdpPercap=sum(pop*gdpPercap)/totpop, numCountries=n())

body <- dashboardBody(

    fluidRow(

     tabBox(
      title = "Population",
      id = "proj", width=12,      
      tabPanel("Visualization", plotOutput("ggp"),    
      tabPanel("GapMinder Data", dataTableOutput("table")),
      tabPanel("Aggregated GapMinder Data", dataTableOutput("table1"))
    )

  )
),
    fluidRow(sliderInput("slider1", label = h3("Year"), min = 1952, 
        max = 2007, value = 1952, step = 5, sep ="", width="100%"))
)

# This is UI, it has a clean look because we defined body up above. 
ui <- dashboardPage(
    dashboardHeader(title = "Module 2"),
    dashboardSidebar(
        helpText("GapMinder Data: Worldwide average life expectancy, population and GPD per Capita 1952-2007"),
        menuItem),
    body
  )

# This is server function is the same as in the layout practice. 
server <- function(input, output) {

      output$tabPanelSelected <- renderText({
        input$proj

      })

      output$ggp <- renderPlot({ 

          p <- ggplot(data=subset(gm, year==input$Year), aes(x=lifeExp)) +  geom_density(alpha=.2, fill="red") + xlab("Life Expectancy")

          q <- ggplot(data=subset(cgm,year==input$Year), aes(x=continent, y=totpop, fill=continent)) + geom_bar(stat="identity")            

          r <- ggplot(data=subset(cgm,year==input$Year), aes(x=continent, y=avggdpPercap, fill=continent)) + geom_bar(stat="identity")  
      print(p)
      print(q)
      print(r)   
  })

    output$table <- renderDataTable({gm})
    output$table1 <- renderDataTable({cgm})
}

shinyApp(ui=ui, server=server)

这是我收到的错误消息:Error Message

所以我无法弄清楚我的代码在正文中发生了什么。我认为 ui 代码没问题,我几乎可以肯定我的服务器代码不符合语法,但错误消息并不允许我对其进行解码。有任何想法吗?

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    参考您上面的图片,我修改了您的代码以获得类似的表示。

    library(shiny)
    library(shinydashboard)
    library(ggplot2)
    library(dplyr)
    library(gapminder)
    
    
    gm <- gapminder 
    gm <- transform(gm, pop = as.numeric(pop))
    
    cgm <- gm %>% group_by(continent,year) %>% summarise(totpop = sum(pop),avglifeExp = sum(pop*lifeExp)/totpop,avggdpPercap = sum(pop*gdpPercap)/totpop, numCountries=n())
    
    
    # This is UI, it has a clean look because we defined body up above. 
    ui <- dashboardPage(
      dashboardHeader(title = "Module 2"),
    
      dashboardSidebar(
        helpText("GapMinder Data: Worldwide average life expectancy, population and GPD per Capita 1952-2007")
        ),
    
      dashboardBody(
    
      #  fluidRow(
          # 
          # tabBox(
          #   title = "Population",
          #   id = "proj",      
            tabPanel("Visualization",
                     fluidRow(
                       column(4,
                              plotOutput("ggp")),
                       column(4,
                              plotOutput("ggp2")),
                       column(4,
                              plotOutput("ggp3"))
    
                     )
                     #tabPanel("GapMinder Data", dataTableOutput("table")),
                     #tabPanel("Aggregated GapMinder Data", dataTableOutput("table1"))
          #   )
          #   
          # )
        ),
        fluidRow(sliderInput("slider1", label = h3("Year"), min = 1952, 
                             max = 2007, value = 1952, step = 5, sep = "", width="100%"))
      )
    )
    
    # This is server function is the same as in the layout practice. 
    server <- function(input, output, session) {
      session$onSessionEnded(stopApp)
      # output$tabPanelSelected <- renderText({
      #   input$proj
      #   
      # })
    
      output$ggp <- renderPlot({ 
    
        ggplot(data = subset(gm, year = input$Year), aes(x = lifeExp)) +  geom_density(alpha = 0.2, fill = "red") + xlab("Life Expectancy")
      })
      output$ggp2 <- renderPlot({
        ggplot(data = subset(cgm,year = input$Year), aes(x = continent, y = totpop, fill = continent)) + geom_bar(stat = "identity")            
      })
      output$ggp3 <- renderPlot({
        ggplot(data = subset(cgm,year = input$Year), aes(x = continent, y = avggdpPercap, fill = continent)) + geom_bar(stat = "identity")  
        # print(p)
        # print(q)
        # print(r)   
      })
    
      #output$table <- renderDataTable({gm})
      #output$table1 <- renderDataTable({cgm})
    }
    
    shinyApp(ui = ui, server = server)
    

    由于对闪亮的了解很少,我觉得你没有得到任何情节的原因很少。

    1. 您没有完全实现menuItem。只需删除 menuItem 即可看到不再出现上述错误。
    2. == 是比较运算符而不是赋值运算符。所以在ggplot中用=替换==。现在您至少可以看到一个地块。
    3. 对于每个情节,您需要在UI 中单独定义它,以便您可以 稍后在 server 代码中分配该 ID 并单独定义该图。

    我假设您可以根据自己的要求改进代码。如果您卡在任何地方,我们会在这里为您提供帮助。祝你好运。

    【讨论】:

    • 非常感谢!如果我遇到更多错误,我会插话。自从这个项目以来,感谢教程,我已经走了很长一段路。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 2017-06-03
    • 2016-09-20
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多