Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 两次指定被动表达式将忽略第一个表达式_R_Shiny_Reactive - Fatal编程技术网

R 两次指定被动表达式将忽略第一个表达式

R 两次指定被动表达式将忽略第一个表达式,r,shiny,reactive,R,Shiny,Reactive,闪亮初学者:我想根据点击的动作按钮加载不同的数据集。因为从那时起,对任何数据集的处理都是相同的,所以我想将它们存储在相同的反应式表达式中,这里是dataset() 请参阅我的代码: library(shiny) ui <- shinyUI(fluidPage( sidebarLayout( sidebarPanel( actionButton("gohere", "dataset1&q

闪亮初学者:我想根据点击的动作按钮加载不同的数据集。因为从那时起,对任何数据集的处理都是相同的,所以我想将它们存储在相同的反应式表达式中,这里是
dataset()

请参阅我的代码:

library(shiny)

ui <- shinyUI(fluidPage(
    sidebarLayout(
        sidebarPanel(
            
            actionButton("gohere", "dataset1"),
            actionButton("gothere", "dataset2")
            
        ),
        
        mainPanel(
            
            tableOutput("dataset")
            
        ),        
 
        )
    )
)



server <- function(input, output) { 
    
    
dataset <- eventReactive(input$gohere, {
    
            mtcars
        })

dataset <- eventReactive(input$gothere, {
    
            cars
    
        })
    
    
output$dataset <- renderTable({
    
    dataset()
    
        })

}

shinyApp(ui = ui, server = server)

库(闪亮)

ui这与普通的R编程类似:您的两个无功导体是同名的R对象,因此第二个对象覆盖第一个对象

您可以使用无功值和一些观察者:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      actionButton("gohere", "dataset1"),
      actionButton("gothere", "dataset2")
    ),
    mainPanel(
      tableOutput("dataset")
    ),        
  )
)

server <- function(input, output) { 
  
  dataset <- reactiveVal(mtcars)
  
  observeEvent(input$gohere, {
    dataset(mtcars)
  })
  
  observeEvent(input$gothere, {
    dataset(cars)
  })
  
  output$dataset <- renderTable({
    dataset()
  })
  
}

shinyApp(ui = ui, server = server)
库(闪亮)

ui这与普通的R编程类似:您的两个无功导体是同名的R对象,因此第二个对象覆盖第一个对象

您可以使用无功值和一些观察者:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      actionButton("gohere", "dataset1"),
      actionButton("gothere", "dataset2")
    ),
    mainPanel(
      tableOutput("dataset")
    ),        
  )
)

server <- function(input, output) { 
  
  dataset <- reactiveVal(mtcars)
  
  observeEvent(input$gohere, {
    dataset(mtcars)
  })
  
  observeEvent(input$gothere, {
    dataset(cars)
  })
  
  output$dataset <- renderTable({
    dataset()
  })
  
}

shinyApp(ui = ui, server = server)
库(闪亮)

谢谢你的回答。我想这可能就是原因,这就是为什么我尝试使用
exists()
is.null()
来查看在第二次
eventReactive()
之后
dataset()
会发生什么,但它没有输出任何内容。在第二个actionButton上,我得到了输出TRUE和FALSE,单击第一个按钮没有输出。所以问题是:它是用什么覆盖它的?谢谢你的回答。我想这可能就是原因,这就是为什么我尝试使用
exists()
is.null()
来查看在第二次
eventReactive()
之后
dataset()
会发生什么,但它没有输出任何内容。在第二个actionButton上,我得到了输出TRUE和FALSE,单击第一个按钮没有输出。所以问题是:它用什么覆盖了它?