将多个值设为(一)闪亮的单选按钮

时间:2016-12-29 12:06:28

标签: r input shiny shinydashboard

我正在尝试使用dashboardPage ui构建一个闪亮的应用程序,我想让我的单选按钮控制两个不同的值。

更详细一点:

考虑到我有这样的df:

mydata <- read.table(header=TRUE, text="
      dailyhigh   dailylow weeklyhigh weeklylow
      3.173455 0.44696251   2.520812 0.9406211
      2.923370 1.60416341   3.481743 0.9520305
      2.584739 0.05719436   4.534701 0.6622959")

我的想法是/是使用将被称为&#34;每天&#34;和&#34;每周&#34;而且我想打电话给&#39;每个按钮(高和低)两个可能的列/值,可以单独显示(在valueBoxes中)。

这有什么作用:

radioButtons("radio", "Choose Period:",
            c("Last 24 hours" = "dailyhigh",
              "Last 7 days" = "weeklyhigh"))

和此:

output$high <- renderValueBox({ 
    h = subset(mydata,select = input$radio)
    valueBox(
      round(h[3,1],3),"High", col='green')
  })

我想要的是以下内容:

注意:这对我来说不起作用,但希望它能显示我想要做的事情。

修改:下面的代码会创建额外的按钮。所以严格来说它确实有效,但它不是我想要的。

    radioButtons("radio", "Choose Period:",
                    c("Last 24 hours" = c("dailyhigh","dailylow"),
                      "Last 7 days" = c("weeklyhigh","weeklylow")))

使用这样的东西:

    # select the daily or weekly high
#=======================================

  output$high <- renderValueBox({
    h = subset(mydata,select = input$radio[1])
    valueBox(
      round(h[3,1],3),"High", col='green')
  })


    # select the daily or weekly low
#=======================================

  output$low <- renderValueBox({
    l = subset(mydata,select = input$radio[2])
    valueBox(
      round(l[3,1],3), "Low", col='red')
  })

感谢

=============================================== ===================

当前代码:

# =================================================== #
# ====== #
#   Shiny Graph Examples  #
# ===== #
# =================================================== #

# ===== #
# Packages, Libraries and Source Code
# ===== #

# === Libraries
require(shiny)
require(shinydashboard)


# === Data
mydata <- read.table(header=TRUE, text="
  dailyhigh   dailylow weeklyhigh weeklylow
                     3.173455 0.44696251   2.520812 0.9406211
                     2.923370 1.60416341   3.481743 0.9520305
                     2.584739 0.05719436   4.534701 0.6622959
                     ")


###START THE APP
# ======================
ui <- dashboardPage( 
  skin="yellow",
  dashboardHeader(
   #title="Playing with Sentiment Data",
   #titleWidth = 450
  ),
  dashboardSidebar(

    radioButtons("radio", "Choose Period:",
                 c(
                   "Last 24 hours" = "dailyhigh",
                   "Last 7 days" = "weeklyhigh"))
  ),
  dashboardBody(
    #boxes to be put in a row (or column)
    fluidRow(
      valueBoxOutput("high"),
      valueBoxOutput("low")
    )
  )
)

server <- function(input, output) { 

  output$high <- renderValueBox({
    h = subset(mydata,select = input$radio)
    valueBox(
      round(h[3,1],3),"High", col='green')
  })

  output$low <- renderValueBox({
    l = subset(mydata,select = input$radio)
    valueBox(
      round(l[3,1],3), "Low", col='red')
  })

}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

这有望为您提供所需的输出:

# =================================================== #
# ====== #
#   Shiny Graph Examples  #
# ===== #
# =================================================== #

# ===== #
# Packages, Libraries and Source Code
# ===== #

# === Libraries
require(shiny)
require(shinydashboard)


# === Data
mydata <- read.table(header=TRUE, text="
                     dailyhigh   dailylow weeklyhigh weeklylow
                     3.173455 0.44696251   2.520812 0.9406211
                     2.923370 1.60416341   3.481743 0.9520305
                     2.584739 0.05719436   4.534701 0.6622959
                     ")


###START THE APP
# ======================
ui <- dashboardPage( 
  skin="yellow",
  dashboardHeader(
    #title="Playing with Sentiment Data",
    #titleWidth = 450
  ),
  dashboardSidebar(

    radioButtons("radio", "Choose Period:",
                 c(
                   "Last 24 hours" = "daily",
                   "Last 7 days" = "weekly"))
  ),
  dashboardBody(
    #boxes to be put in a row (or column)
    fluidRow(
      valueBoxOutput("high"),
      valueBoxOutput("low")
    )
  )
)

server <- function(input, output) { 

  output$high <- renderValueBox({
    h = subset(mydata,select = paste(input$radio,"high",sep = ""))
    valueBox(
      round(h[3,1],3),"High", col='green')
  })

  output$low <- renderValueBox({
    l = subset(mydata,select = paste(input$radio,"low",sep = ""))
    valueBox(
      round(l[3,1],3), "Low", col='red')
  })

}

shinyApp(ui, server)