r - 将闪亮的 DT 单元格值传递给函数
问题描述
我有一个使用 function1 和用户输入构建的数据表。然后,我想使用此数据表中的单元格值作为 function2 的输入来构建另一个数据表。我收到过滤器错误,并认为问题与我选择单元格值的方式有关。我在这里构建一个现有的 SO 解决方案。
假设我们有 iris 数据并且 function1 使用用户输入来过滤物种。function1 的结果是响应式调用的结果。现在 function2 将此结果作为输入,并使用选定的“单元格”值 (Sepal.Length) 进一步过滤和构建另一个数据表。
感谢@Silentdevildoll 的评论,table2 的构造正确(我认为),但我仍然看到这个错误:
警告:错误:filter()
输入有问题..1
。i 输入..1
是Sepal.Length == var
。x 输入..1
的大小必须为 50 或 1,而不是 0。
library(shiny)
library(DT)
ui <- fluidPage(
mainPanel(
fluidRow(
column(4, textInput('textInput1', label=NULL)),
column(1, actionButton('button1', label=NULL))
),
fluidRow(
column(8, DT::dataTableOutput("table1")),
column(4, DT::dataTableOutput("table2"))
)
)
)
server <- function(input, output, session){
data(iris)
function1 <- function(id){
return(dplyr::filter(iris, Species==id))
}
function2 <- function(df, var){
return(dplyr::filter(df, Sepal.Length==var))
}
data <- reactiveValues()
observeEvent(input$button1, { data$ID <- input$textInput1 })
table1_rec <- reactive({ function1(data$ID) })
output$table1 <- DT::renderDataTable({
req(data$ID)
DT::datatable(table1_rec(), selection = list(target = 'cell'))
})
table2_rec <- reactive({
selected = table1_rec()[input$table1_cells_selected]
function2(table1_rec(), selected)
})
output$table2 <- DT::renderDataTable({
DT::datatable(table2_rec())
})
}
shinyApp(ui, server)
解决方案
推荐阅读
- reactjs - 如果我不想再在下拉列表中过滤,如何返回默认产品列表
- laravel - 无法在 Laravel Homestead 上安装 Socialite:内存限制错误
- automated-tests - Cy.click 失败,因为目标元素被禁用或其部分可见
- django - 无法通过 group_send 向 Django 频道发送消息
- scala - http4s 中的自定义普罗米修斯指标
- java - org.mongodb.driver.connection : 连接池后台维护任务 com.mongodb.MongoSocketOpenException 抛出异常
- c# - C# WriteAllText 仅以编码 UTF8 格式写入
- java - Tomcat 服务器无法启动
- c++ - Grpc ssl 服务器崩溃 - C++
- python - 无法在导致 OOM 的 tensorflow keras 模型中提供有效的批量大小