r - 更新 Shiny 的 react 数据帧会出现替换错误

标签 r shiny

我有下面这个 Shiny 的应用程序,用户在其中上传文件(这里我只是将 dt 放入 react 函数中),然后他可以从那里选择他想要将哪些列显示为selectInput() 通过 pickerInput()。如果他选择 value1,他应该能够通过将所有值与 numericInput() value1 相乘来更新其所有值,并创建一个new 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
})

我得到:替换有0行,数据有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)

最佳答案

试试这个代码。我不清楚你想做什么 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)

output

关于r - 更新 Shiny 的 react 数据帧会出现替换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63814791/

相关文章:

r - 如何在 Rcpp 中创建向量列表?

r - 如何在 R 中为分类变量创建偏相关图?

javascript - 如何隐藏R markdown中的代码语句?

r - 将图像添加到 Shiny 的应用程序

r - 如何两两比较多次测量的增减?

来自自定义 shapefile 的 R Highcharter map

r - 使用pandoc.table()减少表格的单元格宽度和字体大小

r - 从 Shiny 的发送电子邮件

r - 当输入相互依赖时,如何防止输出运行两次?

r - 交互式创建一个弹出对话框