使用plotly/dygraphs在Shining中创建动态图形

使用plotly/dygraphs在Shining中创建动态图形,r,dynamic,shiny,plotly,r-plotly,R,Dynamic,Shiny,Plotly,R Plotly,我期待着创建一个数据集的基础上,我不知道长度的图形动态数量。总有一天它会很长再长20。。 我想对动态图或plotly使用相同的代码,因此不想在plotly中使用子图作为选项 我创建了下面的,但它不循环plotly方面,或者如果我在ploty部分中输入for循环,则全部执行4 X,然后打印名称是一个问题,因为它有重复项。。运行时,您可以看到显示问题的打印语句 关于如何让这段代码为每行数据创建一个图形,您有什么想法吗 shinyServer(function(input, output) {

我期待着创建一个数据集的基础上,我不知道长度的图形动态数量。总有一天它会很长再长20。。 我想对动态图或plotly使用相同的代码,因此不想在plotly中使用子图作为选项

我创建了下面的,但它不循环plotly方面,或者如果我在ploty部分中输入for循环,则全部执行4 X,然后打印名称是一个问题,因为它有重复项。。运行时,您可以看到显示问题的打印语句

关于如何让这段代码为每行数据创建一个图形,您有什么想法吗

shinyServer(function(input, output) { 


     # Insert the right number of plot output objects into the web page 


    ############################# graph data###################

     observe({BRANDslotpcp()})
     BRANDslotpcp<-reactive({

        slot_id<-c("a-slot1","b-slot0")
     nga_diagnostic_code<-c("BT","XT")
     nga_color<-c("black","red")
     expr_1<-c(10,20)
     period_date<-c("2019-10-01","2019-10-01")

        a<-data.frame(slot_id,nga_diagnostic_code,nga_color,expr_1,period_date)

        return(a)

     })
  ############################slot options #########################   

     slotdrop<-eventReactive(BRANDslotpcp() ,{
        req(BRANDslotpcp())

        slot1a<- unique(BRANDslotpcp()$slot_id)
       # print(slot1a)
        # slot1<- slot1a[order(slot1a,decreasing=TRUE),]
        # print(slot1a)
        slot1<-data.frame(slot1a)
      #  print(slot1[2,1])
        return(slot1)
        #(slot1)
     })



     ###################################


     #observeEvent(slotdrop(),{

  observe({

     for (i in 1:nrow(slotdrop())){

             plotname2 <- paste0("pcpslotbrandenburgplot", slotdrop()[i,1])
     #


       print(i)
       print("i") 

       local({
             output[[plotname2]] <- renderPlotly ({

           myi<-i
               #for (i in 1:nrow(slotdrop())){
                 plotname1 <<- paste0("pcpslotbrandenburgplot", slotdrop()[myi,1])

                 print("test")
                 print(plotname1)

                  # plotname <- paste0("pcpslotbrandenburgplot", slotdrop()[i,1])

                 a<-slotdrop()[myi,1]
               print("a")
               print(a)
               #View(BRANDslotpcp())

               dataname<- BRANDslotpcp()[BRANDslotpcp()$slot_id == a,]



               print(dataname)
               print( "dataname")


                      plot_ly(dataname, x = substring(as.character(dataname$period_date),0,10), y = dataname$expr_1, 
                              type = 'bar',name=dataname$nga_diagnostic_code,
                              text =~paste(dataname$expr_1,"-",dataname$nga_diagnostic_code),
                              textposition = 'inside',
                              #textinfo = 'count', 
                              insidetextfont = list(color = '#000000'), 
                              marker = list(color = dataname$nga_color,line = list(color = '#FFFFFF', width = 1)),
                              showlegend = FALSE) %>%
                         config(displayModeBar = F)%>%
                         #layout(yaxis = list(title = 'Count'), barmode = 'stack')
                         layout(title = plotname1 ,margin = list(b = 160),xaxis = list(title = "", tickangle = 90), yaxis = list(title = 'Count'), barmode = 'stack')
              # }
                   })
         })


             output$plots <- renderUI({ 
               plot_output_list <- lapply(1:nrow(slotdrop()), function(i) { 
                 plotname <- paste0("pcpslotbrandenburgplot", slotdrop()[i,1]) 

                 print("plotname here")
                 print(plotname)
                 plotlyOutput(plotname, height = 280, width = 550)
               })

               print(plot_output_list)
               do.call(tagList, plot_output_list)

             })      

     }
     })





               }) 
shinyServer(函数(输入、输出){
#在网页中插入正确数量的打印输出对象
#############################图形数据###################
观察({BRANDslotpcp()})

BRANDslotpcp确定修复了此问题。以下是服务器代码

图书馆(闪亮) 图书馆(绘本)

观察({BRANDslotpcp()})

BrandSlotpCPP您没有提供任何UI代码-请编辑并查看。代码太长。您能通过仅包含必要的内容来减小大小吗?
observe({BRANDslotpcp()})
BRANDslotpcp<-reactive({
  slot_id<-c("a-slot0","b-slot1","c-slot2")
  nga_diagnostic_code<-c("BT","XT","PPI")
  nga_color<-c("green","red","blue")
  expr_1<-c(10,20,30)
  period_date<-c("2019-10-01","2019-10-01","2019-10-01")
  a<-data.frame(slot_id,nga_diagnostic_code,nga_color,expr_1,period_date)
  #View(a)
  return(a)
})




slotdrop<-eventReactive(BRANDslotpcp() ,{
  req(BRANDslotpcp())
  slot1a<- unique(BRANDslotpcp()$slot_id)
  slot1<-data.frame(slot1a)
 # View(slot1)
  return(slot1)
})


shinyServer(function(input, output) {



  # Insert the right number of plot output objects into the web page
  output$plots <- renderUI({



    #  print(i)
    plot_output_list <- lapply(1:nrow(slotdrop()), function(i) {

      plotname <- paste("pcpslotbrandenburgplot", slotdrop()[i,1], sep="")


     # tags$div(class = "group-output",
      plotlyOutput(plotname, height = 280, width = 250)
    })

    #}

    print(plot_output_list)


    # Convert the list to a tagList - this is necessary for the list of items
    # to display properly.
  #  do.call(tagList, plot_output_list)

  })
  observe({
  # Call renderPlot for each one. Plots are only actually generated when they
  # are visible on the web page.
  for (i in 1:nrow(slotdrop())) {
    # Need local so that each item gets its own number. Without it, the value
    # of i in the renderPlot() will be the same across all instances, because
    # of when the expression is evaluated.
    local({
      tst<-slotdrop()[i,1]
     # View(tst)
      dataname<- BRANDslotpcp()[BRANDslotpcp()$slot_id == tst,]

      plotname <- paste("pcpslotbrandenburgplot",tst, sep="")
      output[[plotname]] <- renderPlotly({
        print(dataname$expr_1) 
      plot_ly(dataname, x= substring(as.character(dataname$period_date),0,10),y= dataname$expr_1,
                type = 'bar',name=dataname$nga_diagnostic_code,
                text =~paste(dataname$expr_1,"-",dataname$nga_diagnostic_code),
                textposition = 'inside',
                #textinfo = 'count',
                insidetextfont = list(color = '#000000'),
                marker = list(color = dataname$nga_color,line = list(color = '#FFFFFF', width = 1)),
                showlegend = FALSE) %>%
          config(displayModeBar = F)%>%
          #layout(yaxis = list(title = 'Count'), barmode = 'stack')
          layout(title = plotname ,margin = list(b = 160),xaxis = list(title = "", tickangle = 90), yaxis = list(title = 'Count'), barmode = 'stack')
       # plot(factor(dataname$period_date),dataname$expr_1, ylab="a",xlab="b",main = paste(plotname, sep = ""))
      })
    })
  }
})
})

ui is 

shinyUI(pageWithSidebar( 


     headerPanel("Dynamic number of plots"), 


     sidebarPanel(

       ),


     mainPanel( 
         # This is the dynamic UI for the plots 
         uiOutput("plots") 
       ) 
   ))