2017-08-19 9 views
0

どのように私はc3ライブラリを使用する必要がありますか?私はすでにそれをインストールして、簡単なゲージチャートを表示しようとしています。以下は光沢のあるc3ライブラリの使い方は?単純なゲージチャートを作成するには?

ui.Rserver.Rのための私のコードです:

server.R

library(shiny) 
library(c3) 
shinyServer(function(input, output) { 


observeEvent(input$button, { 

output$name <- renderText({ 
paste("Welcome ",input$name,". This is your Assesment result!")}) 
output$nl <- renderText({input$nl}) 

whrs <- input$whrs 

if(whrs == 10) { 
    point <- 20 
} else if (whrs == 8 || whrs == 9) { 
    point <- 18 
} else if (whrs == 7 || whrs == 6) { 
    point <- 12 
} else if(whrs == 5) { 
    point <- 8 
} else { 

point <- 0 
} 

pwhrs <- point 
nl <- input$nl 

if(nl == 10) { 
    point <- 20 
} else if (nl == 8 || whrs == 9) { 
    point <- 18 
} else if (nl == 7 || whrs == 6) { 
    point <- 12 
} else if(nl == 5) { 
    point <- 8 
} else { 

point <- 0 
} 
pnl <- point 


output$pwhrs <- renderText({pwhrs}) 
output$pnl <- renderText({pnl}) 

output$plot <- renderPlot({ 
gauge.chart <- data.frame(red=20,green=45,blue=10) %>% 
    c3() %>% 
c3_gauge(title= 'Colours') 

}) 
}) 

}) 

と、ユーザー入力に基づいて、単純なゲージチャートを生成するために行われなければならない何私のui.R

library(shiny) 

shinyUI(fluidPage(
    hr(), 
titlePanel("Summary"), 
hr(), 
sidebarLayout(
sidebarPanel(
    textInput("name","Enter your name",""), 
numericInput("whrs","How many hours do you work daily?","0",min=0,max=10), 
numericInput("nl","How many leaves do you take per month?","0",min= 0,max=20), 
numericInput("bhr","How many hours of break do you take daily?","0",min= 0,max=10), 
selectInput("wrkdlvr","How often you complete your works?",choices=c("On time","Before Deadline","Delayed")), 
selectInput("consleave","How often you leaves are consecutive?",choices=c("Never","Always","Not Applicable")), 
actionButton("button", "Calculate") 
), 
    mainPanel(("About this tool"), 
    p(helpText("Try this tool to assess yourself. 
    You need to work for a minimum of 9 hrs per day.")), 
helpText("This tool will ask for some parameters and based on the input you will 
    be assessed"), 
hr(), 
    textOutput("name"), 
textOutput("pwhrs"), 
textOutput("pnl"), 
plotOutput("plot"), 
    hr() 
) 
), 
hr() 
)) 

?グラフの最大値は100で、ユーザー入力に基づいて表示される値が設定されます。これを達成する方法は?

ui.Rserver.Rの両方にc3ライブラリを追加する必要がありますか? (両方のファイルに追加のライブラリを追加する必要があるかどうか教えてください)

shinyshinydashboardライブラリはほとんど同じですか?

答えて

2

サーバーにrenderPlotの代わりにrenderC3を使用し、uiにplotOutputの代わりにc3Outputを使用する必要があります。

shinydashboardは、主にUIの外観(UIスクリプトも異なります)がshinyと異なります。その背後にあるロジックは同じです。

それを試してみることにしたい場合にも、チャートを測る行うことができ、私はbillboarderc3と非常によく似たパッケージを書いて、あなたがそれをインストールすることができます:

devtools::install_github("dreamRs/billboarder") 

そして、あなたの例と、それは(与えますが私は)それが良い練習だ、オブザーバーの外に出力を入れて、ポイント数を格納するのにreactiveValuesを使用します。

library("shiny") 
library("billboarder") 

ui <- fluidPage(
    fluidPage(
    hr(), 
    titlePanel("Summary"), 
    hr(), 
    sidebarLayout(
     sidebarPanel(
     textInput("name","Enter your name",""), 
     numericInput("whrs","How many hours do you work daily?","0", min = 0, max = 10), 
     numericInput("nl","How many leaves do you take per month?","0", min = 0, max = 20), 
     numericInput("bhr","How many hours of break do you take daily?","0", min = 0,max = 10), 
     selectInput("wrkdlvr","How often you complete your works?", choices = c("On time", "Before Deadline", "Delayed")), 
     selectInput("consleave", "How often you leaves are consecutive?", choices = c("Never", "Always", "Not Applicable")), 
     actionButton("button", "Calculate") 
    ), 
     mainPanel(
     "About this tool", 
     p(helpText("Try this tool to assess yourself.", 
        "You need to work for a minimum of 9 hrs per day.")), 
     helpText("This tool will ask for some parameters and based on the input you will", 
       "be assessed"), 
     hr(), 
     textOutput(outputId = "name"), 
     textOutput(outputId = "pwhrs"), 
     textOutput(outputId = "pnl"), 
     billboarderOutput(outputId = "gauge"), 
     hr() 
    ) 
    ), 
    hr() 
) 
) 

server <- function(input, output, session) { 

    output$name <- renderText({ 
    paste0("Welcome ", input$name, ". This is your Assesment result!") 
    }) 
    output$nl <- renderText({input$nl}) 

    points <- reactiveValues(pwhrs = 0, pnl = 0) 

    observeEvent(input$button, { 
    whrs <- input$whrs 

    if(whrs == 10) { 
     point <- 20 
    } else if (whrs == 8 | whrs == 9) { 
     point <- 18 
    } else if (whrs == 7 | whrs == 6) { 
     point <- 12 
    } else if(whrs == 5) { 
     point <- 8 
    } else { 

     point <- 0 
    } 

    points$pwhrs <- point 
    nl <- input$nl 

    if(nl == 10) { 
     point <- 20 
    } else if (nl == 8 | nl == 9) { 
     point <- 18 
    } else if (nl == 7 | nl == 6) { 
     point <- 12 
    } else if(nl == 5) { 
     point <- 8 
    } else { 
     point <- 0 
    } 
    points$pnl <- point 
    }) 

    output$pwhrs <- renderText({points$pwhrs}) 
    output$pnl <- renderText({points$pnl}) 


    output$gauge <- renderBillboarder({ 
    billboarder() %>% 
     bb_gaugechart(value = points$pnl + points$pwhrs) 
    }) 

} 

shinyApp(ui = ui, server = server) 
+0

ニースパッケージ 'uが作成billboarder'、私はそれについて聞くのは初めて、私は間違いなくなります私のレパートリーに追加してください。 'rmarkdown'で作成されたもののような静的HTMLを使ったリアルタイムレンダリングの例がありますか? –

+0

ありがとう!それは聞いていいです!パッケージは最近ですが、私は2ヶ月前に始めました。私は今週いくつかのテストを行い、9月上旬にCRANに行く予定です。私はRpubsの例をアップロードしました:http://rpubs.com/Victorp/billboarder_examples、もしあなたがそれを使用して私に教えてください! – Victorp

関連する問題