单击闪亮后的动作按钮

时间:2018-08-02 09:16:09

标签: r shiny

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

Ui。具有相同程序的us和code。 Uioutput是我正在调用的操作栏,我的意图是每个输出图都应在单击操作按钮后发生 服务器包含我正在以羽毛格式读取的csv文件的路径,但这并不重要,重要的是操作按钮与绘图配合使用。我还对数据sbst.unt进行了子集设置,并在闪亮的应用程序上渲染表格。 UI.R

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)

      )

    )
  )
)

SERVER.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)

1 个答案:

答案 0 :(得分:0)

像这样吗?

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)