r - 基于多个用户输入的闪亮分配数据框
问题描述
我正在尝试根据 Shiny 应用程序中的一些用户输入来分配数据框和列表(稍后在代码中的计算中使用)。其中一项作业依赖于两个用户输入 - 我将如何做到这一点?我试图附上一些可重现的代码......
library(shiny)
set.seed(4)
# lists and dataframes to be assigned based on user inputs
A <- list(rnorm(6), rnorm(6))
B <- list(rnorm(6), rnorm(6))
dfA <- as.data.frame(cbind(rnorm(6), rnorm(6)))
dfA_adj <- as.data.frame(cbind(rnorm(6), rnorm(6)))
dfB <- as.data.frame(cbind(rnorm(6), rnorm(6)))
dfB_adj <- as.data.frame(cbind(rnorm(6), rnorm(6)))
ui <- fluidPage(
titlePanel(strong("Title")),
sidebarLayout(
sidebarPanel(
#content
h4(strong("Select data sets to use in calculations:")),
selectInput('L', 'Select list to use', c("List A" = 'a', "List B" = 'b')),
selectInput('P','Select Calculation method', c("Adjusted" = 'Adj', "Standard" = 'St'))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
#assign dataframes and list to use in code calcs (not shown) based on user inputs
pts <- reactive({ switch(input$L, "a" = A, "b" = B) })
PET <- reactive({ switch(c(input$L, input$P),
c("a", "Adj") = dfA_adj,
c("a", "St") = dfA,
c("b", "Adj") = dfB_adj,
c("b", "St") = dfB })
output$table <- renderTable(PET())
}
shinyApp(ui = ui, server = server)
解决方案
我们可以使用 if 语句:
library(shiny)
set.seed(4)
# lists and dataframes to be assigned based on user inputs
A <- list(rnorm(6), rnorm(6))
B <- list(rnorm(6), rnorm(6))
dfA <- as.data.frame(cbind(rnorm(6), rnorm(6)))
dfA_adj <- as.data.frame(cbind(rnorm(6), rnorm(6)))
dfB <- as.data.frame(cbind(rnorm(6), rnorm(6)))
dfB_adj <- as.data.frame(cbind(rnorm(6), rnorm(6)))
ui <- fluidPage(
titlePanel(strong("Title")),
sidebarLayout(
sidebarPanel(
#content
h4(strong("Select data sets to use in calculations:")),
selectInput('L', 'Select list to use', choices = c("List A" = 'A', "List B" = 'B')),
selectInput('P','Select Calculation method', choices = c("Adjusted", "Standard"))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output, session) {
PET <- reactive({
if (input$L == "A" && input$P == "Adjusted") {
dfA_adj}
else if (input$L == "A" && input$P == "Standard") {
dfA
} else if (input$L == "B" && input$P == "Adjusted") {
dfB_adj
} else {
dfB
}
})
output$table <- renderTable({PET()})
}
shinyApp(ui = ui, server = server)
推荐阅读
- vue.js - 如何使用 NUXT.js 在mounted() 方法中访问存储?
- typescript - 一种在 TypeScript 中删除子子属性的接口的方法
- postgresql - postgresql 无法登录到 postgres 管理员用户并且将身份验证设置为信任没有帮助(MacOS)
- python - 验证列表会产生不正确的结果
- c# - 如何在我的代码中传递共享点凭据?
- java - Spring Batch 电子邮件监听器
- kubernetes - 使用 prometheus-alertmanager 通过 Grafana UI 发送警报电子邮件
- javascript - ECMAScript 6 internet explorer 11 - 我必须重新做我的代码吗?
- python - 在循环中将列添加到 DataFrame
- acumatica - 如何通过 Acumatica REST API 获取所有联系人