【问题标题】:Updating rhandsontable from sliderInput从 sliderInput 更新 rhandsontable
【发布时间】:2021-05-01 15:30:09
【问题描述】:

我面临与 rhandsontables 相关的各种操作的挑战,尽管它们对于我正在尝试创建的应用程序来说似乎是无价的。我的第一个挑战是我试图通过从用户输入中过滤原始数据框来更新 rhandsontable,从下拉列表 (selectInput) 中选择。

这是一个简化的可重现示例,它让我得到了我想要的结果,但不是通过我希望的方式:

library(shiny)
library(rhandsontable)
library(tidyverse)

counties = as.factor(c(rep("County1", 2), rep("County2", 2)))
compnames = as.factor(c("comp1", "comp2", "comp3", "comp4"))
properties = 1:4
soilsData <- data.frame(county = counties, compname = compnames, property = properties)

ui <- fluidPage(
    
    selectInput(inputId = "selectCounty", label = h4("Select County"), 
                choices = counties,
                selected = "County1",
                multiple = FALSE),
    br(),
    br(),
    rHandsontableOutput('rTable'),
    
)

server <- function(input, output) {
 
    df1 <- df %>% 
        filter(county == "County1", compname == "comp1")
    
    datavalues <- reactiveValues(data = df1)

    output$rTable <- renderRHandsontable ({
        rhandsontable(datavalues$data,
                      rowHeaders = NULL)
    })
}

如何通过选定的 sliderInput 创建那个 df1?

【问题讨论】:

    标签: r shiny rhandsontable selectinput


    【解决方案1】:

    您可能希望在ui 中使用levelscounties 作为您的choices

    此外,在server 中,您可以使用reactive 表达式来过滤您的数据。如果您希望对 selectInput 的更改做出反应(我假设您的意思是,而不是 sliderInput),它应该是反应式表达式。

    引用时,您需要包含括号。例如,如果您将反应式表达式称为datavalues,那么datavalues() 应该用于rhandsontable 中的数据。

    如果这是您要找的,请告诉我。

    library(shiny)
    library(rhandsontable)
    library(tidyverse)
    
    ui <- fluidPage(
      selectInput(inputId = "selectCounty", 
                  label = h4("Select County"), 
                  choices = levels(counties),
                  selected = "County1",
                  multiple = FALSE),
      br(),
      br(),
      rHandsontableOutput('rTable'),
    )
    
    server <- function(input, output) {
      
      datavalues <- reactive({
        soilsData %>% 
          filter(county == input$selectCounty, 
                 compname == "comp1")
      })
      
      output$rTable <- renderRHandsontable ({
        rhandsontable(datavalues(),
                      rowHeaders = NULL)
      })
      
    }
    

    【讨论】:

    • 谢谢!那太棒了!在之前的尝试中,我曾尝试将过滤器置于反应函数中,但忘记在 rhandsontable 中的“datavalues”之后放置括号!我认为这是我缺少的一些简单的东西。非常感谢您的帮助。
    猜你喜欢
    • 2017-12-02
    • 2018-04-28
    • 1970-01-01
    • 2017-04-13
    • 2020-01-07
    • 2020-01-15
    • 2019-09-30
    • 2016-11-17
    • 2016-06-10
    相关资源
    最近更新 更多