2016-10-01 1 views
0

私はRとシャイニーの初心者で、少し問題があります。mainPanelに複数のrenderTextを挿入するにはどうすればよいですか?

ご協力いただければ幸いです。

たとえば、Rデータフレーム "mtcars"に変数Groupを文字形式で追加しました。今、私は3台の車を持っています:それぞれの車の「mpg」レベルに応じて、1、2、3です。

チェックしたグループの車の数を表示する1つのrenderText、チェックしたグループの4つの車の台数を示す2つ目の番号、3番目の番号6気筒の車の数を表示する4番目の車、

私はグループ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")))) 

server.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})})})})}) 

助けのために非常にありがとうございました、そして私の英語のため申し訳ありません。

答えて

0

おそらくあなたと異なるグループを作成しました。 、それはそれは私が取得するtriyngたまさにですので、素晴らしい

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")))) 

server.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)) 
      }) 
      }) 
+0

こんにちはバルター:これは、1つの可能なソリューションです!私はまだ問題が何かを理解していない場合でも、私は非常に満足しています。どうもありがとうございました。良い一日を。 –

+0

もう一度質問してください。私がプログラムを実行すると、チェックボックスをすべてオンにするにはどうすればいいですか? 'selected = "Group 1" "Group 2" "Group 3"'を試しましたが、エラーメッセージが表示されます。ERROR:引数が論理的であると解釈できません。どうもありがとうございます。 –

+0

バター、あなたは王様です!おかげさまで大変ありがとうございました。 –

関連する問題