首页 > 解决方案 > 在动作按钮被点击后绘制闪亮

问题描述

我正在尝试在单击操作按钮后绘制图表但它不起作用,我正在尝试使用观察 Event()Isolate()函数

ui。具有相同过程的 us 和 code。Uioutput是我正在调用的操作栏,我的意图是单击操作按钮后应该出现每个输出图服务器包含我正在以羽毛格式读取的 csv 文件的路径,但这没关系,重要的是操作按钮使用情节。我还对数据进行了子集化,sbst.unt并在闪亮的应用程序上呈现表格。 用户界面

ui <- fluidPage(
  titlePanel("Neospec Visualization"),
  sidebarLayout(


    sidebarPanel(

      uiOutput("FaceUnit"),

      tags$hr(),

      uiOutput("FaceType")

    ),



    mainPanel(
      tabsetPanel(
        tabPanel("Table", dataTableOutput("table"),6),

        h3("Data table view"),
        #withSpinner(DT::dataTableOutput("contents"),6),
        #dataTableOutput("tt"),
        h3("Raw Neospec signatures"),
        withSpinner(plotOutput("plts"),6)

      )

    )
  )
)

服务器.R

    server <- function(input, output){


  neos <- reactive({read_feather("path")})


  output$FaceUnit <- renderUI({

    actionButton(inputId = "FaceUnit", label = " Unit")


  })
    output$FaceType <- renderUI({

      actionButton(inputId = "FaceType", label = " Type")

    })

  sbst.unt<-reactive({
    neodt<-neos()
    unt.sbst <- neodt[(neodt$unit==input$unit & neodt$Type==input$Type),]
    unt.sbst
  })

  output$table <- renderDataTable({sbst.unt()})


  observeEvent(
  output$plts <- renderPlot({

    input$FaceType

    isolate({


    plt.dt2 <- neos()

    wavelength2<-as.numeric(substr(colnames(plt.dt2[,-c(1:3)]),2,19))

    colnames(plt.dt2) <- c("SSN","unit","Type",wavelength2)

    spec.m2 <- melt(plt.dt2, id = c("SSN","unit","Type"))


    p2 <- ggplot(data =spec.m2 , aes(x = as.numeric(as.vector(variable)),y = value, group = SSN)) +

      geom_line(size = 0.1, col = "blue", alpha = 0.8) +

      ggtitle("Neospec raw spectrums ") +

      xlim(range(wavelength2))+

      ylim(c(0,1)) +

      xlab("Wavelength (nm)") +

      ylab("Reflectance") + 
      #theme with white background
      theme_bw() +
      #eliminates background, gridlines, and chart border
      theme(
        plot.background = element_blank()
        ,panel.grid.major = element_blank()
        ,panel.grid.minor = element_blank()
      )
    p2 <- p2 + theme(plot.title = element_text(hjust = 0.5))

    p2 <- p2 + theme(legend.position = "none")

    fac.typ <- p2 + facet_grid(.~Type, switch ='y', scales = "free")

    fac.typ


    })
  }))
}`


shinyApp(ui = ui, server = server)

 dput(SSN   unit    Type    X2600.000003874302  X2597.4609457191823 X2594.926835544204  X2592.3976714178884 X2589.8734263803212
RResmicro1g3SI1 Unit1   soil    0.37285368  0.364537573 0.356995724 0.350070815
RResmicro1g3SI1 Unit2   soil    0.295855514 0.292268904 0.289343551 0.286564459 
RResmicro1g3SI1 Unit3   soil    0.296041336 0.294366508 0.292749726 0.291253321
RResmicro1mSGe2 Unit1   soil    0.387475087 0.38768638  0.387886013 0.388117495
RResmicro1mSGe2 Unit2   soil    0.428004392 0.42284043  0.41852246  0.414420365
RResmicro1mSGe2 Unit3   soil    0.422322559 0.419495941 0.416767303 0.414211552
RresMicro1mtHj  Unit1   dung    0.458153765 0.456678695 0.455340966 0.454036524
RresMicro1mtHj  Unit2   dung    0.429987543 0.429523389 0.429238502 0.428967891
RresMicro1mtHj  Unit3   dung    0.413184068 0.412489425 0.411818841 0.411190139)

标签: rshiny

解决方案


像这样的东西?

library(shiny)
library(ggplot2)

ui <- fluidPage(
  titlePanel("Neospec Visualization"),
  sidebarLayout(
    sidebarPanel(
      uiOutput("FaceUnitOut"),
      tags$hr(),
      uiOutput("FaceTypeOut")
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Table", dataTableOutput("table"),6),
        h3("Data table view"),
        #withSpinner(DT::dataTableOutput("contents"),6),
        #dataTableOutput("tt"),
        h3("Raw Neospec signatures"),
        withSpinner(plotOutput("plts"),6)
      )
    )
  )
)

server <- function(input, output){

  neos <- reactive({
    read_feather("path")
  })

  output$FaceUnitOut <- renderUI({
    actionButton(inputId = "FaceUnit", label = " Unit")
  })

  output$FaceTypeOut <- renderUI({
    actionButton(inputId = "FaceType", label = " Type")
  })

  sbst.unt<-reactive({
    neodt <- neos()
    unt.sbst <- neodt[(neodt$unit==input$unit & neodt$Type==input$Type),]
    unt.sbst
  })

  output$table <- renderDataTable({
    sbst.unt()
  })

  # here you react off the FaceType button
  plotdata <- eventReactive(input$FaceType,{
    req(input$FaceType)
    neos()
  })

  output$plts <- renderPlot({
    plt.dt2 <- plotdata()

    wavelength2 <- as.numeric(substr(colnames(plt.dt2[,-c(1:3)]),2,19))
    colnames(plt.dt2) <- c("SSN","unit","Type",wavelength2)
    spec.m2 <- melt(plt.dt2, id = c("SSN","unit","Type"))

    p2 <- ggplot(data = spec.m2 , aes(x = as.numeric(as.vector(variable)),y = value, group = SSN)) +
      geom_line(size = 0.1, col = "blue", alpha = 0.8) +
      ggtitle("Neospec raw spectrums ") +
      xlim(range(wavelength2))+
      ylim(c(0,1)) +
      xlab("Wavelength (nm)") +
      ylab("Reflectance") + 
      #theme with white background
      theme_bw() +
      #eliminates background, gridlines, and chart border
      theme(
        plot.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank())
    p2 <- p2 + theme(plot.title = element_text(hjust = 0.5))
    p2 <- p2 + theme(legend.position = "none")
    fac.typ <- p2 + facet_grid(.~Type, switch ='y', scales = "free")
    fac.typ
  })
}

shinyApp(ui = ui, server = server)

推荐阅读