【发布时间】:2020-08-28 13:53:12
【问题描述】:
我正在尝试制作一个闪亮的应用程序,它可以读取 .csv 文件并在应用程序中显示特定数据。例如,如果我选择数字 3 并键入 2,则应用程序中将显示“H”。
以下是示例代码:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
ui = dashboardPage(
dashboardHeader(title = "Selection"),
dashboardSidebar(disable = TRUE),
dashboardBody(uiOutput("body")))
server = function (input, output, session) {
output$body <- renderUI({
box(numericInput('number', "Choose a number:", value = NULL, min = 1, max = 5),
br(),
selectInput('type', "Choose a type:", choices = list("Type 1", "Type 2")),
br(),
textOutput("my_selection"),
br()
)})
selection_table <- reactive({read.csv("selection.csv", header = T, sep = "," , check.names = FALSE, col.names = TRUE)})
output$my_selection = reactive({
selection = ifelse(input$number == 1 & input$type == "Type 1", selection_table()$selection_1[1],
ifelse(input$number == 2 & input$type == "Type 1", selection_table()$selection_1[2],
ifelse(input$number == 3 & input$type == "Type 1", selection_table()$selection_1[3],
ifelse(input$number == 4 & input$type == "Type 1", selection_table()$selection_1[4],
ifelse(input$number == 5 & input$type == "Type 1", selection_table()$selection_1[5],
ifelse(input$number == 1 & input$type == "Type 2", selection_table()$selection_2[1],
ifelse(input$number == 2 & input$type == "Type 2", selection_table()$selection_2[2],
ifelse(input$number == 3 & input$type == "Type 2", selection_table()$selection_2[3],
ifelse(input$number == 4 & input$type == "Type 2", selection_table()$selection_2[4],
ifelse(input$number == 5 & input$type == "Type 2", selection_table()$selection_2[5]))))))))))
selection
})
}
shinyApp(ui,server)
这是 .csv 文件中的示例数据:
age selection_1 selection_2
1 A F
2 B G
3 C H
4 D I
5 E J
感谢任何帮助!谢谢!
【问题讨论】: