在闪亮的应用程序中使用actionButton更新数据框的值

时间:2020-09-08 15:25:27

标签: r shiny

我下面有一个闪亮的应用程序,用户可以在其中上传文件(这里我只是将dt放入了反应式功能),然后他可以从其中选择要通过{{1}显示的文件}。如果他选择了pickerInput(),那么他应该能够通过将所有值乘以value1 numericInput()来更新其所有值,并创建一个新的value1。当我尝试加载应用程序时,我得到:sliderInput()

subscript out of bounds

2 个答案:

答案 0 :(得分:2)

要使用用户输入数据,最好将pickerInput移动到服务器端。然后使用observeEvent更新反应性数据并将其分配给reactValues数据帧。试试这个

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(
      
    )
  )
)

# server()
server <- function(input, output) {
  name<-c("John","Jack","Bill")
  value1<-c(2,4,6)
  dt<-data.frame(name,value1)
  
  DF1 <- reactiveValues(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
          )
        }
        
      }))
    })
  })
  
  observeEvent(input$button, {
    dt<-reactive({
      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)
      return(dt)
    })
    
    DF1$data <- dt()
  })
  
  
}

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

答案 1 :(得分:1)

reactive应该仍然可以正常工作。由于此部分,您得到subscript out of bounds

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()返回。在这种情况下,是dt$value1<-dt$value1*isolate(input$num),它默默地评估为dt$value1,而不是data.frame。试试:

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
})