Shiny 中的依赖 SelectInput 和 dataTableOutput
Dependent SelectInput and dataTableOutput in Shiny
我正在构建一个闪亮的应用程序,但我被卡住了。下面是问题的描述。非常感谢任何帮助。
我想完成两件事。
- 在我的应用程序中有多个依赖的 selectInput,完成了!
- 除了多个依赖的selectInput,我在应用程序中还有多个numericInput。所以我想编译这些列并写成 table 或 dataTable 到 UI.
下面是一个代表。
library(shiny)
library(shinythemes)
ui <- navbarPage("FET Tracker", theme = shinytheme("flatly"),
tabPanel("REARCOM",
fluidPage("REARCOM",
sidebarLayout(
sidebarPanel(width = 2,
dateInput("racomreadingdate", "Meter Reading Date"),
htmlOutput("RRNo_Selector"),
numericInput("prevreading", label = "Previous Meter Reading:", value = 1, min = 0),
numericInput("currreading", label = "Current Meter Reading:", value = 1, min = 0),
htmlOutput("unitsconsumed"),
numericInput("amount", "Enter the Amount (INR):", min = 0, value = 0),
htmlOutput("Location_Selector"),
htmlOutput("sanctionedHP"),
htmlOutput("transmission"),
actionButton("eupdate", label = "Tabulate")
),
mainPanel(dataTableOutput("racomtable"))
)))
)
server <- function(input, output, session) {
racomData <- read.table(
text = "RRNo Location SanctionedHP
RAMEH75 Canteen_01 LT-A
RAMLG2438 Canteen_02 LT-B
RAMLG2941 Canteen_03 LT-C
RAMLG6124 Canteen_04 LT-D
RAMLG6596 Canteen_05 LT-A
RAMP10893 Canteen_06 LT-B
RAMP10894 Canteen_07 LT-C
RAMP12472 Canteen_08 LT-D
RAMP12473 Canteen_09 LT-A
RAMP12474 Canteen_10 LT-B
RAMP15086 Canteen_11 LT-C
RAMP15087 Canteen_12 LT-D
RAMP4646 Canteen_13 LT-A
RAMP4647 Canteen_14 LT-B
RAMP8632 Canteen_15 LT-C
RAMP9223 Canteen_16 LT-D
RAMP9225 Canteen_17 LT-A
RAMP9226 Canteen_18 LT-B
RAMP9825 Canteen_19 LT-C
RAMP9906 Canteen_20 LT-D
RAM12247 Canteen_21 LT-A
RAM4278 Canteen_22 LT-B
RAM4646 Canteen_23 LT-C
RAM4647 Canteen_24 LT-D
RAM4937 Canteen_25 LT-A
RAM5693 Canteen_26 LT-B",
header = TRUE,
stringsAsFactors = FALSE)
output$RRNo_Selector <- renderUI({
selectInput(
inputId = "rrno",
label = "Revenue Register No (RR No):",
choices = as.character(unique(racomData$RRNo)),
selected = " ")
})
output$Location_Selector <- renderUI({
available <- racomData[racomData$RRNo == input$rrno, "Location"]
selectInput(
inputId = "location",
label = "Location:",
choices = unique(available),
selected = unique(available)[1])
})
output$sanctionedHP <- renderUI({
available2 <- racomData[racomData$Location == input$location, "SanctionedHP"]
selectInput(
inputId = "sanctionedHP",
label = "Sanctioned HP:",
choices = unique(available2),
selected = unique(available2)[1]
)
})
output$unitsconsumed <- renderUI({
numericInput("UnitsConsumed","Number of Units Consumed:", value = (input$currreading - input$prevreading))
})
values <- reactiveValues()
values$df <- data.frame(RRNo = character(),
Location = character(),
Sanctioned_HP = character(),
Reading_Date = character(),
Current_Reading = character(),
Previous_Reading = character(),
Units_Consumed = character(),
Amount = character())
observeEvent(input$eupdate, {
new_row <- data.frame(
#RRNo = input$RRNo_Selector,
#Location = input$location,
#Sanctioned_HP = input$sanctionedHP,
Reading_Date = strftime(input$racomreadingdate, "%d-%m-%Y"),
Current_Reading = input$currreading,
Previous_Reading = input$prevreading,
#Units_Consumed = input$unitsconsumed,
Amount = input$amount)
values$df <- rbind(values$df, new_row)
})
output$racomtable <- renderDataTable(values$df)
}
shinyApp(ui, server)
如果 observeEvent 块中的注释行未被注释,则该应用程序无法运行
observeEvent(input$eupdate, {
new_row <- data.frame(
#RRNo = input$RRNo_Selector,
#Location = input$location,
#Sanctioned_HP = input$sanctionedHP,
Reading_Date = strftime(input$racomreadingdate, "%d-%m-%Y"),
Current_Reading = input$currreading,
Previous_Reading = input$prevreading,
#Units_Consumed = input$unitsconsumed,
Amount = input$amount)
values$df <- rbind(values$df, new_row)
})
output$racomtable <- renderDataTable(values$df)
}
您应确保使用正确的输入 ID。试试这个
newrow <- eventReactive(input$eupdate, {
data.frame(
RRNo = input$rrno,
Location = input$location,
Sanctioned_HP = input$sanctionedHP,
Reading_Date = strftime(input$racomreadingdate, "%d-%m-%Y"),
Current_Reading = input$currreading,
Previous_Reading = input$prevreading,
Units_Consumed = input$UnitsConsumed,
Amount = input$amount)
})
observeEvent(input$eupdate, {
values$df <- rbind(values$df, newrow())
})
output$racomtable <- renderDataTable(values$df)
我正在构建一个闪亮的应用程序,但我被卡住了。下面是问题的描述。非常感谢任何帮助。
我想完成两件事。
- 在我的应用程序中有多个依赖的 selectInput,完成了!
- 除了多个依赖的selectInput,我在应用程序中还有多个numericInput。所以我想编译这些列并写成 table 或 dataTable 到 UI.
下面是一个代表。
library(shiny)
library(shinythemes)
ui <- navbarPage("FET Tracker", theme = shinytheme("flatly"),
tabPanel("REARCOM",
fluidPage("REARCOM",
sidebarLayout(
sidebarPanel(width = 2,
dateInput("racomreadingdate", "Meter Reading Date"),
htmlOutput("RRNo_Selector"),
numericInput("prevreading", label = "Previous Meter Reading:", value = 1, min = 0),
numericInput("currreading", label = "Current Meter Reading:", value = 1, min = 0),
htmlOutput("unitsconsumed"),
numericInput("amount", "Enter the Amount (INR):", min = 0, value = 0),
htmlOutput("Location_Selector"),
htmlOutput("sanctionedHP"),
htmlOutput("transmission"),
actionButton("eupdate", label = "Tabulate")
),
mainPanel(dataTableOutput("racomtable"))
)))
)
server <- function(input, output, session) {
racomData <- read.table(
text = "RRNo Location SanctionedHP
RAMEH75 Canteen_01 LT-A
RAMLG2438 Canteen_02 LT-B
RAMLG2941 Canteen_03 LT-C
RAMLG6124 Canteen_04 LT-D
RAMLG6596 Canteen_05 LT-A
RAMP10893 Canteen_06 LT-B
RAMP10894 Canteen_07 LT-C
RAMP12472 Canteen_08 LT-D
RAMP12473 Canteen_09 LT-A
RAMP12474 Canteen_10 LT-B
RAMP15086 Canteen_11 LT-C
RAMP15087 Canteen_12 LT-D
RAMP4646 Canteen_13 LT-A
RAMP4647 Canteen_14 LT-B
RAMP8632 Canteen_15 LT-C
RAMP9223 Canteen_16 LT-D
RAMP9225 Canteen_17 LT-A
RAMP9226 Canteen_18 LT-B
RAMP9825 Canteen_19 LT-C
RAMP9906 Canteen_20 LT-D
RAM12247 Canteen_21 LT-A
RAM4278 Canteen_22 LT-B
RAM4646 Canteen_23 LT-C
RAM4647 Canteen_24 LT-D
RAM4937 Canteen_25 LT-A
RAM5693 Canteen_26 LT-B",
header = TRUE,
stringsAsFactors = FALSE)
output$RRNo_Selector <- renderUI({
selectInput(
inputId = "rrno",
label = "Revenue Register No (RR No):",
choices = as.character(unique(racomData$RRNo)),
selected = " ")
})
output$Location_Selector <- renderUI({
available <- racomData[racomData$RRNo == input$rrno, "Location"]
selectInput(
inputId = "location",
label = "Location:",
choices = unique(available),
selected = unique(available)[1])
})
output$sanctionedHP <- renderUI({
available2 <- racomData[racomData$Location == input$location, "SanctionedHP"]
selectInput(
inputId = "sanctionedHP",
label = "Sanctioned HP:",
choices = unique(available2),
selected = unique(available2)[1]
)
})
output$unitsconsumed <- renderUI({
numericInput("UnitsConsumed","Number of Units Consumed:", value = (input$currreading - input$prevreading))
})
values <- reactiveValues()
values$df <- data.frame(RRNo = character(),
Location = character(),
Sanctioned_HP = character(),
Reading_Date = character(),
Current_Reading = character(),
Previous_Reading = character(),
Units_Consumed = character(),
Amount = character())
observeEvent(input$eupdate, {
new_row <- data.frame(
#RRNo = input$RRNo_Selector,
#Location = input$location,
#Sanctioned_HP = input$sanctionedHP,
Reading_Date = strftime(input$racomreadingdate, "%d-%m-%Y"),
Current_Reading = input$currreading,
Previous_Reading = input$prevreading,
#Units_Consumed = input$unitsconsumed,
Amount = input$amount)
values$df <- rbind(values$df, new_row)
})
output$racomtable <- renderDataTable(values$df)
}
shinyApp(ui, server)
如果 observeEvent 块中的注释行未被注释,则该应用程序无法运行
observeEvent(input$eupdate, {
new_row <- data.frame(
#RRNo = input$RRNo_Selector,
#Location = input$location,
#Sanctioned_HP = input$sanctionedHP,
Reading_Date = strftime(input$racomreadingdate, "%d-%m-%Y"),
Current_Reading = input$currreading,
Previous_Reading = input$prevreading,
#Units_Consumed = input$unitsconsumed,
Amount = input$amount)
values$df <- rbind(values$df, new_row)
})
output$racomtable <- renderDataTable(values$df)
}
您应确保使用正确的输入 ID。试试这个
newrow <- eventReactive(input$eupdate, {
data.frame(
RRNo = input$rrno,
Location = input$location,
Sanctioned_HP = input$sanctionedHP,
Reading_Date = strftime(input$racomreadingdate, "%d-%m-%Y"),
Current_Reading = input$currreading,
Previous_Reading = input$prevreading,
Units_Consumed = input$UnitsConsumed,
Amount = input$amount)
})
observeEvent(input$eupdate, {
values$df <- rbind(values$df, newrow())
})
output$racomtable <- renderDataTable(values$df)