【问题标题】:Update reactive data frame in shiny gives replacement error更新闪亮的反应数据框会导致替换错误
【发布时间】:2020-09-09 15:24:09
【问题描述】:

我有下面的闪亮应用程序,用户在其中上传文件(这里我只是将dt 放在反应函数中),然后他可以从那里选择他想通过@ 显示为selectInput() 的列987654323@。如果他选择value1,他应该能够通过将所有值与numericInput() value1 相乘来更新其所有值,并创建一个新的sliderInput(),因此也更新表中显示的数据框.当我尝试使用以下内容更新我的数据框时:

dt<-reactive({input$button
    name<-c("John","Jack","Bill")
    value1<-c(2,4,6)
    dt<-data.frame(name,value1)
    dt$value1<-dt$value1*isolate(input$num)
    dt
})

我收到:replacement has 0 rows, data has 3

我的 dt 需要被读取为响应式,因为在我的原始应用程序中它是通过 fileInput() 加载的

library(DT)
    library(shiny)
    ui <- fluidPage(
        titlePanel(p("Spatial app", style = "color:#3474A7")),
        sidebarLayout(
            sidebarPanel(
                uiOutput("inputp1"),
                uiOutput("NUM"),
                #Add the output for new pickers
                uiOutput("pickers"),
                actionButton("button", "Update")
            ),
            
        mainPanel(
            DTOutput("table")
        )
    )
)

# server()
server <- function(input, output) {
    dt<-reactive({
        name<-c("John","Jack","Bill")
        value1<-c(2,4,6)
        dt<-data.frame(name,value1)
        dt$value1<-dt$value1*isolate(input$num)
        dt
        
    })
    output$NUM<-renderUI({
        if("value1" %in% colnames(dt())){
            numericInput("num", label = ("value"), value = 1)
            
        }
        else{
            return(NULL)
        }
    })
    output$inputp1 <- renderUI({
        pickerInput(
            inputId = "p1",
            label = "Select Column headers",
            choices = colnames( dt()),
            multiple = TRUE,
            options = list(`actions-box` = TRUE)
        )
    })
    
    observeEvent(input$p1, {
        #Create the new pickers 
        output$pickers<-renderUI({
            input$button2
            div(lapply(input$p1, function(x){
                if (is.numeric(dt()[[x]])) {
                    sliderInput(inputId=x, label=x, min=min(dt()[x]), max=max(dt()[[x]]), value=c(min(dt()[[x]]),max(dt()[[x]])))
                }
                else if (is.factor(dt()[[x]])) {
                    pickerInput(
                        inputId = x#The colname of selected column
                        ,
                        label = x #The colname of selected column
                        ,
                        choices = as.character(unique(dt()[,x]))#all rows of selected column
                        ,
                        multiple = TRUE,options = list(`actions-box` = TRUE)
                        
                    )
                }
                
                
            }))
        })
    })
    output_table <- reactive({
        req(input$p1, sapply(input$p1, function(x) input[[x]]))
        dt_part <- dt()
        for (colname in input$p1) {
            if (is.factor(dt_part[[colname]]) && !is.null(input[[colname]])) {
                dt_part <- subset(dt_part, dt_part[[colname]] %in% input[[colname]])
            } else {
                if (!is.null(input[[colname]][[1]])) {
                    dt_part <- subset(dt_part, (dt_part[[colname]] >= input[[colname]][[1]]) & dt_part[[colname]] <= input[[colname]][[2]])
                }
            }
        }
        dt_part
    })
    output$table<-renderDT({
        output_table()
    })
    
}

# shinyApp()
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 在某些情况下您确实需要req()。此外,尚不清楚您在output$NUM 中尝试做什么。 if("value1" %in% colnames(dt())) 应该做什么?
  • 仅当上传文件中存在“value1”列时,此数字输入才存在
  • 在这种情况下,您当前的逻辑将不起作用。你需要input$num 来创建dt(),但是你需要dt() 来创建input$num。以后再多用input$num 比较好。因此,最好将 reactiveValues 对象创建为您的数据框以启用更新。
  • 我该如何应用你的逻辑?

标签: r shiny


【解决方案1】:

试试这个代码。我不清楚你想做什么 output_table() 数据框。

library(shiny)
library(shinyWidgets)
# ui object

ui <- fluidPage(
  titlePanel(p("Spatial app", style = "color:#3474A7")),
  sidebarLayout(
    sidebarPanel(
      uiOutput("inputp1"),
      numericInput("num", label = ("value"), value = 1),
      #Add the output for new pickers
      uiOutput("pickers"),
      actionButton("button", "Update")
    ),

    mainPanel(
      DTOutput("table")
    )
  )
)

# server()
server <- function(input, output, session) {
  DF1 <- reactiveValues(data=NULL)
  
  dt <- reactive({
    name<-c("John","Jack","Bill")
    value1<-c(2,4,6)
    dt<-data.frame(name,value1)
  })
  
  observe({
    DF1$data <- dt()
  })

  output$inputp1 <- renderUI({
    pickerInput(
      inputId = "p1",
      label = "Select Column headers",
      choices = colnames( dt()),
      multiple = TRUE,
      options = list(`actions-box` = TRUE)
    )
  })

  observeEvent(input$p1, {
    #Create the new pickers
    output$pickers<-renderUI({
      dt1 <- DF1$data
      div(lapply(input$p1, function(x){
        if (is.numeric(dt1[[x]])) {
          sliderInput(inputId=x, label=x, min=min(dt1[[x]]), max=max(dt1[[x]]), value=c(min(dt1[[x]]),max(dt1[[x]])))
        }else { # if (is.factor(dt1[[x]])) {
          selectInput(
            inputId = x,       # The col name of selected column
            label = x,         # The col label of selected column
            choices = dt1[,x], # all rows of selected column
            multiple = TRUE
          )
        }

      }))
    })
  })


  dt2 <- eventReactive(input$button, {
    req(input$num)
    dt <- DF1$data ## here you can provide the user input data read inside this observeEvent or recently modified data DF1$data
    dt$value1<-dt$value1*isolate(input$num)

    dt
  })
  observe({DF1$data <- dt2()})
  
  output_table <- reactive({
    req(input$p1, sapply(input$p1, function(x) input[[x]]))
    dt_part <- dt2()
    for (colname in input$p1) {
      if (is.factor(dt_part[[colname]]) && !is.null(input[[colname]])) {
        dt_part <- subset(dt_part, dt_part[[colname]] %in% input[[colname]])
      } else {
        if (!is.null(input[[colname]][[1]])) {
          dt_part <- subset(dt_part, (dt_part[[colname]] >= input[[colname]][[1]]) & dt_part[[colname]] <= input[[colname]][[2]])
        }
      }
    }
    dt_part
  })
  
  output$table<-renderDT({
    output_table()
  })

}

# shinyApp()
shinyApp(ui = ui, server = server)

【讨论】:

  • 谢谢,这与我的想法非常接近。如果我再次需要您的帮助并提出类似的问题,我会在此处发布链接。
  • 这是一个基于你的方法的 Q。 stackoverflow.com/questions/63829996/…
【解决方案2】:

首先,您需要在您的reactive() 中包含一个req,因为input$num 在您的示例初始化时不可用:

dt<-reactive({input$button
    req(input$num)
    name<-c("John","Jack","Bill")
    value1<-c(2,4,6)
    dt<-data.frame(name,value1)
    dt$value1<-dt$value1*isolate(input$num)
    dt
})

【讨论】:

  • tnx 但它什么也不显示
猜你喜欢
  • 2016-02-10
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 2013-04-23
  • 2015-03-21
  • 2019-01-10
  • 1970-01-01
  • 2015-05-13
相关资源
最近更新 更多