在Shiny R中显示雷达图

时间:2016-10-25 13:59:46

标签: r shiny rstudio shiny-server

在我闪亮的应用程序的server.R上我基本上有这个: 图书馆fmsb, 数据集和雷达图, 我可以在R studio的情节查看器中查看图表,但如何在UI.R中显示图表的显示? 感谢您的帮助。

library(fmsb)

# Create data: note in High school for several students
set.seed(99)
data=as.data.frame(matrix( sample( 0:20 , 15 , replace=F) , ncol=5))
colnames(data)=c("math" , "english" , "biology" , "music" , "R-coding" )
rownames(data)=paste("mister" , letters[1:3] , sep="-")

# To use the fmsb package, I have to add 2 lines to the dataframe: the max and min of each topic to show on the plot!
data=rbind(rep(20,5) , rep(0,5) , data)

colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) ,     rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data  , axistype=1 , 
#custom polygon
pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
#custom the grid
cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,20,5), cglwd=0.8,
#custom labels
vlcex=0.8 
)
legend(x=0.7, y=1, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)

我是R和Shiny的新手所以我不确定在最后一个框中使用什么:

library(shiny)
library(shinydashboard)
library(fmsb)
#for the geoschool selection screen
df_geo_13_14 <- data.frame(geometry.annual.report.december.2014.may.2015.clean)
geo_school <- df_geo_13_14$School.Name
geo_list <- as.list(geo_school)
#end selection screen

dashboardPage(
dashboardHeader(title = "Working on it"),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)

fluidRow(
  box(  
    selectInput("School","Please select the schools you want to compare",
                    choices = c("Elementary", "Middle & High"))),

  box(
    title = "Controls",
    sliderInput("schoolSize", "Please filter the schools based upon student population:",
                min=2, max = 800, value = c(100, 200), step = 20)
  )
),
fluidRow(
  box(
    selectizeInput("geo_13_14", "Select the school, sos my wording:", choices = geo_school )
  ),
  box(
    ??????????How do I display the radar chart?????????
      )

)
)
)

1 个答案:

答案 0 :(得分:3)

我认为您没有提供使用户输入部分工作所需的数据,所以我已经评论过了。

我认为你只需要

  • 将雷达绘图代码放在shinyServer内的renderPlot中 部分
  • 将plotOutput添加到ui部分

这对我有用:

library(shiny)
library(shinydashboard)
library(fmsb)

ui <- shinyUI(dashboardPage(
  dashboardHeader(title = "Working on it"),
  dashboardSidebar(),
  dashboardBody(# Boxes need to be put in a row (or column)

    # fluidRow(
    #   box(
    #     selectInput(
    #       "School",
    #       "Please select the schools you want to compare",
    #       choices = c("Elementary", "Middle & High")
    #     )
    #   ),
    #
    #   box(
    #     title = "Controls",
    #     sliderInput(
    #       "schoolSize",
    #       "Please filter the schools based upon student population:",
    #       min = 2,
    #       max = 800,
    #       value = c(100, 200),
    #       step = 20
    #     )
    #   )
    # ),
    fluidRow(# box(
      #   selectizeInput("geo_13_14",
      #                  "Select the school, sos my wording:",
      #                  choices = geo_school)
      # ),
      box(
        plotOutput('radarPlot')
      )))
))

server <- shinyServer(function(input, output) {
  output$radarPlot <- renderPlot({
    # Create data: note in High school for several students
    set.seed(99)
    data = as.data.frame(matrix(sample(0:20 , 15 , replace = F) , ncol = 5))
    colnames(data) = c("math" , "english" , "biology" , "music" , "R-coding")
    rownames(data) = paste("mister" , letters[1:3] , sep = "-")

    # To use the fmsb package, I have to add 2 lines to the dataframe: the max and min of each topic to show on the plot!
    data = rbind(rep(20, 5) , rep(0, 5) , data)

    colors_border = c(rgb(0.2, 0.5, 0.5, 0.9),
                      rgb(0.8, 0.2, 0.5, 0.9) ,
                      rgb(0.7, 0.5, 0.1, 0.9))
    colors_in = c(rgb(0.2, 0.5, 0.5, 0.4),
                  rgb(0.8, 0.2, 0.5, 0.4) ,
                  rgb(0.7, 0.5, 0.1, 0.4))
    radarchart(
      data  ,
      axistype = 1 ,
      #custom polygon
      pcol = colors_border ,
      pfcol = colors_in ,
      plwd = 4 ,
      plty = 1,
      #custom the grid
      cglcol = "grey",
      cglty = 1,
      axislabcol = "grey",
      caxislabels = seq(0, 20, 5),
      cglwd = 0.8,
      #custom labels
      vlcex = 0.8
    )
    legend(
      x = 0.7,
      y = 1,
      legend = rownames(data[-c(1, 2), ]),
      bty = "n",
      pch = 20 ,
      col = colors_in ,
      text.col = "grey",
      cex = 1.2,
      pt.cex = 3
    )
  })
})

# Run the application
shinyApp(ui = ui, server = server)