【问题标题】:How to put several renderText in a mainPanel?如何将几个renderText放在一个mainPanel中?
【发布时间】:2017-02-09 15:53:53
【问题描述】:

我是 R 和 Shiny 的初学者,但我有一个小问题。

如果您能帮忙,我将不胜感激。

例如,我已将变量 Group 添加到字符格式的 R 数据框“mtcars”中。现在我有 3 组汽车:1、2 和 3,这取决于每辆车的“mpg”级别。

我想要一个显示我检查过的组的汽车数量的渲染文本,第二个显示检查组的 4 缸汽车的数量,第三个显示检查组的数量6缸汽车,第四个显示6缸汽车的数量。

如果我只保留 Group textRender,效果很好,但是当我添加其他 3 个时,出现错误,我找不到原因。

这是代码:

ui.R

library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Example"),
sidebarPanel(checkboxGroupInput("dynamic", "Groups", label = "Groups",
           choices = c("Group 1"="1","Group 2"="2","Group 3" = "3"))),

mainPanel(("Group : "), textOutput("textDisplay"),
        ("4 cylinders : "), textOutput("text4cyl"),
        ("6 cylinders : "), textOutput("text6cyl"),
        ("8 cylinders : "), textOutput("text8cyl"))))

服务器.R

library(shiny)
shinyServer(function(input, output) {
  output$textDisplay <- renderText({
    a<-sum(mtcars$Group %in% input$dynamic)
  output$text4cyl <- renderText({
    b<-sum(mtcars$Group %in% input$dynamic) & mtcars$cyl ==4
  output$text6cyl <- renderText({
    c<-sum(mtcars$Group %in% input$dynamic) & mtcars$cyl ==6
  output$text8cyl <- renderText({
    d<-sum(mtcars$Group %in% input$dynamic) & mtcars$cyl ==8})})})})})

非常感谢你的帮助,对不起我的英语。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我刚刚创建了可能与您的不同的分组。这是一种可能的解决方案:

    ui.R

        library(shiny)
        shinyUI(pageWithSidebar(
                headerPanel("Example"),
                sidebarPanel(checkboxGroupInput("dynamic", label = "Groups",
                                                choices = list("Group 1"= "1","Group 2" = "2","Group 3" = "3"),
                                                selected = c(1,2,3))),
    
                mainPanel(("Group : "), textOutput("textDisplay"),
                          ("4 cylinders : "), textOutput("text4cyl"),
                          ("6 cylinders : "), textOutput("text6cyl"),
                          ("8 cylinders : "), textOutput("text8cyl"))))
    

    服务器.R

        library(shiny)
        library(dplyr)
    
        group <- c(rep("1", 11), rep("2", 11), rep("3", 10))
        group
        mtcars$Group<-factor(group)
    
        shinyServer(function(input, output) {
                selection <- reactive({
                        filter(mtcars, Group %in% input$dynamic)
                })
                output$textDisplay <- renderText({
                        NROW(selection())
                })
                output$text4cyl <- renderText({
                        tmp <- selection()
                        NROW(filter(tmp, cyl == 4))
                })
                output$text6cyl <- renderText({
                        tmp <- selection()
                        NROW(filter(tmp, cyl == 6))
                })
                output$text8cyl <- renderText({
                        tmp <- selection()
                        NROW(filter(tmp, cyl == 8))
                })
                })
    

    【讨论】:

    • 你好 Valter,太棒了,这正是我想要得到的!即使我还不明白问题出在哪里,我也很高兴。非常感谢。祝你有美好的一天。
    • 拜托,再问一个问题 Valter,我怎样才能在运行 prog 时检查所有复选框?我尝试使用'selected =“Group 1”“Group 2”“Group 3”',但收到一条错误消息:错误:参数不可解释为逻辑。非常感谢。
    • 瓦尔特,你是国王!非常感谢 祝你有美好的一天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多