【问题标题】:Plot after action button is click in shiny在动作按钮被点击后绘制闪亮
【发布时间】:2019-01-10 00:08:03
【问题描述】:

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

喂。具有相同过程的 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)

      )

    )
  )
)

服务器.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)

【问题讨论】:

  • 看看eventReactive
  • 有什么方法可以推荐我在代码上使用

标签: r shiny


【解决方案1】:

这样的?

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)

【讨论】:

  • 效果很好,唯一的挑战是它的绘图和输出速度很慢$table
  • 请提供一些数据,以便我整理。我在没有实际运行的情况下编写了代码
  • 你怎么能得到它...我能做到的
  • dput(data)的结果粘贴到问题中
  • 我已经用一小块数据完成了,它很乱,但是它的组织列以 SNN、单位、类型然后列来绘制的方式,现在我将数据转换为羽毛格式,对其进行子集化,然后融化。你可以选择前 4 行来绘制
猜你喜欢
  • 1970-01-01
  • 2019-04-19
  • 2017-03-03
  • 1970-01-01
  • 2019-08-14
  • 2016-02-09
  • 2021-03-12
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多