R Shiny无法更改反应性数据框对象

时间:2019-09-01 15:07:48

标签: r shiny reactive

有光泽的应用程序使用fileInput()加载.csv,但无法在后记后更改它。

我想在识别列之后用按钮更改(乘以(-1))应用程序内数据框的其中一列

loadedData对象是一个read.csv()函数,它输出我在其余应用程序中使用的数据框,我尝试通过函数直接更改该对象,无论是反应性数据还是尝试使用的静态副本稍后覆盖,但似乎无法使更改永久生效。

#--------------UI side
#import data
sidebarLayout( 
          sidebarPanel(

            fileInput('main', 'Input file', multiple = FALSE,
                      accept =".csv"),

            checkboxInput("header", "Header", TRUE),


            radioButtons("sep", "Separator",
                         choices = c(Comma = ",",
                                     Semicolon = ";",
                                     Tab = "\t"),
                         selected = ","),


            radioButtons("quote", "Quote",
                         choices = c(None = "",
                                     "Double Quote" = '"',
                                     "Single Quote" = "'"),
                         selected = '"')
          ),
#--------------Server Side

# read dataset 

loadedData <- reactive({
    read.csv(
      input$main$datapath,
      header = input$header,
      sep = input$sep,
      quote = input$quote
    )
  })

# change signal

observeEvent(input$main, {
    output$advancedsignalchange <- renderUI({
      tagList(
        selectInput('signalchangecolumn', 'Signal Change Column', choices = names(loadedData())),
        actionButton('signalchange','Submit signal change')
      )    
    })
    })


observeEvent(input$signalchange,{

    print(loadedData)
    print(names(loadedData()))
    aa <- as.array(input$signalchangecolumn)

    print(loadedData()[aa][[1]][1]) #token value before change

    #loadedData()[aa] <-  loadedData()[aa] *-1  ## can't override
    print(loadedData()[aa] *-1) #change 

    print(loadedData()[aa][[1]][1]) #token value after change


  })

2个错误: 如果我取消注释此行以尝试覆盖loadedData()

loadedData()[aa] <- loadedData()[aa] *-1

警告:错误<-:分配左侧无效(NULL)

如果我尝试

loadedData[aa] <- loadedData()[aa] *-1

警告:<-:类型为'closure'的对象的错误不可表

所以我无法保存更改

print语句是一种验证语法的方法,因此,假设我有一列,其中第一个值为“ 1”,输出将为

1

-1(以及该列的其余部分)

1

因此,更改操作的语法本身是可行的,但我无法覆盖导入的数据框中的列

1 个答案:

答案 0 :(得分:0)

要存储和更新反应对象,可以使用reactiveValreactiveValues代替reactive

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("main", "Input file",
        multiple = FALSE,
        accept = ".csv"
      ),

      checkboxInput("header", "Header", TRUE),

      radioButtons("sep", "Separator",
        choices = c(
          Comma = ",",
          Semicolon = ";",
          Tab = "\t"
        ),
        selected = ","
      ),

      radioButtons("quote", "Quote",
        choices = c(
          None = "",
          "Double Quote" = '"',
          "Single Quote" = "'"
        ),
        selected = '"'
      )
    ),

    mainPanel(
      uiOutput("advancedsignalchange"),
      dataTableOutput("data")
    )
  )
)


server <- function(input, output, session) {

  loadedData <- reactiveVal() # create reactive val

  observe({
    req(input$main$datapath) # make sure variable isn't empty
    loadedData(read.csv( # initialize reactiveVal
      input$main$datapath,
      header = input$header,
      sep = input$sep,
      quote = input$quote)) 
  })

  observeEvent(input$main, {
    output$advancedsignalchange <- renderUI({
      tagList(
        selectInput("signalchangecolumn", "Signal Change Column", choices = names(loadedData())),
        actionButton("signalchange", "Submit signal change")
      )
    })
  })

  observeEvent(input$signalchange, {
    aa <- as.array(input$signalchangecolumn)
    tmp <- loadedData()
    tmp[aa] <- tmp[aa] * -1
    loadedData(tmp) # update reactiveVal
  })

  output$data <- renderDataTable(loadedData()) # display data
}


shinyApp(ui, server)