根据闪亮应用程序的朗声表中其他单元格的修改来更改单元格 [英] Change cells based on modifications of other cells in rhandsontable of a shiny app

查看:58
本文介绍了根据闪亮应用程序的朗声表中其他单元格的修改来更改单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个带有rhosontable的闪亮应用程序.这个rhonsontable基于我在应用程序内创建的datframe.

I am trying to build a shiny app with rhandsontable in it. This rhandsontable is based on the datframe I create inside the app.

在应用程序中,我最初显示此数据帧的第一行,共有3列.当第一列的值被其下拉级别列表修改并按search时,则修改其他两列.

In the app I initially display the first row of this dataframe with al 3 columns. When the value of the 1st column is modified by the list of its dropdown levels and press search then the other 2 columns are modified.

我也想对第二栏做同样的事情.我也想一开始只显示前两列,而当按下搜索按钮时当然会显示第三列,当然,如果存在此行.

I would like to do the same with the second column as well. Also I would like initially to display only the first 2 columns and the third will be displayed when the search button is pressed and of course if this row exists.

我试图复制我对第一列所做的操作(注释的代码),但是它不起作用.前2列应始终在下拉列表中显示其所有级别,而第三列则是每次搜索后仅显示可用级别.

I tried to replicate what I did for the 1st column (commented code) but it does not work. The first 2 columns should always display all of their levels in the dropdown but the third only the available ones after the every search.

DF = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                           car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                           transmission=factor(rep(c("automatic","manual"),5)))
write.csv(DF,"C:/Users/User/Documents/Test//cars.csv", row.names = FALSE)

ui.r

library(shiny)
library(rhandsontable)


ui <- fluidPage(

  titlePanel("RHandsontable"),
  sidebarLayout(
    sidebarPanel(
     fileInput("file1", "Choose CSV File",
            accept = c(
              "text/csv",
              "text/comma-separated-values,text/plain",
              ".csv"),
      actionButton("sr", "Search")
    ),
    mainPanel(
      rHandsontableOutput("test")
    )
  )
)

server.r

server <- function(input, output) {

   # Assign value of 12345 as default to postcode for the default table rendering
   values <- reactiveValues(postcode = "12345"
                            #car_group = "Microcar"
                            ,tabledata = data.frame())


   # An observer which will check the value assigned to postcode variable and create the sample dataframe
   observeEvent(values$postcode,{
      inFile <- input$file1

  if (is.null(inFile))
     return(NULL)

  DF<- read.csv(inFile$datapath,stringsAsFactors = T)
  for(i in 1:ncol(DF)){
     DF[,i]<-as.factor(DF[,i])

  }
  DF
      DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                       car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                       transmission=factor(rep(c("automatic","manual"),5)))
      # Created dataframe is assigned to a reactive dataframe 'tabledata'
      values$tabledata <- DF2[ which(DF2$agency_postcode ==values$postcode
                                     #&DF2$car_group==values$car_group
                                     ), ]
      for(i in 2:ncol(values$tabledata)){
         values$tabledata[,i] <- factor(values$tabledata[,i])
      }
   })

   # Capture changes made in the first column of table and assign the value to the postcode reactive variable. This would then trigger the previous observer
   observeEvent(input$test$changes$changes,{
      col <- input$test$changes$changes[[1]][[2]]
      if(col==0){
         values$postcode <- input$test$changes$changes[[1]][[4]]
         #values$car_group<-input$test$changes$changes[[1]][[4]]
      }
   })

   # Use the reactive df 'tabledata' to render.
   output$test <- renderRHandsontable({input$sr
      isolate(rhandsontable(values$tabledata[1,], rowHeaders = NULL, width = 550, height = 300)%>%
         hot_col(colnames(values$tabledata))) 
   })


}

推荐答案

在为检索第二列中选择的值而添加的代码中,我们将需要进行一些更新.

In the code that you have added for retrieving the value selected in second column, we would need to update something.

if(col==0){
         values$postcode <- input$test$changes$changes[[1]][[4]]
         #values$car_group<-input$test$changes$changes[[1]][[4]]
      }

handsonable的索引以0开头.因此,其第一列的索引为0,第二列的索引为1,这意味着您无法在 if 中将值更新为 car_group 反应变量.第一列的条件

Index of handsontable starts with 0. So, its 0 for first column and 1 for second column, meaning you cannot update the values to car_group reactive variable within the if condition for the first column

根据我在此处提供的答案为您当前问题提供的解决方案. 通过更改一个单元格的值来更新rhandsontable

A solution to your current question based on the answer that I provided here. Update rhandsontable by changing one cell value

library(shiny)
library(rhandsontable)


ui <- fluidPage(

   titlePanel("RHandsontable"),
   sidebarLayout(
      sidebarPanel(),
      mainPanel(
         rHandsontableOutput("test")
      )
   )
)


server <- function(input, output) {

  # Assigning blank values to reactive variable as all the values need to be listed first
  values <- reactiveValues(postcode = "",cargroup = "",tabledata = data.frame())

  observeEvent(values$postcode,{
    DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                     car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                     transmission=factor(rep(c("automatic","manual"),5)))
    # When the user selects any value from the dropdown, filter the table and update the value of reactive df
    if(values$postcode!=""){
      values$tabledata <- DF2[ which(DF2$agency_postcode ==values$postcode), ]
    }else{
      # When the postcode value is blank, meaning the user hasn't selected any, the table 
      # will render without the third column
      values$tabledata <- DF2[,-3]
    }

  })

  observeEvent(values$cargroup,{
    DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                     car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                     transmission=factor(rep(c("automatic","manual"),5)))
    values$tabledata <- DF2
    # When the user selects any value from the dropdown, filter the table and update the value of reactive df
    if(values$cargroup!=""){
      values$tabledata <- DF2[ which(DF2$car_group ==values$cargroup), ]
    }else{
      # When the cargroup value is blank, meaning the user hasn't selected any, the table 
      # will render without the third column
      values$tabledata <- DF2[,-3]
    }

  })

  # Observer for changes made to the hot
  observeEvent(input$test$changes$changes,{
    col <- input$test$changes$changes[[1]][[2]]
    # Changes made in first column
    if(col==0){
      values$postcode <- input$test$changes$changes[[1]][[4]]
    }
    # Changes made in second column
    if(col==1){
      values$cargroup <- input$test$changes$changes[[1]][[4]]
    }
  })

  # Render the hot object
  output$test <- renderRHandsontable({
    rhandsontable(values$tabledata[1,], rowHeaders = NULL, width = 550, height = 300)%>%
      hot_col(colnames(values$tabledata)) 
  })


}

shinyApp(ui = ui, server = server)

检查是否满足您的需求.然后,您可以基于搜索按钮更新观察者部分,而不用对用户所做的更改做出反应.

Check if this suits your needs. You can then update the observer part based on search button instead of being reactive to the changes made by user.

这篇关于根据闪亮应用程序的朗声表中其他单元格的修改来更改单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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