【问题标题】:Shiny : How to modify reactive objectShiny:如何修改反应对象
【发布时间】:2017-12-30 07:19:27
【问题描述】:

我有一个反应对象,当用户单击 GO 按钮时我想修改它。我尝试了这段代码,Per 给出了很好的结果。 现在考虑到第一个,我想做不止一个修改,所以每次修改时我都应该保存RA_s

我该如何处理这个问题?

代码

shinyServer(function(input, output) {

  RA_s <- reactive({
    read.csv("C:/alay/Desktop/RA.csv")
  })

  Per <- reactive({
    if(input$go == 0) return(RA_s())
    else {
       c = RA_s()
       c[1] = rep(0,nrow(RA_s()))
    }
   c
  })

      })


sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Download", tabName = "d")
)
body<- dashboardBody(
  tabItems(
    tabItem(tabName = "d",
         actionButton("go",'GO!')     )
  )
dashboardPage(
dashboardHeader(title = "Valo"),
sidebar,
body
)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    为了存储和更新响应式对象,您可以使用 reactiveVal 或 reactiveValues。

    我创建了一个简单的示例,说明这如何满足您的目标:

    server <- shinyServer(function(input, output) {
    
      RA_s <- reactiveVal()
      RA_s(1) # intiialize the reactiveVal, place your csv read here.
    
    
      # an observer to update the reactiveVal RA_s
      observeEvent(input$go, {
        # read the current value
       current_value <- RA_s()
    
       # update the value
       new_value <- current_value +1
    
       # write the new value to the reactive value
       RA_s(new_value)
    
       # print to console, just to check if it updated.
       print(paste0("Succesfully updated, new value: ",RA_s()))
      })
    
    })
    
    
    ui <- shinyUI(
      fluidPage(
        actionButton("go","GO!")     
      )
    )
    shinyApp(ui,server)
    

    希望这会有所帮助!

    【讨论】:

    • 我试过这段代码,但没有用。当我尝试修改反应对象时,我遇到了同样的错误:All arguments passed to reactiveValues() must be named
    • 我很乐意提供帮助,但您应该提供一些示例数据,以便我可以重现您的错误。特别是,请确保您提供一些数据,请参阅here
    • 当我直接在代码中使用导入功能时,它可以工作,但是当我通过fileInput 时,我得到了这个错误in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context (You tried to do something that can only be done from inside a reactive expression or observer.).,所以我认为数据没有问题。错误在于这一行 new_value &lt;- current_value[-(1)] 。 (同样的代码不使用fileinput
    【解决方案2】:

    我使用了 reactiveValues 而不是 reactiveVal,而且效果很好

    感谢@弗洛里安

    server <- shinyServer(function(input, output) {
    
       in_data <- reactive({
           inFile <- input$e
           read.csv(inFile$datapath)
        })
    
      RA_s <- reactiveValues(ra = NULL)
      RA_s$ra <- in_data() # intiialize the reactiveValue
    
    
      # an observer to update the reactiveValue RA_s
      observeEvent(input$go, {
        # read the current value
       current_value <- RA_s$ra
    
       # update the value
       new_value <- current_value[-(1)]
    
       # write the new value to the reactive value
       RA_s$ra <- new_value
    
    
      })
      output$x <- renderDataTable({
       RA_s$ra
      })
    })
    
    
    ui <- shinyUI(
      fluidPage(
        fileInput('e', 'E'),
        actionButton("go","GO!"),
        dataTableOutput('x')     
      )
    )
    shinyApp(ui,server)
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2020-07-31
      • 2018-02-07
      • 2016-08-21
      • 2020-03-24
      • 2013-06-21
      • 2021-06-19
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多