0

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

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

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)
    })
}

如何通过选定的滑块输入创建 df1?

4

1 回答 1

0

您可能想levels在.countieschoicesui

此外,在 中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)
  })
  
}
于 2021-01-27T21:19:01.720 回答