闪亮的R动作按钮控制反应元素

时间:2014-11-12 15:24:53

标签: r shiny shiny-server

不确定我是否应该使用这个术语。基本上,我有一个反应函数,显示用户上传的CSV文件,我想使用action button来激活绘图生成过程。此时,该图总是在运行中生成。所以我想知道,在renderPlot函数中,如何让action button覆盖reactive元素。谢谢!

ui.R

library(shiny)

shinyUI(fluidPage(
  titlePanel("Test"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),

      checkboxGroupInput('show_vars', 'Columns to show:',
                         choices=c("Col1"="Col1", "Col2"="Col2", "Col3"="Col3"), 
                         c("Col1"="Col1", "Col2"="Col2", "Col3"="Col3")),

      uiOutput("study"),
      actionButton("go", "Draw the plot")
    ),
    mainPanel(
      dataTableOutput('contents'),
      plotOutput("Plot", width = 800, height = 800)

    )
  )
))

server.R

shinyServer(function(input, output) {

  dataInput <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    data_load<-subset(read.csv(inFile$datapath, header=T, sep=",", quote='"', stringsAsFactors=FALSE), select=input$show_vars)
    data_study<-unique(data_load$Col2)
    return (list("data_load"=data_load, "data_study"=data_study))
  })

  study <- reactive({
    all_citation<-dataInput()$data_load$Col2
    study_choose<-unlist(lapply(input$columns,  function(x) which(all_citation==x)))
    return (list("study_choose"=study_choose))
  })

  ##Generate the data table####
  output$contents <- renderDataTable({
    study_choose_temp<-study()$study_choose
    dataInput()$data_load[study_choose_temp,]
  })

  ###I would like the actionButton controls reactive elements 
  ###dataInput()$data_load$Col3[study()$study_choose]

  output$Plot <- renderPlot({
    input$go
    hist(dataInput()$data_load$Col3[study()$study_choose])
  })

  output$study <- renderUI({
    # If missing input, return to avoid error later in function
    if(is.null(dataInput()$data_study)) return()
    # Create the checkboxes and select them all by default
    checkboxGroupInput("columns", "Choose studies to plot", 
                       choices  = dataInput()$data_study,
                       selected = dataInput()$data_study)

  })
})

###CSV template

1 个答案:

答案 0 :(得分:6)

isolate中使用renderPlot(),以便只有在input$go按钮被触发时才会更新。如果这样做,当其他被动元素被更改时,绘图将不会重绘,但input$go超出isolate,因此它将导致绘图重绘。

 output$Plot <- renderPlot({
    input$go
    isolate( 
        hist(dataInput()$data_load$Col3[study()$study_choose]) 
    )
  })