创建第二个下拉列表作为列值并绘制面积图

时间:2019-12-20 11:11:13

标签: r shiny rstudio

我是R的新手。我正在创建一个具有日期范围的仪表板,并且对数据表的列名称有一个下拉列表,并且需要基于该列创建另一个列注释类型(即好或坏)的下拉列表第一个下拉菜单,需要将其绘制在面积图中,以使日期在x轴上,y轴是第二个下拉列表的列和颜色的度量。怎么做? 我的数据表类似于“用户名”,“评论”,“日期”,“帖子”,“情感分析”(即0和1),评论类型[即好或坏]列。 下面是我的代码

library(dplyr)
library(shiny)
library(plotly)
library(dplyr)
library(data.table)
library(shinyjs)
ui <- fluidPage(
  useShinyjs(),
  {
    tags$head(
      tags$style(
        HTML(".circle{position:relative; display:inline-block; border:1px solid black; border-radius: 50px;  width: 20%;height:0; text-align: center;margin-left: 20px;margin-right: 50px;top: 29px;}
             ")))},
  tabsetPanel({
    tabPanel("Comments by dates",
             fluidRow(column(4, dateRangeInput('dateRange', label = 'Filter comments by date', start = min(comments$dates) , end = max(comments$dates)))),
             fluidRow(column(4,selectInput("names", "Select Data", choices=c("Posts" =  "post_id","Comments" = "comment","users" = "User_name","Sentiment" = "commenttype")))),
             fluidRow(column(4,selectInput("types","select types",choices=unique(comments$commenttype)))),
             fluidRow((column(12,htmlOutput('Summary')))),
             fluidRow(column(12,plotlyOutput("Bar")))
    )
  })
        )
server <- function(input, output, session) {
  output$Summary  <- renderUI({
    comments = comments[,commenttype:=ifelse(SA==-1,'bad',ifelse(SA==0,'DontKnow','Good')),]
    comments = comments[dates >= input$dateRange[1] & dates <= input$dateRange[2]]
    Posts = length(unique(comments$post_id))
    Comments = length(unique(comments$comment))
    Users = length(unique(comments$User_name))
    HTML(paste("
               <div>
               <div class='.circle'>",Posts," </div>
               <div class='.circle'>",Comments," </div>
               <div class='.circle'>",Users,"</div>
               </div>
               "))
  })
  output$Bar <- renderPlotly({
    plotdata = comments
    plotdata
    plotdata$Metric <- plotdata %>% pull(input$names)
    plotdata$Metric
    plotdata=plotdata[,metric:=list(length(unique(Metric))),by=.(dates)]
    plotdata

    p <- plot_ly(x = ~plotdata$dates, y = ~plotdata$metric, type = 'scatter', mode = 'lines', fill = 'tozeroy') %>%
      layout(xaxis = list(title = 'dates',showgrid = FALSE),
             yaxis = list(title = 'Metric',showgrid = FALSE))
    p
    })
}


shinyApp(ui, server)

我需要做的是为注释类型列值创建另一个下拉列表,当我单击其他列名称(如注释)时,用户需要获取该特定列的面积图,注释类型值必须在x轴上显示为面积图日期和y轴作为列的计数,颜色由commenttype值表示。你能帮我吗?

0 个答案:

没有答案