如何使用 for 循环和 ifs return 数据框中的值与闪亮服务器中的输入值匹配
How to return a value from a data frame using a for loop and ifs to match with input values in shiny server
我已经为闪亮的应用设置了 ui 和服务器。我还有一个数据框我的变量集。我正在尝试使用 if 语句遍历行数据框,以将我选择的输入与数据框中的列相匹配。然后 return 最后一列中的值 (result/score)。
我为此 post 重新创建了一个更小的简化版本的代码和数据集。
我尝试使用过滤器来过滤掉数据框中的条件和 return 我想要的值。我也尝试过单独使用 if 语句但无济于事。
我的数据集的列数和行数比下面创建的数据集多,但为简单起见:
z <- c("Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled")
y <- c("Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable")
x <- c("A", "A","A","A","A","A", "B", "B", "B", "B", "B", "B", "C", "C","C","C","C","C", "D", "D","D","D","D","D")
result <- runif(24, 50, 500)
Ui.R:
library("shiny")
shinyUI(
pageWithSidebar(
headerPanel("Demo")
,
sidebarPanel(
wellPanel(
selectInput(
inputId= 'x',
label= "Country of Origin",
choices= list('A', 'B', 'C', 'D'),
selected='A',
multiple= FALSE),
selectInput(
inputId= 'y',
label= "Product",
choices= list("Fruit", "Vegetable"),
selected='Fruit',
multiple= FALSE),
selectInput(
inputId= 'z',
label= "Quality",
choices = list("Perfect", "Edible", "Spoiled"),
multiple= FALSE)
)
)
,
mainPanel(
wellPanel(
textOutput("score")))))
Server.R:
shinyServer(
function(input,output,session){
score <- reactive({
for (row in 1:nrow(df())) {
if(input$x == df$x[row] && input$y == df$y[row] && input$z == df$z){
score$result <- df$result[row]
}
}
})
output$score <- renderText({
score$result})
})
我希望在应用程序的主面板中显示相应的结果分数,该面板会根据所选输入进行更新。目前我发现我根本没有输出。
如有任何帮助,我们将不胜感激。
我不确定,但您是否正在尝试执行以下操作(请参阅下面的代码)。在这种情况下,您不需要 for 循环。
library("shiny")
library("dplyr")
z <- c("Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled")
y <- c("Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable")
x <- c("A", "A","A","A","A","A", "B", "B", "B", "B", "B", "B", "C", "C","C","C","C","C", "D", "D","D","D","D","D")
df = data.frame(x, y, z, result = runif(24, 50, 500), stringsAsFactors = FALSE)
shinyApp(
ui = shinyUI(
pageWithSidebar(
headerPanel("Demo")
,
sidebarPanel(
wellPanel(
selectInput(
inputId= 'x',
label= "Country of Origin",
choices= list('A', 'B', 'C', 'D'),
selected='A',
multiple= FALSE),
selectInput(
inputId= 'y',
label= "Product",
choices= list("Fruit", "Vegetable"),
selected='Fruit',
multiple= FALSE),
selectInput(
inputId= 'z',
label= "Quality",
choices = list("Perfect", "Edible", "Spoiled"),
multiple= FALSE)
)
)
,
mainPanel(
wellPanel(
textOutput("score"))))),
# Server ------
server = function(input, output, session){
score <- reactive({
df %>%
filter(x == input$x,
y == input$y,
z == input$z)
})
output$score <- renderText({
score()$result
})
}
) # Closes ShinyApp
我已经为闪亮的应用设置了 ui 和服务器。我还有一个数据框我的变量集。我正在尝试使用 if 语句遍历行数据框,以将我选择的输入与数据框中的列相匹配。然后 return 最后一列中的值 (result/score)。
我为此 post 重新创建了一个更小的简化版本的代码和数据集。
我尝试使用过滤器来过滤掉数据框中的条件和 return 我想要的值。我也尝试过单独使用 if 语句但无济于事。
我的数据集的列数和行数比下面创建的数据集多,但为简单起见:
z <- c("Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled")
y <- c("Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable")
x <- c("A", "A","A","A","A","A", "B", "B", "B", "B", "B", "B", "C", "C","C","C","C","C", "D", "D","D","D","D","D")
result <- runif(24, 50, 500)
Ui.R:
library("shiny")
shinyUI(
pageWithSidebar(
headerPanel("Demo")
,
sidebarPanel(
wellPanel(
selectInput(
inputId= 'x',
label= "Country of Origin",
choices= list('A', 'B', 'C', 'D'),
selected='A',
multiple= FALSE),
selectInput(
inputId= 'y',
label= "Product",
choices= list("Fruit", "Vegetable"),
selected='Fruit',
multiple= FALSE),
selectInput(
inputId= 'z',
label= "Quality",
choices = list("Perfect", "Edible", "Spoiled"),
multiple= FALSE)
)
)
,
mainPanel(
wellPanel(
textOutput("score")))))
Server.R:
shinyServer(
function(input,output,session){
score <- reactive({
for (row in 1:nrow(df())) {
if(input$x == df$x[row] && input$y == df$y[row] && input$z == df$z){
score$result <- df$result[row]
}
}
})
output$score <- renderText({
score$result})
})
我希望在应用程序的主面板中显示相应的结果分数,该面板会根据所选输入进行更新。目前我发现我根本没有输出。
如有任何帮助,我们将不胜感激。
我不确定,但您是否正在尝试执行以下操作(请参阅下面的代码)。在这种情况下,您不需要 for 循环。
library("shiny")
library("dplyr")
z <- c("Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled", "Perfect", "Edible", "Spoiled")
y <- c("Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable", "Fruit", "Fruit", "Fruit","Vegetable", "Vegetable", "Vegetable")
x <- c("A", "A","A","A","A","A", "B", "B", "B", "B", "B", "B", "C", "C","C","C","C","C", "D", "D","D","D","D","D")
df = data.frame(x, y, z, result = runif(24, 50, 500), stringsAsFactors = FALSE)
shinyApp(
ui = shinyUI(
pageWithSidebar(
headerPanel("Demo")
,
sidebarPanel(
wellPanel(
selectInput(
inputId= 'x',
label= "Country of Origin",
choices= list('A', 'B', 'C', 'D'),
selected='A',
multiple= FALSE),
selectInput(
inputId= 'y',
label= "Product",
choices= list("Fruit", "Vegetable"),
selected='Fruit',
multiple= FALSE),
selectInput(
inputId= 'z',
label= "Quality",
choices = list("Perfect", "Edible", "Spoiled"),
multiple= FALSE)
)
)
,
mainPanel(
wellPanel(
textOutput("score"))))),
# Server ------
server = function(input, output, session){
score <- reactive({
df %>%
filter(x == input$x,
y == input$y,
z == input$z)
})
output$score <- renderText({
score()$result
})
}
) # Closes ShinyApp