【问题标题】:How to subset data frame with actionButton based on user info on shiny R如何根据闪亮 R 上的用户信息使用 actionButton 对数据框进行子集化
【发布时间】:2017-09-04 17:41:21
【问题描述】:

我希望运行一段代码,而不是根据闪亮 UI 上选择的输入执行子集。

为此,我尝试了不同的方法,但没有成功:

我想在 UI 中做这样的事情:

ui = navbarPage(title = 'panel',
            fluidPage(
              fluidRow(
                column(2,
                       selectInput('a', label = "choose",
                                   choices = unique(df$a)),
                       sliderInput(
                         'b', label = 'choose range:',
                         min = 5, max = 8, value = c(6,7)),
                       actionButton('click','click')),
column(5,
      tableOutput('table')))))

在服务器上类似这样的东西

server = function(input,output){
                  df= data.frame(a = c(1,2,3,4), b = c(5,6,7,8))

  observeEvent(input$click,{
               df_ =  subset(df,  
               a == input$a & 
               b %in% input$b[1]:input$b[2])
               }) 

  input$table = renderDataTable({
  df_
  })
  }

运行这段代码会把我带到这个错误:

 shinyApp(ui, server)

 Warning: Error in $<-.reactivevalues: Attempted to assign value to a read-
 only reactivevalues object
 Stack trace (innermost first):
 47: $<-.reactivevalues
 46: $<- [#11]
 45: server [#11]
 4: <Anonymous>
 3: do.call
 2: print.shiny.appobj
 1: <Promise>
 Error in `$<-.reactivevalues`(`*tmp*`, "table", value = structure(function 
 (...)  : 
 Attempted to assign value to a read-only reactivevalues object

我认为我错过了一些具有反应性价值的重要事物,但我迷失了,因为我是新人,但很闪亮。我尝试了一些响应式按钮的示例,例如 Rstudio 提供的按钮,但没有成功。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    也许我们需要将input$table改为output$table,并在'ui'中使用DT::dataTableOutput('table')

    library(shiny)
    library(DT
    fsel <- function(dat) {
    
    ui = navbarPage(title = 'panel',
                    fluidPage(
                      fluidRow(
                        column(2,
                               selectInput('a', label = "choose",
                                           choices = unique(dat$a), selected = unique(dat$a)[1]),
                               sliderInput(
                                 'b', label = 'choose range:',
                                 min = 5, max = 8, value = c(6,7)),
                               actionButton('click','click')),
                        column(5,
                               DT::dataTableOutput('table')))))
    
    
    server = function(input,output){
    
    
      rs <- reactiveValues()
    
    
      rs$df <- dat
      observeEvent(input$click,{
      df_ <-  subset(dat,  
                      a == input$a & 
                        b %in% input$b[1]:input$b[2])
      rs$df <- df_
      }) 
    
      output$table = renderDataTable({
    
        DT::datatable(rs$df)
    
      })
    }
    shinyApp(ui = ui, server = server) 
    }
    

    -数据

    df <- data.frame(a = sample(letters[1:3], 10, replace = TRUE), 
                   b =1:10, stringsAsFactors = FALSE)
    

    -运行应用程序

    fsel(df)
    

    -输出

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 2019-10-03
      • 2017-01-05
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2013-08-15
      • 1970-01-01
      相关资源
      最近更新 更多