重新调整闪亮的反应变量

relevel a reactive variable in shiny

我想使用 shiny 读取 CSV 文件,然后允许用户能够在该文件的特定列中选择参考级别。 想象一下这样的数据:

mydata <- data.frame(letter = rep(letters[1:3], 3), num = 1:9)
write.csv(mydata, "mydata.csv", row.names = FALSE)

这是我的代码:

library(shiny)
ui <- fluidPage(
  fileInput(
    "file",
    label = "File Input",
    accept = ".csv"
  ),
  selectInput(
    inputId = "ref",
    label = "Reference Outcome",
    choices = NULL
  ),
  actionButton( inputId = "action",
                label = "action"),
  textOutput("text")
)

server <- function(input, output, session) {
  observe({
    updateSelectInput(session,
                      "ref",
                      choices = unique(data()[,1]))
  })

  data <- reactive({
    inFile <- req(input$file)
    fileinput <- read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)
    return(fileinput)
  })
  
  observeEvent(input$action,{
    data()$letter <- factor(data()$letter)
    data()$letter <- relevel(data()$letter, ref = input$ref)
    output$text <- renderText({
      paste("Your reference level is: ", levels(data()$letter)[1])
    }) 
  })  
}
shinyApp(ui, server)

我收到这个错误:

Warning: Error in <-: invalid (NULL) left side of assignment
  [No stack trace available]

我知道您不应该在赋值的左侧使用反应变量,但是如何在将数据导入为反应变量后重新调整数据的水平?

试试这个

server <- function(input, output, session) {
  rv <- reactiveValues(df=NULL)
  observe({
    updateSelectInput(session,
                      "ref",
                      choices = unique(data()[,1]))
  })
  
  data <- reactive({
    inFile <- req(input$file)
    fileinput <- read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)
    return(fileinput)
  })
  
  observeEvent(input$action,{
    rv$df <- data()
    rv$df$letter <- factor(rv$df$letter)
    rv$df$letter <- relevel(rv$df$letter, ref = input$ref)
    output$text <- renderText({
      paste("Your reference level is: ", levels(rv$df$letter)[1])
    }) 
  })  
}

除了每次单击操作按钮时数据帧值不会更新外,这里的答案几乎相同。仅在上传文件时更新。

library(shiny)

mydata <- data.frame(letter = rep(letters[1:3], 3), num = 1:9)
write.csv(mydata, "mydata.csv", row.names = FALSE)
  
  
ui <- fluidPage(
  fileInput(
    "file",
    label = "File Input",
    accept = ".csv"
  ),
  selectInput(
    inputId = "ref",
    label = "Reference Outcome",
    choices = NULL
  ),
  actionButton( inputId = "action",
                label = "action"),
  textOutput("text")
)

server <- function(input, output, session) {
  observe({
    updateSelectInput(session,
                      "ref",
                      choices = unique(data()[,1]))
  })
  
  data <- reactive({
    inFile <- req(input$file)
    fileinput <- read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)
    r$df <- fileinput
  })
  
  r <- reactiveValues(
    df = NULL
  )

  observeEvent(input$action,{
    r$df$letter <- factor(r$df$letter)
    r$df$letter <- relevel(r$df$letter, ref = input$ref)
    output$text <- renderText({
      paste("Your reference level is: ", levels(r$df$letter)[1])
    }) 
  })  
}
shinyApp(ui, server)