2017-11-11 27 views
0

我想在下面的示例中使用chartJSRadar()而不是webplot()。可能吗?我不知道webplot()的功能,但我需要在这个地方使用雷达图表。该功能的使用从33行开始。该代码也可以在这里找到:https://gist.github.com/mbannert/9124890/如何使用ChartJS雷达绘制闪亮

data_sets <- c("mtcars") 

shinyServer(function(input, output) { 

    # Drop-down selection box for which data set 
    output$choose_dataset <- renderUI({ 
    selectInput("dataset", "Data set", as.list(data_sets)) 
    }) 

    # select a car 
    output$choose_car <- renderUI({ 
    selectInput("car","car",as.list(rownames(get(input$dataset)))) 
    }) 


    # Check boxes 
    output$choose_columns <- renderUI({ 
    # If missing input, return to avoid error later in function 
    if(is.null(input$dataset)) 
     return() 

    # Get the data set with the appropriate name 
    dat <- get(input$dataset) 
    colnames <- names(dat) 

    # Create the checkboxes and select them all by default 
    checkboxGroupInput("columns", "Choose columns", 
         choices = colnames, 
         selected = colnames) 
    }) 


    output$radar <- renderPlot({ 
    source("radar.R") 
    webplot(get(input$dataset), 
      which(rownames(mtcars) == input$car), y.cols = input$columns,add=F) 
    }) 

    # Output the data 
    output$data_table <- renderTable({ 
    # If missing input, return to avoid error later in function 
    if(is.null(input$dataset)) 
     return() 

    # Get the data set 
    dat <- get(input$dataset) 

    # Make sure columns are correct for data set (when data set changes, the 
    # columns will initially be for the previous data set) 
    if (is.null(input$columns) || !(input$columns %in% names(dat))) 
     return() 

    # Keep the selected columns 
    dat <- dat[, input$columns, drop = FALSE] 

    # Return first 20 rows 
    head(dat, 20) 
    }) 
}) 
shinyUI(pageWithSidebar(

    headerPanel("Car Comparison Radar"), 

    sidebarPanel(
    uiOutput("choose_dataset"), 
    uiOutput("choose_car"), 

    uiOutput("choose_columns"), 
    br(), 
    a(href = "http://statisticstoproveanything.blogspot.de/2013/11/spider-web-plots-in-r.html", 
     "Radar by Alan Vaughn from statisticstoproveanything"), 
    br(), 
    a(href = "https://gist.github.com/mbannert/9124890/", 
     "Find the shiny code gist here.") 

), 


    mainPanel(

    plotOutput(outputId = "radar", height = "600px"), 
    tableOutput("data_table")  
) 
)) 

回答

0

还有一些其他的警告,该应用程序是扔,我不碰,但这个工程:

library(shiny) 
library(chartjs) 

data_sets <- c("mtcars") 

shinyServer(function(input, output) { 

    # Drop-down selection box for which data set 
    output$choose_dataset <- renderUI({ 
    selectInput("dataset", "Data set", as.list(data_sets)) 
    }) 

    # select a car 
    output$choose_car <- renderUI({ 
    selectInput("car","car",as.list(rownames(get(input$dataset)))) 
    }) 


    # Check boxes 
    output$choose_columns <- renderUI({ 
    # If missing input, return to avoid error later in function 
    if(is.null(input$dataset)) 
     return() 

    # Get the data set with the appropriate name 
    dat <- get(input$dataset) 
    colnames <- names(dat) 

    # Create the checkboxes and select them all by default 
    checkboxGroupInput("columns", "Choose columns", 
         choices = colnames, 
         selected = colnames) 
    }) 


    output$radar <- renderChartjs({ 

    # Get the data set 
    dat <- get(input$dataset) 

    # Make sure columns are correct for data set (when data set changes, the 
    # columns will initially be for the previous data set) 
    if (is.null(input$columns) || !(input$columns %in% names(dat))) 
     return() 

    # Keep the selected columns 
    dat <- dat[, input$columns, drop = FALSE] 

    #row data for plot 
    car <- as.vector(t(dat[row.names(dat) == input$car,])) 

    chartjs() %>% 
     cjsRadar(labels = colnames(dat)) %>% 
     cjsSeries(data = car) %>% 
     cjsEditScale(axis = NULL, ticks = list(beginAtZero = TRUE)) 

    }) 

    # Output the data 
    output$data_table <- renderTable({ 
    # If missing input, return to avoid error later in function 
    if(is.null(input$dataset)) 
     return() 

    # Get the data set 
    dat <- get(input$dataset) 

    # Make sure columns are correct for data set (when data set changes, the 
    # columns will initially be for the previous data set) 
    if (is.null(input$columns) || !(input$columns %in% names(dat))) 
     return() 

    # Keep the selected columns 
    dat <- dat[, input$columns, drop = FALSE] 

    # Return first 20 rows 
    head(dat, 20) 
    }) 
}) 

shinyUI(pageWithSidebar(

    headerPanel("Car Comparison Radar"), 

    sidebarPanel(
    uiOutput("choose_dataset"), 
    uiOutput("choose_car"), 

    uiOutput("choose_columns"), 
    br(), 
    a(href = "http://statisticstoproveanything.blogspot.de/2013/11/spider-web-plots-in-r.html", 
     "Radar by Alan Vaughn from statisticstoproveanything"), 
    br(), 
    a(href = "https://gist.github.com/mbannert/9124890/", 
     "Find the shiny code gist here.") 

), 


    mainPanel(

    chartjsOutput(outputId = "radar", height = '75px'), 
    tableOutput("data_table")  
) 
)) 
+0

不幸的是,chartjs包不是最新的,在我的工作中我不得不使用radarchart包,你能改写它吗? – Kim

+0

我在下面添加了一个使用radarchart软件包的新答案。我会坚持使用chartjs - 即使文档尚未更新。它看起来好多了。出于这个原因,我会留下我的两个答案。 – IanK

0

OK,你可以这样做这种方式如果你真的想坚持与雷达图库:

library(shiny) 
library(radarchart) 

data_sets <- c("mtcars") 

shinyServer(function(input, output) { 

    # Drop-down selection box for which data set 
    output$choose_dataset <- renderUI({ 
    selectInput("dataset", "Data set", as.list(data_sets)) 
    }) 

    # select a car 
    output$choose_car <- renderUI({ 
    selectInput("car","car",as.list(rownames(get(input$dataset)))) 
    }) 


    # Check boxes 
    output$choose_columns <- renderUI({ 
    # If missing input, return to avoid error later in function 
    if(is.null(input$dataset)) 
     return() 

    # Get the data set with the appropriate name 
    dat <- get(input$dataset) 
    colnames <- names(dat) 

    # Create the checkboxes and select them all by default 
    checkboxGroupInput("columns", "Choose columns", 
         choices = colnames, 
         selected = colnames) 
    }) 


    output$radar <- renderChartJSRadar({ 

    # Get the data set 
    dat <- get(input$dataset) 

    # Make sure columns are correct for data set (when data set changes, the 
    # columns will initially be for the previous data set) 
    if (is.null(input$columns) || !(input$columns %in% names(dat))) 
     return() 

    # Keep the selected columns 
    dat <- dat[, input$columns, drop = FALSE] 

    #reform data for plot 
    dat <- as.data.frame(t(dat), stringsAsFactors = FALSE) 
    dat$labs <- row.names(dat) 

    dat <- dat[, c('labs', input$car)] 

    chartJSRadar(dat) 

    }) 

    # Output the data 
    output$data_table <- renderTable({ 
    # If missing input, return to avoid error later in function 
    if(is.null(input$dataset)) 
     return() 

    # Get the data set 
    dat <- get(input$dataset) 

    # Make sure columns are correct for data set (when data set changes, the 
    # columns will initially be for the previous data set) 
    if (is.null(input$columns) || !(input$columns %in% names(dat))) 
     return() 

    # Keep the selected columns 
    dat <- dat[, input$columns, drop = FALSE] 

    # Return first 20 rows 
    head(dat, 20) 
    }) 
}) 

shinyUI(pageWithSidebar(

    headerPanel("Car Comparison Radar"), 

    sidebarPanel(
    uiOutput("choose_dataset"), 
    uiOutput("choose_car"), 

    uiOutput("choose_columns"), 
    br(), 
    a(href = "http://statisticstoproveanything.blogspot.de/2013/11/spider-web-plots-in-r.html", 
     "Radar by Alan Vaughn from statisticstoproveanything"), 
    br(), 
    a(href = "https://gist.github.com/mbannert/9124890/", 
     "Find the shiny code gist here.") 

), 


    mainPanel(
    chartJSRadarOutput('radar', height = '350px'), 
    #chartjsOutput(outputId = "radar", height = '75px'), 
    tableOutput("data_table")  
) 
))