闪亮:如何修改反应对象

时间:2017-07-24 15:56:28

标签: r shiny

当用户点击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
)

2 个答案:

答案 0 :(得分: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)

希望这有帮助!

答案 1 :(得分:1)

我使用reactiveValues而不是reactiveVal,它完美地运行

谢谢@Florian

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)
相关问题