R Shiny 无法更改反应式数据框对象 [英] R Shiny can't change reactive dataframe object

查看:122
本文介绍了R Shiny 无法更改反应式数据框对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Shiny 应用程序使用 fileInput() 加载 .csv,但无法更改.

Shiny app loads a .csv with fileInput() but can't change it afterwords.

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

I want to change (multiply by (-1)) one of the columns of the dataframe within the app with a button after identifying the column

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

The object loadedData is a read.csv() function that outputs a dataframe that I use in the remainder of the app I've tried changing the object directly, through functions, be it it's reactive data or a static copy that try to override later but can't seem to make the change permanent.

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

2 errors here: If I uncomment this line in an attempt to override loadedData()

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

警告:<-: 赋值左侧无效 (NULL) 中的错误

Warning: Error in <-: invalid (NULL) left side of assignment

同样,如果我尝试

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

警告:<- 中的错误:'closure' 类型的对象不可子集

Warning: Error in <-: object of type 'closure' is not subsettable

所以我无法保存更改

打印语句是一种验证语法的方法,所以,假设我有一列第一个值为1",输出将是,

the print statements are a way to validate the syntax so, say I had a column where the 1st value is "1", output would be,

1

-1(连同列的其余部分)

-1 (along with the rest of the column)

1

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

therefore the syntax itself for the change operation is viable but I can't override the column on the imported dataframe

推荐答案

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

To store and update reactive objects, you can use reactiveVal or reactiveValues instead of 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)

这篇关于R Shiny 无法更改反应式数据框对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆