如何将 sliderinput 与 selectinput 相乘,其中结果显示为文本输出
How to multiply a sliderinput with a selectinput where result is shown as a text output
不熟悉使用 shinyapp 和不熟悉寻求帮助。
这是名为 Data1 的 df 示例:
+--------+-------+-----+-------+--------+-------------+
| Manu | Model | Des | Trans | Fuel | costpermile |
+--------+-------+-----+-------+--------+-------------+
| ABARTH | 1 | a | SAT5 | Diesel | 0.12 |
| ABARTH | 2 | b | 6AT | Petrol | 0.14 |
| ALFA | 3 | c | M6 | Petrol | 0.13 |
| ALFA | 4 | d | M6 | Petrol | 0.11 |
+--------+-------+-----+-------+--------+-------------+
我正在尝试创建一个 R shiny 应用程序,其中的列是选择输入。然后用户使用滑块输入来选择他们想要多少英里。我希望结果显示 [costpermile] * [sliderinput value] 作为文本输出。
到目前为止,我的代码给出了所有行的值,这不是我需要的。我不知道如何去获得一个价值。
示例场景:
人从下拉菜单中选择:ABARTH、1、a、SAT5、Diesel、0.12
然后在滑块中选择 100 英里
结果我想出现:(0.12*100) = 12
Code so far:
library(openxlsx)
library(googlesheets4)
library(tidyverse)
library(tidyr)
library(plyr)
library(dplyr)
library(googledrive)
library(googlesheets)
library(shiny)
ui <- fluidPage(
selectInput("Select1", "Manu", unique(Data1$Manu)),
selectInput("Select2", "Model", choices = NULL),
selectInput("Select3", "Des", choices= NULL),
selectInput("Select4", "Trans", choices= NULL),
selectInput("Select5", "Feul", choices= NULL),
selectInput("Select6", "costpermile", choices= NULL),
sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50),
textOutput( "total")
)
server <- function(input, output,session) {
observeEvent(input$Select1,{
updateSelectInput(session,'Select2',
choices=unique(Data1$Model[Data1$Manu==input$Select1]))
})
observeEvent(input$Select2,{
updateSelectInput(session,'Select3',
choices=unique(Data1$Des[Data1$Manu==input$Select1 &
Data1$Model==input$Select2]))
})
observeEvent(input$Select3,{
updateSelectInput(session,'Select4',
choices=unique(Data1$Trans[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3]))
})
observeEvent(input$Select4,{
updateSelectInput(session,'Select5',
choices=unique(Data1$Fuel[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3 &
Data1$Trans==input$Select4]))
})
observeEvent(input$Select5,{
updateSelectInput(session,'Select6',
choices=unique(Data1$costpermile[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3 &
Data1$Trans==input$Select4 &
Data1$Fuel == input$Select5
]))
})
output$out <- renderUI({
if (input$Select6 == TRUE){
sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50)
}})
values <- reactiveValues()
observe({values$complete <- Data1$costpermile * input$num
})
output$total <- renderText({ values$complete})
}
shinyApp( ui= ui, server= server)
您需要替换代码的最后一部分:
values <- reactiveValues()
observe({values$complete <- Data1$costpermile * input$num})
output$total <- renderText({ values$complete})
只有 output$total
如下:
output$total <- renderText({as.numeric(input$Select6) * input$num})
所以完整的代码应该是:
library(openxlsx)
library(tidyr)
library(plyr)
library(dplyr)
library(shiny)
Data1 = data.frame(Manu = c("ABARTH", "ABARTH", "ALFA", "ALFA"),
Model = c(1,2,3,4),
Des= c("a", "b", "c", "d"),
Trans = c("SAT5", "6AT", "M6", "M6"),
Fuel = c("Diesel", "Petrol", "Petrol", "Petrol"),
costpermile = c(0.12, 0.14, 0.13, 0.11))
ui <- fluidPage(
selectInput("Select1", "Manu", unique(Data1$Manu)),
selectInput("Select2", "Model", choices = NULL),
selectInput("Select3", "Des", choices= NULL),
selectInput("Select4", "Trans", choices= NULL),
selectInput("Select5", "Feul", choices= NULL),
selectInput("Select6", "costpermile", choices= NULL),
sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50),
textOutput( "total")
)
server <- function(input, output,session) {
observeEvent(input$Select1,{
updateSelectInput(session,'Select2',
choices=unique(Data1$Model[Data1$Manu==input$Select1]))
})
observeEvent(input$Select2,{
updateSelectInput(session,'Select3',
choices=unique(Data1$Des[Data1$Manu==input$Select1 &
Data1$Model==input$Select2]))
})
observeEvent(input$Select3,{
updateSelectInput(session,'Select4',
choices=unique(Data1$Trans[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3]))
})
observeEvent(input$Select4,{
updateSelectInput(session,'Select5',
choices=unique(Data1$Fuel[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3 &
Data1$Trans==input$Select4]))
})
observeEvent(input$Select5,{
updateSelectInput(session,'Select6',
choices=unique(Data1$costpermile[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3 &
Data1$Trans==input$Select4 &
Data1$Fuel == input$Select5
]))
})
output$out <- renderUI({
if (input$Select6 == TRUE){
sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50)
}})
output$total <- renderText({as.numeric(input$Select6) * input$num})
}
shinyApp( ui= ui, server= server)
不熟悉使用 shinyapp 和不熟悉寻求帮助。 这是名为 Data1 的 df 示例:
+--------+-------+-----+-------+--------+-------------+
| Manu | Model | Des | Trans | Fuel | costpermile |
+--------+-------+-----+-------+--------+-------------+
| ABARTH | 1 | a | SAT5 | Diesel | 0.12 |
| ABARTH | 2 | b | 6AT | Petrol | 0.14 |
| ALFA | 3 | c | M6 | Petrol | 0.13 |
| ALFA | 4 | d | M6 | Petrol | 0.11 |
+--------+-------+-----+-------+--------+-------------+
我正在尝试创建一个 R shiny 应用程序,其中的列是选择输入。然后用户使用滑块输入来选择他们想要多少英里。我希望结果显示 [costpermile] * [sliderinput value] 作为文本输出。
到目前为止,我的代码给出了所有行的值,这不是我需要的。我不知道如何去获得一个价值。
示例场景: 人从下拉菜单中选择:ABARTH、1、a、SAT5、Diesel、0.12 然后在滑块中选择 100 英里 结果我想出现:(0.12*100) = 12
Code so far:
library(openxlsx)
library(googlesheets4)
library(tidyverse)
library(tidyr)
library(plyr)
library(dplyr)
library(googledrive)
library(googlesheets)
library(shiny)
ui <- fluidPage(
selectInput("Select1", "Manu", unique(Data1$Manu)),
selectInput("Select2", "Model", choices = NULL),
selectInput("Select3", "Des", choices= NULL),
selectInput("Select4", "Trans", choices= NULL),
selectInput("Select5", "Feul", choices= NULL),
selectInput("Select6", "costpermile", choices= NULL),
sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50),
textOutput( "total")
)
server <- function(input, output,session) {
observeEvent(input$Select1,{
updateSelectInput(session,'Select2',
choices=unique(Data1$Model[Data1$Manu==input$Select1]))
})
observeEvent(input$Select2,{
updateSelectInput(session,'Select3',
choices=unique(Data1$Des[Data1$Manu==input$Select1 &
Data1$Model==input$Select2]))
})
observeEvent(input$Select3,{
updateSelectInput(session,'Select4',
choices=unique(Data1$Trans[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3]))
})
observeEvent(input$Select4,{
updateSelectInput(session,'Select5',
choices=unique(Data1$Fuel[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3 &
Data1$Trans==input$Select4]))
})
observeEvent(input$Select5,{
updateSelectInput(session,'Select6',
choices=unique(Data1$costpermile[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3 &
Data1$Trans==input$Select4 &
Data1$Fuel == input$Select5
]))
})
output$out <- renderUI({
if (input$Select6 == TRUE){
sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50)
}})
values <- reactiveValues()
observe({values$complete <- Data1$costpermile * input$num
})
output$total <- renderText({ values$complete})
}
shinyApp( ui= ui, server= server)
您需要替换代码的最后一部分:
values <- reactiveValues()
observe({values$complete <- Data1$costpermile * input$num})
output$total <- renderText({ values$complete})
只有 output$total
如下:
output$total <- renderText({as.numeric(input$Select6) * input$num})
所以完整的代码应该是:
library(openxlsx)
library(tidyr)
library(plyr)
library(dplyr)
library(shiny)
Data1 = data.frame(Manu = c("ABARTH", "ABARTH", "ALFA", "ALFA"),
Model = c(1,2,3,4),
Des= c("a", "b", "c", "d"),
Trans = c("SAT5", "6AT", "M6", "M6"),
Fuel = c("Diesel", "Petrol", "Petrol", "Petrol"),
costpermile = c(0.12, 0.14, 0.13, 0.11))
ui <- fluidPage(
selectInput("Select1", "Manu", unique(Data1$Manu)),
selectInput("Select2", "Model", choices = NULL),
selectInput("Select3", "Des", choices= NULL),
selectInput("Select4", "Trans", choices= NULL),
selectInput("Select5", "Feul", choices= NULL),
selectInput("Select6", "costpermile", choices= NULL),
sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50),
textOutput( "total")
)
server <- function(input, output,session) {
observeEvent(input$Select1,{
updateSelectInput(session,'Select2',
choices=unique(Data1$Model[Data1$Manu==input$Select1]))
})
observeEvent(input$Select2,{
updateSelectInput(session,'Select3',
choices=unique(Data1$Des[Data1$Manu==input$Select1 &
Data1$Model==input$Select2]))
})
observeEvent(input$Select3,{
updateSelectInput(session,'Select4',
choices=unique(Data1$Trans[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3]))
})
observeEvent(input$Select4,{
updateSelectInput(session,'Select5',
choices=unique(Data1$Fuel[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3 &
Data1$Trans==input$Select4]))
})
observeEvent(input$Select5,{
updateSelectInput(session,'Select6',
choices=unique(Data1$costpermile[Data1$Manu==input$Select1 &
Data1$Model==input$Select2 &
Data1$Des==input$Select3 &
Data1$Trans==input$Select4 &
Data1$Fuel == input$Select5
]))
})
output$out <- renderUI({
if (input$Select6 == TRUE){
sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50)
}})
output$total <- renderText({as.numeric(input$Select6) * input$num})
}
shinyApp( ui= ui, server= server)