$ operator is invalid for atomic vectors

output$plott6 <- renderPlotly({
 d<-event_data('plotly_click',source = 'linkhere')
 if(is.null(d)==F){

qrydd=paste("exec databasename..[sp name] ",d$key,",'','','','','0','2','15','','01/01/2017','31/12/2017'")
    qrydd
    dtd <- sqlQuery(dbcnd,qrydd)
    dtd <- data.frame(dtd)
    dtd
    closeAllConnections()
    layout <- list(
      font = list(size = 12),
      title = "",
      xaxis = list(title = "district"),
      yaxis = list(title = "records",automargin = TRUE)
    )

    p <- plot_ly(dtd, colors = COL, marker = list(color = COL),key = ~paste(dtd$DID, sep = "")) %>%
     add_trace(dtd,x = dtd$DistrictName, y = dtd$TotState,name = " ",type = 'bar') %>%
     layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)}
 else
 {
   return(NULL)
 }

  })

it is giving an error : $ operator is invalid for atomic vectors

could anyone suggest any solution to get it done

Hi,

The error you get is because somewhere you're trying to access something like a data frame while it isn't

data = c(1:5)
data$number
Error in data$number : $ operator is invalid for atomic vectors

Could you please again create a reprex. We need to be able to reproduce the error in order to be able to solve the issue.

Let me remind you that we don't need the full app, just a slimmed down version of the app that runs only the plot of interest. Also make sure that you provide a data frame instead of the database lookup, we won't have access to it.

PJ

I know how this works theoretically and knew about the answer you have given but I am unable to implement it

output$plott5 <- renderPlotly({
    
    qrydd=paste("exec database_name..[sp] '','','','','0','0','2','0','','01/01/2017','31/12/2017'")
    qrydd
    dtd <- sqlQuery(dbcnd,qrydd)
    dtd <- data.frame(dtd)
    dtd
    closeAllConnections()
    tablename=rowSums(cbind(dtd$TotFormFRaj,dtd$TotState),na.rm=TRUE)
    
    layout <- list(
      font = list(size = 12),
      title = "",
      xaxis = list(title = ""),
      yaxis = list(title ="" ,automargin = TRUE)
    )
    
    p <- plot_ly(dtd, colors = COL, marker = list(color = COL),source = 'linkhere',orientation='h') %>%
      add_trace( x =dtd$TotState,y = dtd$StateName,name = "states",type = 'bar') %>%
      layout( font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
    
    
  })

as soon as the chart(code above given) is clicked, it must render or plot the chart like ...

output$plott6 <- renderPlotly({
    d<-event_data('plotly_click',source = 'linkhere')
    if(is.null(d)==F){
      
      qrydd=paste("exec databasename..[sp_name] ",d$key,",'','','','','0','2','15','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      layout <- list(
        font = list(size = 12),
        title = "",
        xaxis = list(title = "district"),
        yaxis = list(title = "records",automargin = TRUE)
      )
      
      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),key=~paste(dtd$DID,sep = ""),source = "link10") %>%
        add_trace(x = dtd$DistrictName, y = dtd$TotState,name = "",type = 'bar') %>%
        layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)}
    else
    {
      return(NULL)
    }
    
  })

but it throws the error that i have mentioned earlier '$ not valid for atomic vector',all i need to know ,the way i am providing the key is valid or not,if it isn't then how come is it working for another scenario that i have mentioned earlier on ,not for this case,i have tried most of the things given on web like using
object["element"] and some other way of writing key like key=dtd$value and proving it without $ operator like ",key,"

Hi.

You still did not create a reprex... :slight_smile: I can't recreate the error without, so I'm not sure how to help.

These are my thoughts:

  • As far as I know, there is no key argument for the event_data function. Meaning your d$key will return NULL, resulting in issues downsteam. The arguments that are returned are curveNumber, pointNumber, x, and y. Use a print(d) in your code to see what it returns and what it is you need.
  • It might also be something with the database query. Check again the output by using a print statement. Example: print(head(qrydd))
  • In plot_ly, I don't know what the key argument means as I can't find it anywhere in the documentation.

Good luck
PJ

1 Like

dummy data for the given scenario will be respectively as per the stored procedure

stored procedure 1 or chart 1:

DID DistrictName BlockName CenterName TotRaj TotOtherState StateCode
76 state null null 7656454 5645345 null

stored procedure 2 or chart 2:

DID DistrictName BlockName CenterName TotRaj TotOtherState StateCode
101 a null null 564534 564534 null
201 b null null 675645 765645 null
301 c null null 765645 786756 null
401 d null null 987656 764534 null
501 e null null 675645 543423 null
601 f null null 765434 231234 null
701 g null null 564534 763423 null
801 h null null 234565 567876 null
901 i null null 985646 876345 null
102 j null null 876754 453675 null
202 k null null 876756 876754 null

stored procedure 3 or chart 3:

DID DistrictName BlockName CenterName TotRaj TotOtherState StateCode CID
101 a aaa null 564534 564534 null nul
101 a bbb null 675645 765645 null nul
101 a ccc null 765645 786756 null nul
101 a ddd null 987656 764534 null nul
101 a eee null 675645 543423 null nul
101 a fff null 765434 231234 null nul
101 a ggg null 564534 763423 null nul
101 a hhh null 234565 567876 null nul
101 a iii null 985646 876345 null nul
101 a jjj null 876754 453675 null nul
101 a kkk null 876756 876754 null nul

stored procedure 4 or chart 4:

DID DistrictName BlockName CenterName TotRaj TotOtherState StateCode CID
101 a aaa abc 2345 4532 null c01
101 a aaa bac 2343 2345 null c02
101 a aaa dac 6754 3456 null c03
101 a aaa efc 7656 4567 null c04
101 a aaa ghc 8767 9845 null c05
101 a aaa hic 6756 9876 null c06
101 a aaa jkl 4534 6756 null c07
101 a aaa mnl 5678 5645 null c08
101 a aaa lkj 6756 4534 null c09
101 a aaa ghj 5434 5645 null c10
101 a aaa kjh 9876 7865 null c11

stored procedure 5 or chart 5:

DID DistrictName BlockName CenterName TotRaj TotOtherState StateCode CID StateName
null null null nul nul 4532 1 null alpha
null null null nul nul 2345 2 null beta
null null null nul nul 3456 3 null gama
null null null nul nul 4567 4 null theta
null null null nul nul 9845 5 null abn
null null null nul nul 9876 6 null mnb
null null null nul nul 6756 7 null vbg
null null null nul nul 5645 8 null hjg
null null null nul nul 4534 9 null klj
null null null nul nul 5645 10 null ghj
null null null nul nul 7865 11 null jhg

stored procedure 6 or chart 6:

DID DistrictName BlockName CenterName TotRaj TotOtherState StateCode CID StateName
901 fdd null nul nul 4532 1 null alpha
801 ghf null nul nul 2345 2 null alpha
702 jkh null nul nul 3456 3 null alpha
502 hgf null nul nul 4567 4 null alpha
567 nmb null nul nul 9845 5 null alpha
876 nlm null nul nul 9876 6 null alpha
567 lkj null nul nul 6756 7 null alpha
987 hjg null nul nul 5645 8 null alpha
678 klj null nul nul 4534 9 null alpha
789 jkh null nul nul 5645 10 null alpha
654 hyl null nul nul 7865 11 null alpha

the flow of the diagram would be like...

chart 1 is the pie chart that you have given me code for,as soon as the slicing part is clicked it renders or plot
the second bar chart and when we click on other states it renders bar chart no 5 given in the scenario.
when we click on bar chart of totraj part it must render the bar chart 3 as per the id and then bar chart 4 as per the DID & CID passed as an argument to the stored procedure to ms SQL server,that the part where I was using key as a dynamic argument to sp to get the data as per the id which is unique to each district and so on,
all the scenario that I have mentioned above have been achieved, I just gave you rough idea to get an idea.

what I am trying to achieve is with graph 5 to render the graph 6 , this is the same method that I applied for the above-given scenario but it is not working out for this case, tell me where is am going wrong and what to do to improve it or is there any other method to achieve the same.

hope you would get what i am tryna explain here and i am kinda new to this field if anything is wrong ,you can guide me through

here's the reprex for this scenario,but still you require database to get your hands on it as i am using stored procedure from my local host (ms sql server),but i hope you will work it out somehow


library(RODBC)
library(RODBCext)
#> 
#> Attaching package: 'RODBCext'
#> The following objects are masked from 'package:RODBC':
#> 
#>     odbcFetchRows, sqlFetchMore
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box
library(tidyverse)
library(readr)
library(shiny)
library(reprex)

COL = c("#9B59B6","#58D68D","#F4D03F","#909497","#D35400","#5D6D7E","#C39BD3","#5499C7","#CD6155","#BDC3C7","#808B96","#58D68D","#D7BDE2","#A93226","#B3B6B7","#138D75","#34495E","#F1948A","#D7BDE2" ,"#D35400", "#CD6155", "#5D6D7E", "#C39BD3" ,"#138D75" ,"#808B96", "#A93226" ,"#909497", "#B3B6B7" ,"#34495E" ,"#5499C7", "#9B59B6", "#58D68D" ,"#F1948A" ,"#F4D03F", "#BDC3C7", "#58D68D")
dbcnd <- odbcDriverConnect('Driver={SQL Server};Server=20.342.12.123;Database=;Uid=dan;Pwd=566$abc')
#> Warning in odbcDriverConnect("Driver={SQL
#> Server};Server=20.342.12.123;Database=;Uid=dan;Pwd=566$abc"): [RODBC]
#> ERROR: state 08001, code 17, message [Microsoft][ODBC SQL Server Driver]
#> [DBNETLIB]SQL Server does not exist or access denied.
#> Warning in odbcDriverConnect("Driver={SQL
#> Server};Server=20.342.12.123;Database=;Uid=dan;Pwd=566$abc"): [RODBC]
#> ERROR: state 01000, code 53, message [Microsoft][ODBC SQL Server Driver]
#> [DBNETLIB]ConnectionOpen (Connect()).
#> Warning in odbcDriverConnect("Driver={SQL
#> Server};Server=20.342.12.123;Database=;Uid=dan;Pwd=566$abc"): ODBC
#> connection failed

ui<-dashboardPage(
  dashboardHeader(title = "dashboard",titleWidth = 300),
  
  dashboardSidebar(
    sidebarMenuOutput("menu"),
    selectInput(inputId = "category",
                label = "STATE", 
                choices = c("RAJ","OTHER_STATES")),  
    selectInput(inputId = "category1", 
                label = "DISTRICT", choices = c("ANISTEN","BIAN")),
    selectInput(inputId = "category2", 
                label = "CENTER_NAME", choices = c("ASTER","CLANE","JABELIN"))
  ),
  
  dashboardBody(
    
    fluidRow(plotlyOutput("plott1")),
    fluidRow(plotlyOutput("plott2")),
    fluidRow(plotlyOutput("plott3")),
    fluidRow(plotlyOutput("plott4")),
    fluidRow(plotlyOutput("plott5")),
    fluidRow(plotlyOutput("plott6")),
    fluidRow(plotlyOutput("plott7")),
    verbatimTextOutput("check")
    
  ))

server<-function(input,output){
  output$plott1 <- renderPlotly({
    
    qrydd=paste("exec database_map..[spformother]'','99','','','0','01/01/2017','31/12/2017'")
    qrydd
    dtd <- sqlQuery(dbcnd,qrydd)
    dtd <- data.frame(dtd)
    dtd
    ds <- data.frame(labels = c("Raj","Other_State"),
                     values = c(dtd$TotRaj,dtd$TotOtherState)
    )
    closeAllConnections()
    
    plot_ly(ds, labels = ~labels, values = ~values,type = 'pie',source ='listenhere1',
            textposition = 'inside',
            textinfo = 'label+percent',
            insidetextfont = list(color = '#FFFFFF'),
            marker = list(colors = colors,
                          line = list(color =colors , width = 1)),showlegend = FALSE) %>% 
      layout(title = 'state wise',
             xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
             yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
  }) 
  
  
  output$plott2 <- renderPlotly({
    d<-event_data('plotly_click',source = 'listenhere1')
    if(is.null(d)==F){
      
      qrydd=paste("exec database_map..[spformother] '0','0','','0','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      tablename=rowSums(cbind(dtd$TotRaj,dtd$TotOtherState),na.rm=TRUE)
      
      layout <- list(
        font = list(size = 12),
        title = "",
        xaxis = list(title = "district"),
        yaxis = list(title = "records",automargin = TRUE)
      )
      
      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),source = 'link3') %>%
        add_trace( x = dtd$DistrictName, y =~tablename,name = "",type = 'bar') %>%
        layout( font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)}
    else
    {
      return(NULL)
    }
    
  })
  
  output$plott3 <- renderPlotly({
    d<-event_data('plotly_click',source = 'link3')
    if(is.null(d)==F){
     
      qrydd=paste("exec database_map..[spformother]",d$key,",'3','','0','','01/01/2017','31/12/2017'")
      
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      
      data <- data.frame(dtd$BlockName, dtd$TotRaj, dtd$TotOtherState)
      
      p <- plot_ly(dtd, x = dtd$BlockName, y = dtd$TotRaj, type = 'bar', name = 'rajasthan',key = ~paste(dtd$DID,sep = ""), source = 'link1') %>%
        add_trace(y = dtd$TotOtherState, name = 'other-state') %>%
        layout(yaxis = list(title = ''), barmode = 'group',title=dtd$BlockName)}
    else{
      return(NULL)
    }
    
  })
  
  
  output$plott4 <- renderPlotly({
    d<-event_data('plotly_click',source = 'link1')
    if(is.null(d)==F){
      qrydd=paste("exec database_map..[spformother]",d$key,",'4','1','0','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      data <- data.frame(dtd$CenterName, dtd$TotRaj, dtd$TotOtherState)
      p <- plot_ly(data, x = dtd$TotRaj, y =dtd$CenterName , type = 'bar', name = 'rajasthan',key = ~paste(dtd$CID, sep = ""),
                   marker = list(color = 'rgba(246, 78, 139, 0.6)',
                                 line = list(color = 'rgba(246, 78, 139, 1.0)'))) %>%
        add_trace(x = dtd$TotFormFOtherState, name = 'other-state',
                  marker = list(color = 'rgba(58, 71, 80, 0.6)',
                                line = list(color = 'rgba(58, 71, 80, 1.0)'))) %>%
        layout(barmode = 'stack',
               xaxis = list(title = ""),
               yaxis = list(title =""))}
    else{return(NULL)}
  })
  
  
  output$plott5 <- renderPlotly({
    
    qrydd=paste("exec database_map..[spformother] '','','','','0','0','2','0','','01/01/2017','31/12/2017'")
    qrydd
    dtd <- sqlQuery(dbcnd,qrydd)
    dtd <- data.frame(dtd)
    dtd
    closeAllConnections()
    tablename=rowSums(cbind(dtd$TotRaj, dtd$TotOtherState),na.rm=TRUE)
    
    layout <- list(
      font = list(size = 12),
      title = "OTHER_STATES",
      xaxis = list(title = "RECORDS"),
      yaxis = list(title ="STATES" ,automargin = TRUE)
    )
    
    p <- plot_ly(dtd, colors = COL, marker = list(color = COL),source = 'linkhere',orientation='h') %>%
      add_trace( x =dtd$TotOtherState,y = dtd$StateName,name = "states",type = 'bar') %>%
      layout( font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
    
    
  })
  
  
  output$plott6 <- renderPlotly({
   d<-event_data('plotly_click',source = 'linkhere')
   if(is.null(d)==F){

  qrydd=paste("exec database_map..[spformother] ",d$key,",'','','','',",d$key1,",'2','15','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      layout <- list(
        font = list(size = 12),
        title = "",
        xaxis = list(title = "district"),
        yaxis = list(title = "records",automargin = TRUE)
      )

      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),key = ~paste(dtd$StateCode, sep = ""),key1 = ~paste(dtd$DID, sep = "")) %>%
       add_trace( x = dtd$DistrictName, y = dtd$TotFormFOtherState,name = "",type = 'bar') %>%
       layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)}
   else
   {
     return(NULL)
   }

    })
  output$plott6 <- renderPlotly({
    d<-event_data('plotly_click',source = 'linkhere')
    if(is.null(d)==F){
      
      qrydd=paste("exec database_map..[spformother]",d$key,",'','','','','0','2','15','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      layout <- list(
        font = list(size = 12),
        title = "",
        xaxis = list(title = "district"),
        yaxis = list(title = "records",automargin = TRUE)
      )
      
      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),key=~paste(dtd$DID,sep = ""),source = "link10") %>%
        add_trace(x = dtd$DistrictName, y = dtd$TotOtherState,name = "",type = 'bar') %>%
        layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)}
    else
    {
      return(NULL)
    }
    
  })
  
  output$plott7 <- renderPlotly({
    d<-event_data('plotly_click',source = 'link10')
    if(is.null(d)==F){
      
      qrydd=paste("exec database_map..[spformother] '4',",d$key,",'','','','4','2','','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      layout <- list(
        font = list(size = 12),
        title = "centre details",
        xaxis = list(title = "records"),
        yaxis = list(title = "centre names",automargin = TRUE)
      )
      
      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),orientation='h',source = "link12",key= ~paste(dtd$DID,sep="")) %>%
        add_trace(x =dtd$TotOtherState , y =dtd$CenterAddress ,name = "centre",type = 'bar') %>%
        layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
    }
    else
    {
      return(NULL)
    }
    
  })
  
  output$check<-renderPrint({
    d<-event_data('plotly_click',source ='link12')
    if(length(d)==0){
      return ('click on chart')
    }
    else{
      as.list(d)
    }
  })
}


shinyApp(ui,server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.

Shiny applications not supported in static R Markdown documents

Created on 2019-10-25 by the reprex package (v0.3.0)

After applying the condition to piechart, I know it's kinda redundant to create the same reprex multiple times but I did this intentionally coz I wanted to make both separate just to make it clear and it won't be efficient and kinda inappropriate to create a new topic for this, just tryna keep it as short as possible, I hope you are getting what am trying to explain here

library(RODBC)
library(RODBCext)
#> 
#> Attaching package: 'RODBCext'
#> The following objects are masked from 'package:RODBC':
#> 
#>     odbcFetchRows, sqlFetchMore
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box
library(tidyverse)
library(readr)
library(shiny)
library(reprex)

COL = c("#9B59B6","#58D68D","#F4D03F","#909497","#D35400","#5D6D7E","#C39BD3","#5499C7","#CD6155","#BDC3C7","#808B96","#58D68D","#D7BDE2","#A93226","#B3B6B7","#138D75","#34495E","#F1948A","#D7BDE2" ,"#D35400", "#CD6155", "#5D6D7E", "#C39BD3" ,"#138D75" ,"#808B96", "#A93226" ,"#909497", "#B3B6B7" ,"#34495E" ,"#5499C7", "#9B59B6", "#58D68D" ,"#F1948A" ,"#F4D03F", "#BDC3C7", "#58D68D")
dbcnd <- odbcDriverConnect('Driver={SQL Server};Server=20.342.12.123;Database=;Uid=dan;Pwd=566$abc')
#> Warning in odbcDriverConnect("Driver={SQL
#> Server};Server=20.342.12.123;Database=;Uid=dan;Pwd=566$abc"): [RODBC]
#> ERROR: state 08001, code 17, message [Microsoft][ODBC SQL Server Driver]
#> [DBNETLIB]SQL Server does not exist or access denied.
#> Warning in odbcDriverConnect("Driver={SQL
#> Server};Server=20.342.12.123;Database=;Uid=dan;Pwd=566$abc"): [RODBC]
#> ERROR: state 01000, code 53, message [Microsoft][ODBC SQL Server Driver]
#> [DBNETLIB]ConnectionOpen (Connect()).
#> Warning in odbcDriverConnect("Driver={SQL
#> Server};Server=20.342.12.123;Database=;Uid=dan;Pwd=566$abc"): ODBC
#> connection failed

ui<-dashboardPage(
  dashboardHeader(title = "dashboard",titleWidth = 300),
  
  dashboardSidebar(
    sidebarMenuOutput("menu"),
    selectInput(inputId = "category",
                label = "STATE", 
                choices = c("RAJ","OTHER_STATES")),  
    selectInput(inputId = "category1", 
                label = "DISTRICT", choices = c("ANISTEN","BIAN")),
    selectInput(inputId = "category2", 
                label = "CENTER_NAME", choices = c("ASTER","CLANE","JABELIN"))
  ),
  
  dashboardBody(
    
    fluidRow(plotlyOutput("plott1")),
    fluidRow(plotlyOutput("plott2")),
    fluidRow(plotlyOutput("plott3")),
    fluidRow(plotlyOutput("plott4")),
    fluidRow(plotlyOutput("plott5")),
    fluidRow(plotlyOutput("plott6")),
    fluidRow(plotlyOutput("plott7")),
    verbatimTextOutput("check")
    
  ))

server<-function(input,output){
  output$plott1 <- renderPlotly({
    
    qrydd=paste("exec database_map..[spformother]'','99','','','0','01/01/2017','31/12/2017'")
    qrydd
    dtd <- sqlQuery(dbcnd,qrydd)
    dtd <- data.frame(dtd)
    dtd
    ds <- data.frame(labels = c("Raj","Other_State"),
                     values = c(dtd$TotRaj,dtd$TotOtherState)
    )
    closeAllConnections()
    
    plot_ly(ds, labels = ~labels, values = ~values,type = 'pie',source ='listenhere1',
            textposition = 'inside',
            textinfo = 'label+percent',
            insidetextfont = list(color = '#FFFFFF'),
            marker = list(colors = colors,
                          line = list(color =colors , width = 1)),showlegend = FALSE) %>% 
      layout(title = 'state wise',
             xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
             yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
  }) 
  
  
  output$plott2 <- renderPlotly({
    s <- event_data("plotly_click", source = "listenhere1")
    req(!is.null(s))
    
    if(s$pointNumber == 0){
      
      qrydd=paste("exec database_map..[spformother] '0','0','','0','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      tablename=rowSums(cbind(dtd$TotRaj,dtd$TotOtherState),na.rm=TRUE)
      
      layout <- list(
        font = list(size = 12),
        title = "",
        xaxis = list(title = "district"),
        yaxis = list(title = "records",automargin = TRUE)
      )
      
      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),source = 'link3',key = ~paste(dtd$DID, sep = "")) %>%
        add_trace( x = dtd$DistrictName, y =~tablename,name = "",type = 'bar') %>%
        layout( font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
    }
    else {
      
      qrydd=paste("exec database_map..[spformother] '','','','','0','0','2','0','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      tablename=rowSums(cbind(dtd$TotRaj,dtd$TotOtherState),na.rm=TRUE)
      
      layout <- list(
        font = list(size = 12),
        title = "OTHER_STATES",
        xaxis = list(title = "RECORDS"),
        yaxis = list(title ="STATES" ,automargin = TRUE)
      )
      
      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),source = 'linkhere',orientation='h') %>%
        add_trace( x =dtd$TotFormFOtherState,y = dtd$StateName,name = "states",type = 'bar') %>%
        layout( font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
      
      
    }
    
  })
  
  output$plott3 <- renderPlotly({
    d<-event_data('plotly_click',source = 'link3')
    if(is.null(d)==F){
     
      qrydd=paste("exec database_map..[spformother]",d$key,",'3','','0','','01/01/2017','31/12/2017'")
      
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      
      data <- data.frame(dtd$BlockName, dtd$TotRaj, dtd$TotOtherState)
      
      p <- plot_ly(dtd, x = dtd$BlockName, y = dtd$TotRaj, type = 'bar', name = 'rajasthan',key = ~paste(dtd$DID,sep = ""), source = 'link1') %>%
        add_trace(y = dtd$TotOtherState, name = 'other-state') %>%
        layout(yaxis = list(title = ''), barmode = 'group',title=dtd$BlockName)}
    else{
      return(NULL)
    }
    
  })
  
  
  output$plott4 <- renderPlotly({
    d<-event_data('plotly_click',source = 'link1')
    if(is.null(d)==F){
      qrydd=paste("exec database_map..[spformother]",d$key,",'4','1','0','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      data <- data.frame(dtd$CenterName, dtd$TotRaj, dtd$TotOtherState)
      p <- plot_ly(data, x = dtd$TotRaj, y =dtd$CenterName , type = 'bar', name = 'rajasthan',key = ~paste(dtd$CID, sep = ""),
                   marker = list(color = 'rgba(246, 78, 139, 0.6)',
                                 line = list(color = 'rgba(246, 78, 139, 1.0)'))) %>%
        add_trace(x = dtd$TotFormFOtherState, name = 'other-state',
                  marker = list(color = 'rgba(58, 71, 80, 0.6)',
                                line = list(color = 'rgba(58, 71, 80, 1.0)'))) %>%
        layout(barmode = 'stack',
               xaxis = list(title = ""),
               yaxis = list(title =""))}
    else{return(NULL)}
  })
  
  
  
  output$plott6 <- renderPlotly({
   d<-event_data('plotly_click',source = 'linkhere')
   if(is.null(d)==F){

  qrydd=paste("exec database_map..[spformother] ",d$key,",'','','','',",d$key1,",'2','15','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      layout <- list(
        font = list(size = 12),
        title = "",
        xaxis = list(title = "district"),
        yaxis = list(title = "records",automargin = TRUE)
      )

      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),key = ~paste(dtd$StateCode, sep = ""),key1 = ~paste(dtd$DID, sep = "")) %>%
       add_trace( x = dtd$DistrictName, y = dtd$TotFormFOtherState,name = "",type = 'bar') %>%
       layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)}
   else
   {
     return(NULL)
   }

    })
  output$plott6 <- renderPlotly({
    d<-event_data('plotly_click',source = 'linkhere')
    if(is.null(d)==F){
      
      qrydd=paste("exec database_map..[spformother]",d$key,",'','','','','0','2','15','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      layout <- list(
        font = list(size = 12),
        title = "",
        xaxis = list(title = "district"),
        yaxis = list(title = "records",automargin = TRUE)
      )
      
      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),key=~paste(dtd$DID,sep = ""),source = "link10") %>%
        add_trace(x = dtd$DistrictName, y = dtd$TotOtherState,name = "",type = 'bar') %>%
        layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)}
    else
    {
      return(NULL)
    }
    
  })
  
  output$plott7 <- renderPlotly({
    d<-event_data('plotly_click',source = 'link10')
    if(is.null(d)==F){
      
      qrydd=paste("exec database_map..[spformother] '4',",d$key,",'','','','4','2','','','01/01/2017','31/12/2017'")
      qrydd
      dtd <- sqlQuery(dbcnd,qrydd)
      dtd <- data.frame(dtd)
      dtd
      closeAllConnections()
      layout <- list(
        font = list(size = 12),
        title = "centre details",
        xaxis = list(title = "records"),
        yaxis = list(title = "centre names",automargin = TRUE)
      )
      
      p <- plot_ly(dtd, colors = COL, marker = list(color = COL),orientation='h',source = "link12",key= ~paste(dtd$DID,sep="")) %>%
        add_trace(x =dtd$TotOtherState , y =dtd$CenterAddress ,name = "centre",type = 'bar') %>%
        layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
    }
    else
    {
      return(NULL)
    }
    
  })
  
  output$check<-renderPrint({
    d<-event_data('plotly_click',source ='link12')
    if(length(d)==0){
      return ('click on chart')
    }
    else{
      as.list(d)
    }
  })
}


shinyApp(ui,server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.

Shiny applications not supported in static R Markdown documents

Created on 2019-10-25 by the reprex package (v0.3.0)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.