dataTable output within box() in R Shiny: sizing not reactive - r

I have a dataTable within a box() in my R Shiny app.
When I change the size of the page, the size of the dataTable doesn't change to stay within its box. My plot outputs within the same box have no problem adjusting size, but the data table does.
Thoughts on fixing the data table width?
Here's some code using R's mpg data set to demonstrate my UI issue. Play around with size of window to see the sizing issue I'm referring to.
library(shiny)
library(shinydashboard)
library(data.table)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(width = 325),
dashboardBody(
fluidPage(
box( width = 12,
tabsetPanel(
tabPanel("Summary",
dataTableOutput("coeffTable"))
)
)
)))
server <- function(input, output){
data<-mpg
output$coeffTable<-renderDataTable({
data.table(data[,1:2])
},options = list(lengthMenu = c(5, 10, -1), pageLength = 5))
}
shinyApp(ui = ui, server = server)

This works with the DT package. To use like this:
library(DT)
output$coeffTable <- DT::renderDataTable({...
DT::dataTableOutput("coeffTable")

Related

dataTableOutput fit column size

Building a Shiny Dashboard where I need to render a matrix.
For that I plan to use a DT::renderDataTable.
Everything is inside a FluidRow with a column size of 3 (out of 12).
The DataTable spread out of the defined column.
How can I force the table to fit the column and don't go over it ?
Perhaps you can use splitLayout to obtain your desired output. Try this
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Test App"
),
dashboardSidebar(),
dashboardBody(
splitLayout(cellWidths = c("25%", "25%", "50%"),
DTOutput("t1"), DTOutput("t2"), DTOutput("t3")
)
)
)
server <- function(input, output) {
output$t1 <- renderDT({mtcars})
output$t2 <- renderDT({mtcars[9:15,]})
output$t3 <- renderDT({mtcars[21:32,]})
}
shinyApp(ui, server)

My img funcition will not render image in shiny

I am trying to use an image as the title instead of text, but all I get in the app is the img icon, not the actual image. Here is my code.
library(tidyverse)
library(leaflet)
library(htmltools)
library(shiny)
ui <- fluidPage(
# Application title
titlePanel(img(src = "www/BBBLogo.png",
height = 40,
width = 60)),
sidebarLayout(
I can't seem to find the issue. Thanks in advance.
You need tags$img and remove www. Try this
ui <- fluidPage(
# Application title
titlePanel(tags$img(src = "BBBLogo.png",
height = 50,
width = 50)),
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)

Read only indicator in Shiny?

I have an existing Shiny script with standard widgets from the Shiny library. Now I wish to add something to show temperature on a graphical scale? This would be a read-only value, so it wouldn't make sense to use a slider unless the slider can be locked and only changed programatically. Is that possible? If not, what are other suggestions?
To clarify:
Is it possible to have a Shiny slider as read only. The user can not slide it but it can be programmatically changed. Here is a Shiny slider:
library(shiny)
ui <- fluidPage(
sliderInput("aa", "Temp",
min = -20, max = 20,
value = 10, step = 10)
)
server <- function(input, output) { }
shinyApp(ui, server)
I'm not familiar with Shiny Dashboard but I saw taskItem. Can these be "dropped in" and used with a normal Shiny app that uses fluidPage, sidebarPanel, mainPanel? How does one remove the bullet point and the percentage? Here is an example of a taskItem.
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
taskItem(value = temp <- 89, color = "red",
"Temp"
))
)
server <- function(input, output) { }
temp <- 89
shinyApp(ui, server)
AFAIK, sliderInput cannot be used as an output. However here's a potential solution using progressBar from shinyWidgets package
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
h3("Sidebar")
),
mainPanel(
br(), br(), br(),
progressBar("tempbar", value = 0, title = "Temperature", status = "danger")
)
)
)
server <- function(input, output, session) {
temp <- 89
updateProgressBar(session, id = "tempbar", value = temp)
}
shinyApp(ui, server)
shiny app with temperature bar
Replace temp in server with whatever calculated value you might have. For fixed temperature value just set it in ui, no need to use updateProgressBar. By default progressBar is scaled from 0-100. To modify see documentation for it.
You can use updateSliderInput to achieve such an behaviour. Couple this with shinyjs::disabled and you get what you want. I would however look for a less hackish solution:
library(shiny)
library(shinyjs)
ui <- fluidPage(
## add style to remove the opacity effect of disabled elements
tags$head(
tags$style(HTML("
.irs-disabled {
opacity: 1
}")
)
),
useShinyjs(),
disabled(sliderInput("aa", "Temp",
min = -20, max = 20,
value = 10, step = 10)),
actionButton("Change", "Change")
)
server <- function(input, output, session) {
observeEvent(input$Change, {
new_temp <- sample(seq(-20, 20, 10), 1)
updateSliderInput(session, "aa", value = new_temp)
})
}
shinyApp(ui, server)

R Shiny: Dynamic column layout to prevent whitespace

I have a shiny dashboard app with a box. The box contains two elements, a slider and a switch. They should be displayed in one row.
When I zoom out, I do not want to get additional whitespace at the end of the row, but rather let the slider take up more space.
It seems like column() is not the right way to layout my app, however I did not find a way to get my elements in one row without column().
How can I get the elements in one row and let the switch take up no more space than it needs?
library(shiny)
library(shinydashboard)
library(shinyWidgets)
ui <- dashboardPage(header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = dashboardBody( column(12, box(width="100%",
column(width=9, sliderInput("hi", label = "I like sliding", min = 1, max =
42, value = 20,width="100%")),
column(width=3,
switchInput("ho", label ="Switch me tonight", width="100%")))))
)
server <- function(input, output) {
}
shinyApp(ui, server)
Play around with the width in the style argument below
library(shiny)
library(shinydashboard)
library(shinyWidgets)
ui <- dashboardPage(header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = dashboardBody(box(width=12,
div(style="display: inline-block; width: 92%;",sliderInput("hi", label = "I like sliding", min = 1, max = 42, value = 20)),
div(style="display: inline-block; width: 7%;",switchInput("ho", label ="Switch me tonight"))
))
)
server <- function(input, output) { }
shinyApp(ui, server)

Shiny Dashboard boxes responsivity

I have two boxes in the first are two inputs and second box contains table. I use package DT for table. This setting is ok for resolution 1920x1080 but problem is in smaller resolution for example 1024x768
tabItem(tabName="Table",titlePanel("My Data"),
box(width=2,uiOutput("first_input"),textInput("numbers_input",label="choose your option",width ="200px")),
box(width=9,dataTableOutput("tabData"))
),
1024x768
Try adding scrollX = T
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
tabItem(tabName="Table",titlePanel("My Data"),
box(width=2,uiOutput("first_input"),textInput("numbers_input",label="choose your option",width ="200px")),
box(width=9,dataTableOutput("tabData"))
)
)
)
server <- function(input, output, session) {
output$tabData <- renderDataTable({cbind(mtcars,mtcars)},options = list(scrollX = T))
}
shinyApp(ui, server)

Resources