问题描述
我面临着与 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(),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?
解决方法
您可能希望将 levels
的 counties
用于 choices
中的 ui
。
此外,在 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(),rHandsontableOutput('rTable'),)
server <- function(input,output) {
datavalues <- reactive({
soilsData %>%
filter(county == input$selectCounty,compname == "comp1")
})
output$rTable <- renderRHandsontable ({
rhandsontable(datavalues(),rowHeaders = NULL)
})
}