如果闪亮服务器中的条件
If condition in Shiny server
我正在尝试根据输入操作数据框。这是我的代码:
library(shiny)
library(quantmod)
ui <- fluidPage(
plotOutput("chart", click = "SD1"),
radioButtons(
"term",
"Term",
choices = c("Daily", "Weekly", "Monthly"),
))
server <- function(input, output){
df1 <- reactive(getSymbols("JPM", src = "google", auto.assign = F))
output$chart <- renderPlot(
if (input$term == "Weekly") {
df <- to.weekly(df1())
}
else if (input$term == "Monthly") {
df <- to.monthly(df1())
}
else {
df <- df1()
}
chartSeries(
df()
)
)
}
shinyApp(ui, server)
那么为什么我的 if 条件不起作用?非常感谢!
我认为您混淆了括号,这应该可以完成工作。如果你想使用你的子集数据并仍然保持对原始数据的访问,你可以创建两个反应:一个你可以使用 df1()
访问,另一个是 df()
library(shiny)
library(quantmod)
ui <- fluidPage(
plotOutput("chart", click = "SD1"),
radioButtons(
"term",
"Term",
choices = c("Daily", "Weekly", "Monthly"),
))
server <- function(input, output){
df1 <- reactive({getSymbols("JPM", src = "google", auto.assign = F)})
df <-reactive({
if (input$term == "Weekly") {
df <- to.weekly(df1())
}
else if (input$term == "Monthly") {
df <- to.monthly(df1())
}
else {
df <- df1()
}
return(df)
})
output$chart <- renderPlot({
chartSeries(df())
})
}
shinyApp(ui, server)
我正在尝试根据输入操作数据框。这是我的代码:
library(shiny)
library(quantmod)
ui <- fluidPage(
plotOutput("chart", click = "SD1"),
radioButtons(
"term",
"Term",
choices = c("Daily", "Weekly", "Monthly"),
))
server <- function(input, output){
df1 <- reactive(getSymbols("JPM", src = "google", auto.assign = F))
output$chart <- renderPlot(
if (input$term == "Weekly") {
df <- to.weekly(df1())
}
else if (input$term == "Monthly") {
df <- to.monthly(df1())
}
else {
df <- df1()
}
chartSeries(
df()
)
)
}
shinyApp(ui, server)
那么为什么我的 if 条件不起作用?非常感谢!
我认为您混淆了括号,这应该可以完成工作。如果你想使用你的子集数据并仍然保持对原始数据的访问,你可以创建两个反应:一个你可以使用 df1()
访问,另一个是 df()
library(shiny)
library(quantmod)
ui <- fluidPage(
plotOutput("chart", click = "SD1"),
radioButtons(
"term",
"Term",
choices = c("Daily", "Weekly", "Monthly"),
))
server <- function(input, output){
df1 <- reactive({getSymbols("JPM", src = "google", auto.assign = F)})
df <-reactive({
if (input$term == "Weekly") {
df <- to.weekly(df1())
}
else if (input$term == "Monthly") {
df <- to.monthly(df1())
}
else {
df <- df1()
}
return(df)
})
output$chart <- renderPlot({
chartSeries(df())
})
}
shinyApp(ui, server)