assigning multiple values to (one) shiny radio button

1.1k Views Asked by At

I'm trying to build a shiny app using the dashboardPage ui and I would like to have my radio button control two different values.

In a bit more detail:

Considering I have a df such as this:

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")

My idea is/was to use radio buttons that would be called "daily" and "weekly" and I'd like to 'call' two possible columns/values per button (high and low) that could be displayed (in valueBoxes) seperately of one another.

What works is the combo of this:

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

and this:

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

What I would like is something like the following:

Note: This doesn't work (for me), but hopefully it displays what I want to do.

Edit: The code below creates extra buttons. So strictly speaking it does work, but it's not what I want.

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

To work with something like this:

    # 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')
  })

Thanks

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

Current code:

# =================================================== #
# ====== #
#   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

There are 1 best solutions below

0
On BEST ANSWER

This would hopefully give you the output you want:

# =================================================== #
# ====== #
#   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)