使用动作按钮移至闪亮应用程序中的数据框的下一行 [英] Use actionbutton to move to the next row of dataframe in a shiny app

查看:36
本文介绍了使用动作按钮移至闪亮应用程序中的数据框的下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有一个 shiny 应用程序,其中,当用户单击数据表的一行时,子集出现在另一个数据框 df 中,并显示文本.

当我按下 Next actionbutton()文本时,想要显示子集数据帧的下一行数据.在这种情况下,它应该显示花b的得分为5" .

我的原始数据将有很多行,因此每当我需要根据对应的行来适应文本时,按 Next .

  shinyApp(ui = fluidPage(DT :: dataTableOutput('tableId'),textOutput("celltext"),actionButton(下一个",下一个")),服务器=函数(输入,输出){output $ tableId = DT :: renderDataTable(iris [,c(1,5)],选择=列表(目标=行",模式=单个"))物种< -c("setosa","setosa","virginica","virginica"))花< -c("a","b","c","d")得分< -c(7,5,6,9)df< -data.frame(种类,花,分数)watchEvent(input $ tableId_rows_selected,{输出$ celltext<-renderText({单元格<-输入$ tableId_rows_selecteddat< -df [df $ species%in%iris [cell,5],]df< -dat [order(dat $ score,decreasing = T),]df粘贴(花",df [1,2],有得分",df [1,3])})})}) 

解决方案

定义 reactiveValues 对象会有所帮助.也许您正在寻找这个

  shinyApp(ui<-fluidPage(DT :: dataTableOutput('tableId'),textOutput("celltext"),actionButton(下一个",下一个")),服务器<-功能(输入,输出){rv<-reactValues(text = NULL)dt<-reactValues(数据= NULL)rnum<-reactVal(0)output $ tableId = DT :: renderDataTable(iris [,c(1,5)],选择=列表(目标=行",模式=单个"))物种< -c("setosa","setosa","virginica","virginica","setosa","setosa","virginica","virginica")花< -c("a","b","c","d","e","f","g","h")得分< -c(7,5,6,9,1,2,3,4)df< -data.frame(种类,花,分数)watchEvent(input $ tableId_rows_selected,{第<行-输入$ tableId_rows_selecteddat< -df [df $ species%in%iris [row,5],]dt $ data< -dat [order(dat $ score,decreasing = T),]rv $ text<-paste(花",dt $ data [1,2],有分数",dt $ data [1,3])朗姆酒(1)输出$ celltext<-renderText({rv $文本})})watchEvent(input [['next']],{rnum(rnum()+ 1)rv $ text<-paste(花",dt $ data [rnum(),2],有分数",dt $ data [rnum(),3])})}) 

I have the shiny app below in which when the user clicks on a row of the datatable a subset happens in another dataframe df and a text is displayed.

I want when I press the Next actionbutton() the text to display the data of the next row of the subseted dataframe. In this case it should display "flower b has score 5".

My original data will have many rows so when pressing Next everytime I need to adapt the text based on the correspondent row.

shinyApp(
  ui = fluidPage(DT::dataTableOutput('tableId'),
                 textOutput("celltext"),
                 actionButton("next","Next")),
  server = function(input, output) {
    output$tableId = DT::renderDataTable(
      iris[,c(1,5)],  selection = list(target = 'row',mode="single")
    )
    species<-c("setosa","setosa","virginica","virginica")
    flower<-c("a","b","c","d")
    score<-c(7,5,6,9)
    df<-data.frame(species,flower,score)
    
    observeEvent(input$tableId_rows_selected, {
    output$celltext <- renderText({
      cell <- input$tableId_rows_selected
      dat<-df[df$species %in% iris[cell,5],]
      df <-dat[order(dat$score,decreasing = T),]
      df
      paste("flower",df[1,2],"has score",df[1,3])
    })
    })
  }
)

解决方案

Defining reactiveValues object will help. Perhaps you are looking for this

shinyApp(
  ui <- fluidPage(DT::dataTableOutput('tableId'),
                 textOutput("celltext"),
                 actionButton("next","Next")),

  server <- function(input, output) {
    rv <- reactiveValues(text=NULL)
    dt <- reactiveValues(data=NULL)
    rnum <- reactiveVal(0)
    output$tableId = DT::renderDataTable(
      iris[,c(1,5)],  selection = list(target = 'row',mode="single")
    )
    species<-c("setosa","setosa","virginica","virginica","setosa","setosa","virginica","virginica")
    flower<-c("a","b","c","d","e","f","g","h")
    score<-c(7,5,6,9,1,2,3,4)
    df<-data.frame(species,flower,score)

    observeEvent(input$tableId_rows_selected, {
      row <- input$tableId_rows_selected
      dat<-df[df$species %in% iris[row,5],]
      dt$data <-dat[order(dat$score,decreasing = T),]
      rv$text <- paste("flower",dt$data[1,2],"has score",dt$data[1,3])
      rnum(1)
      output$celltext <- renderText({
        rv$text
      })

    })

    observeEvent(input[['next']], {
      rnum(rnum()+1)
      rv$text <- paste("flower",dt$data[rnum(),2],"has score",dt$data[rnum(),3])
    })
  }
)

这篇关于使用动作按钮移至闪亮应用程序中的数据框的下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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