如何使用 c3 库进行闪亮?如何创建一个简单的仪表图表?

How to use c3 library for shiny? How to create a simple gauge chart?

我应该如何使用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 库几乎一样吗?

您需要在服务器中使用renderC3而不是renderPlot,并且在ui中使用c3Output而不是plotOutput

shinydashboardshiny的区别主要在于UI的外观(ui的脚本也会不同),背后的逻辑是一样的。

如果想试一试,我写了一个非常类似于c3billboarder的包,你也可以做仪表盘,你可以用:

安装
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)