在Shiny中将数据帧与actionButton的每个剪辑绑定在一起

问题描述 投票:0回答:1

我正在构建的应用程序的一部分从

clip
获取输入,该输入是为剪贴板上的 Excel 表格设计的。我希望使用
clipr::read_clip_tbl()
将其转换为 data.frame。这效果很好。但是,每次触发
clip 
actionButton
时,我希望通过单击
table2
时绑定每个数据帧中的行来更新
clip
的输出。相反,每次触发
clip
时,它仅显示最新的数据帧。

有什么建议吗?

这是有问题的部分:

  c <- reactiveValues(value=0)

  observeEvent(input$clip, {
    c$value <- c$value+1
  })

  table2 <- eventReactive(input$clip, {
      read_clip_tbl() %>%
      mutate(exp = c$value) %>%
      bind_rows()
  })

但这是完整的代码:

library(shiny)
library(dplyr)
library(shinydashboard)
library(clipr)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    actionButton('clip', 'Copy from clipboard'),
    actionButton("go", "Go")
  ),
  dashboardBody(
    tags$style(type="text/css",
               ".shiny-output-error { visibility: hidden; }",
               ".shiny-output-error:before { visibility: hidden; }"
    ),
    
    plotOutput('plot1'),
  tableOutput( 'table1'), 
  tableOutput( 'table2')
  )
)
server <- function(input, output, session) {
  
  c <- reactiveValues(value=0)
  
  observeEvent(input$clip, {
    c$value <- c$value+1
  })
  
  table2 <- eventReactive(input$clip, {
      read_clip_tbl() %>%
      mutate(exp = c$value) %>%
      bind_rows()
  })
  plot1 <- eventReactive(input$go, {
   read_clip_tbl() %>%
      mutate(treatment.name = as.factor(treatment.name)) %>%
      mort(plot_only=T)
  
    })
  table1 <- eventReactive(input$go, {
    read_clip_tbl() %>%
      mutate(treatment.name = as.factor(treatment.name)) %>%
      mort(table_only=T)
  })
  output$table2<- renderTable({
    req(table2())
    table2()
  })
    output$plot1<- renderPlot({
      req(plot1())
      plot1()
    })
    output$table1<- renderTable({
      req(table1())
      table1()
    })
}
shinyApp(ui, server)
r shiny
1个回答
0
投票

我明白了。我将数据帧转换为reactiveValue。

library(shiny)
library(dplyr)
library(shinydashboard)
library(clipr)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    actionButton('clip', 'Copy from clipboard'),
    actionButton("go", "Go")
  ),
  dashboardBody(
    tags$style(type="text/css",
               ".shiny-output-error { visibility: hidden; }",
               ".shiny-output-error:before { visibility: hidden; }"
    ),
    
    plotOutput('plot1'),
  tableOutput( 'table1'), 
  tableOutput( 'table2')
  )
)
server <- function(input, output, session) {
  comb <- data.frame
  c <- reactiveValues(value=0)
  prev <- reactiveValues(data = data.frame())
  
  observeEvent(input$clip, {
    c$value <- c$value+1
  })
  
  observeEvent(input$clip, {
    new <- read_clip_tbl()%>%
    mutate(exp = c$value)
    prev$data <- rbind(prev$data, new)
  })

  table2 <- eventReactive(input$clip, {
      prev$data 
  })
  plot1 <- eventReactive(input$go, {
   read_clip_tbl() %>%
      mutate(treatment.name = as.factor(treatment.name)) %>%
      mort(plot_only=T)
  
    })
  table1 <- eventReactive(input$go, {
    read_clip_tbl() %>%
      mutate(treatment.name = as.factor(treatment.name)) %>%
      mort(table_only=T)
  })


  output$table2<- renderTable({
    req(table2())
    table2()
  })
  


    output$plot1<- renderPlot({
      req(plot1())
      plot1()
    })
 
    


    output$table1<- renderTable({
      req(table1())
      table1()
    })
    
 

   
  

}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.