我正在尝试制作一个可以读取.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
感谢任何帮助!!谢谢!
答案 0 :(得分:0)
如果应用程序运行时csv中的数据没有动态更改,那么我会将read.csv移至标头,以便在应用程序启动时将其一次加载到内存中。
library(shiny)
library(shinydashboard)
library(shinyWidgets)
ui = dashboardPage(
dashboardHeader(title = "Selection"),
dashboardSidebar(disable = TRUE),
dashboardBody(uiOutput("body")))
df <- read.csv("selection.csv", header = T, sep = "," , check.names = FALSE, col.names = TRUE)
server = function (input, output, session) { #### ...rest of your app code
您嵌套的ifelse代码很复杂-尝试在csv中重命名数据可能会更容易,以便于子集设置,例如
output <- df[input1, input2]
...并将该子设置操作的输出传递到UI。