R Shiny Scrollable List of Tables in TabPanel - r

How do I create a scrollable list of tables within a tabPanel?
Based on Outputing N tables in shiny, where N depends on the data, I have tried the following
Server.R
userHist <- list(
data.frame(X=1:10,Y=11:20),
data.frame(X=1:10,Y=11:20))
output$groupHistory <- renderUI({
userHistList <- lapply( seq(userHist), function(i){
hist_i <- userHist[[i]]
TabName <- paste0("User", i)
fluidRow( column(10,
h2(TabName),
hr(),
column(3, renderTable(hist_i, rownames=TRUE) )
) )
} )
userHistList
})
ui.R
tabsetPanel(id="tabsetpanel",
tabPanel(h1("Group History"),
style="overflow-y:scroll;",
uiOutput("groupHistory")
)
)
There is a main firefox scrollbar that shows up when the list gets long, but there is a second scrollbar for the table that does not scroll vertically. Ideally I would also eliminate horizontal scrolling.

You need to call the render first to create the output objects and the compose the UI with those objects:
ui <- fluidPage(
tabsetPanel(
id = "tabsetpanel",
tabPanel(
style = "overflow-y:scroll; max-height: 600px",
h1("Group History"),
numericInput("n_users", "Number of Users", value = 5, min = 1, max = 10),
uiOutput("group_history")
)
)
)
server <- shinyServer(function(input, output) {
df_list <- reactive({
n <- input$n_users
# generate some observations
obs_x <- seq(3)
obs_y <- obs_x + n
# generate the df
df_template <- data.frame(x = obs_x, y = obs_y)
# make a list of df and return
lapply(seq(n), function(n) {
df_template
})
})
# use the constructed renders and compose the ui
output$group_history <- renderUI({
table_output_list <- lapply(seq(input$n_users), function(i) {
table_name <- paste0("table", i)
tab_name <- paste("User", i)
fluidRow(
column(
width = 10,
h2(tab_name),
hr(), column(3, tableOutput(table_name))
)
)
})
# Convert the list to a tagList - this is necessary for the list of items
# to display properly.
do.call(tagList, table_output_list)
})
# Call renderTable for each one. Tables are only actually generated when they
# are visible on the web page.
observe({
data <- df_list()
for (i in seq(input$n_users)) {
# Need local so that each item gets its own number. Without it, the value
# of i in the renderPlot() will be the same across all instances, because
# of when the expression is evaluated.
local({
my_i <- i
tab_name <- paste0("table", my_i)
output[[tab_name]] <- renderTable(data[[my_i]], rownames = TRUE)
})
}
})
})
shinyApp(ui, server)
Based off of Winston Chang's work here

I wrapped the list in fluidPage or wellPanel and everything works as I want.
Server.R
userHist <- list(
data.frame(X=1:10,Y=11:20),
data.frame(X=1:10,Y=11:20))
output$groupHistory <- renderUI({
userHistList <- lapply( seq(userHist), function(i){
hist_i <- userHist[[i]]
TabName <- paste0("User", i)
fluidRow( column(10,
h2(TabName),
hr(),
column(3, renderTable(hist_i, rownames=TRUE) )
) )
} )
table_output_list <- fluidPage(userHistList,
style="overflow-y:scroll; max-height: 90vh")
})
UI.R
tabsetPanel(id="tabsetpanel",
tabPanel(h1("Group History"),
style="overflow: visible",
uiOutput("groupHistory")
)
)

Related

Shiny Module Communication

I am trying to create a dynamic UI based on user inputs. First, I am using an lapply inside Mod2.R to return the values of Mod1 onto Mod2 and save it in a reactiveVal object, which lives inside mod2.R and is saved as binner_step_data. The main app then also does an lapply on the return value of Mod2.R (which depends on Mod1.R) and saves it in a reactiveVal object, which is saved as binner_data.
Mod1.R
mod1_UI <- function(id) {
ns <- NS(id)
tagList(
fluidRow(
column(
width = 6,
selectInput(ns("sel_step_name"), "Step Name", choices = c("step1", "step2"))
) # Column
,
column(
width = 3,
numericInput(ns("num_threshold"), "Threshold", value = 0)
) # Column
) # Row
) # tag list
}
mod1_Server <- function(input, output, session) {
return(
list(
step_name = reactive({input$sel_step_name}),
threshold = reactive({input$num_threshold})
)
)
}
Mod2.R
mod2_UI <- function(id) {
ns <- NS(id)
tagList(
fluidRow(
column(
width = 12,
selectInput(ns("sel_n_steps"), "Number of Steps", selected = NULL, choices = 1:3),
uiOutput(ns("ui_mod1_steps"))
)
),
fluidRow(
column(
width = 12,
textInput(ns("txt_bin_name"), "Bin Name", placeholder = "Name of Bin e.g., Blocker", value = "**")
) # Column
) # Fluid Row
) # tag list
}
mod2_Server <- function(input, output, session) {
ns <- session$ns
binner_step_data <- reactiveVal()
observeEvent(input$sel_n_steps{
req(input$sel_n_steps)
lapply(
1:input$sel_n_steps,
function(i) {
res <- callModule(mod1_Server, paste0("binner_step", i), binner_number = reactive(i), dat = reactive(dat()))
return(res)
}
) %>% binner_step_data(.)
})
output$ui_mod1_steps <- renderUI({
req(input$sel_n_steps)
tagList(
fluidRow(
column(
width = 12,
lapply(
1:as.numeric(input$sel_n_steps),
function(i) {
mod1_UI(ns(paste0("binner_step_", i)))
}
) # lapply
) # Column
) # Fluid Row
) # Tag List
})
return(
list(
bin_name = reactive({input$txt_bin_name}),
bin_criteria = reactive({lapply(binner_step_data(), function(step_data) { step_data() })})
)
)
}
Here are my first two modules, in which Mod2.R is using the return values of Mod1.R. Now here's the main app.R file that is using the return value of Mod2.R.
app.R
library(shiny)
library(tidyverse)
# Modules
source("Mod1.R")
source("Mod2.R")
ui <- fluidPage(
fluidRow(
column(
width = 12,
numericInput("num_bins", "Number of Bins", value = NULL, min = 1, max = 10)
) # Column
) # Fluid Row
,
fluidRow(
column(
width = 12,
uiOutput("ui_binners")
) # Column
), # FLuid Row
fluidRow(
column(
width = 12,
verbatimTextOutput("reactive_output_from_mod2")
)
)
)
server <- function(input, output, session) {
binner_data <- reactiveVal()
# Modules -----------------------------------------------------------------
observeEvent(input$num_bins, {
req(input$num_bins)
lapply(
1:input$num_bins,
function(i) {
res <- callModule(mod2_Server, paste0("binner_", i))
return(res)
}
) %>% binner_data(.)
})
output$ui_binners <- renderUI({
req(input$num_bins)
tagList(
fluidRow(
column(
width = 12,
lapply(
1:input$num_bins,
function(i) {
mod2_UI(paste0("binner_", i))
}
) # lapply
) # Column
) # Fluid Row
) # Tag List
})
output$reactive_output_from_mod2 <- renderPrint({
# req(input$sel_n_steps)
binner_data()
})
}
shinyApp(ui, server)
At the end, I'm using a renderPrint to confirm that the values I need are getting passed to the main server but apparently they are not. I should get back a list of two, with the first element being the Bin name and the second element being the Bin Criteria (Step Name + Threshold Value). What is the best way to handle the return values from the two modules to get back the data that I need? Thanks.
To move values out of the server modules, you want the individual elements to be reactive, not the entire list. This structure at the end of BinnerStep.R should provide a list of reactive elements returned from the server module.
return(list(
step_name = reactive({input$sel_step_name}),
threshold = reactive({input$num_threshold}),
rank_direction = reactive({input$sel_rank_direction})
))

Access dynamic id in shiny R

So this is an extension to my previous question.
Dynamic repeating conditionalPanel in R shiny dashboard
Here is the shiny code I am using right now.
library(shiny)
library(shinydashboard)
ui <- fluidPage(
br(),
selectInput("inpt", "Input Number", seq(1,50), selectize = FALSE),
br(),
uiOutput("selectors")
)
server <- function(input, output, session){
output[["selectors"]] <- renderUI({
n <- input[["inpt"]]
selectors <- lapply(1:n, function(i){
selectInput(paste0("id",i), "Select number", seq(1,24), selected = 1)
})
do.call(function(...){
box(..., width = 2, status = "primary")
}, selectors)
})
}
shinyApp(ui, server)
It will generate selection windows depending on 'inpt' number selection.
Now my issue is that I want to access the value of generated selection input.
Example: If I have selected 3, three inputs will be generated with id1, id2, id3.
How to access these ids? If I want to print them, how can I?
for (j in 1:inpt){
print(eval(parse(text = paste0("input$", paste0("id",j)))))
}
But output for this is:
NULL
NULL
NULL
I thought my eval and parse method is wrong so I tried with just inpt
for (j in 1:inpt){
print(eval(parse(text = paste0("input$", paste0("in","pt")))))
}
Output was (3 was selected in selection input)
3
3
3
So my eval, parse method was correct I guess.
So how to access id1, id2, ..., idn in above example?
Please check the following:
library(shiny)
library(shinydashboard)
ui <- fluidPage(
br(),
selectInput("inpt", "Input Number", seq(1,50), selectize = FALSE),
br(),
uiOutput("selectors"),
uiOutput("printMyDynamicInputs"),
uiOutput("printMyFirstDynamicInput")
)
server <- function(input, output, session){
output[["selectors"]] <- renderUI({
n <- input[["inpt"]]
selectors <- lapply(1:n, function(i){
selectInput(paste0("id",i), "Select number", seq(1,24), selected = 1)
})
do.call(function(...){
box(..., width = 2, status = "primary")
}, selectors)
})
myDynamicInputs <- reactive({
lapply(1:input$inpt, function(i){
input[[paste0("id",i)]]
})
})
output$printMyDynamicInput <- renderUI({
paste("You selected:", paste(myDynamicInputs(), collapse = ", "))
})
output$printMyFirstDynamicInputs <- renderUI({
paste("You selected:", input$id1)
})
}
shinyApp(ui, server)

How to combine multiple inputs in one vector in shiny. Number of inputs depends on user's choice

I'm trying to create an UI in which user can choose some objects (as many as they want) and their respective weights. The weight input fields appear only when there's more than one object and increase as the user selects more objects. This part already works.
What I need is a vector that holds all the weights saved in the w1, w2 and so on.
I've tried using for loops and sapply with get() function but can't access the input$w1, input$w2 etc.
library(shiny)
# Create list of objects
object_list <- vector()
object_list <- paste0("O_", 1:10)
names(object_list) <- paste("Object", 1:10)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic UI"),
dashboardSidebar(
width = 700,
fluidRow(
column(7, selectInput("chosen_objects", "Chosen objects", choices = object_list, multiple = TRUE, width = "100%")),
column(5, uiOutput("weights"))
)
),
dashboardBody(
fluidPage(tabBox(width=2500,
tabPanel(
title = "Table"
)
)
)
)
)
server <- function(input,output) {
objects_number <- reactive({length(input$chosen_objects)})
output$weights <- renderUI({
if (is.na(objects_number()) | objects_number() <= 1)
return(NULL)
lapply(1:objects_number(), function(i) {
id <- paste0("w", i)
textInput(id, paste("Weight of", input$chosen_objects[i]), value = input[[id]], width = "50%", placeholder = "%")
})
})
}
shinyApp(ui, server)
Is there a way to collect the dynamic inputs in one vector or list?
I have made a few changes and based on your code I think you are good enough to see and get them by yourself. Let me know if you have any questions -
library(shiny)
# Create list of objects
object_list <- vector()
object_list <- paste0("O_", 1:10)
names(object_list) <- paste("Object", 1:10)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic UI"),
dashboardSidebar(
width = 700,
fluidRow(
column(7, selectInput("chosen_objects", "Chosen objects",
choices = object_list, multiple = TRUE, width = "100%")),
column(5, uiOutput("weights"))
)
),
dashboardBody(
fluidPage(tabBox(width=2500,
tabPanel(
title = "Table",
verbatimTextOutput("weight_output")
)
)
)
)
)
server <- function(input,output) {
objects_number <- reactive({length(input$chosen_objects)})
output$weights <- renderUI({
if (is.na(objects_number()) | objects_number() <= 1)
return(NULL)
lapply(gsub("[A-Z]+_", "", input$chosen_objects), function(i) {
id <- paste0("w", i)
textInput(id, paste("Weight of", paste0("O_", i)),
value = NULL, width = "50%", placeholder = "%")
})
})
output$weight_output <- renderPrint({
req(input$chosen_objects)
sapply(paste0("w", gsub("[A-Z]+_", "", input$chosen_objects)), function(a) input[[a]])
})
}
shinyApp(ui, server)

How to customize tabPanel added by clicking a button in Shiny

I have a sidebarLayout app in which I've set up buttons to add and remove tabPanels in the sidebarPanel. However, I can't figure out how to customize those tabPanels. My code is below:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(width = 3, fixed=T,
h3("L2 Machine"),
actionButton('moreL2', tags$b('Add L2')),
actionButton('lessL2', tags$b('Remove L2')),
uiOutput('panelset'),
tabPanel("L2panel",
numericInput(inputId='L2amount', 'Select L2 Amount', value=0),
selectInput(inputId='L2type', 'Select L2 Type', c('Percent', 'Absolute')),
uiOutput('L2daterange')
)
),
mainPanel(
verbatimTextOutput('L2a'),
verbatimTextOutput('L2t')
)
)
)
server <- function(input, output) {
output$L2a <- renderPrint(input$L2amount)
output$L2t <- renderPrint(input$L2type)
output$panelset <- renderUI({
n <- seq(max(input$moreL2 - input$lessL2 + 1, 1))
tabList <- lapply(paste("Pan", n), tabPanel)
do.call(tabsetPanel, tabList)
})
output$L2daterange <- renderUI({
dateRangeInput(inputId='L2daterange',
label='Select Adjustment Period',
start='01-01-2010', end='01-12-2015'
)
})
}
shinyApp(ui, server)
Currently, I have numericInput(), selectInput(), and uiOutput() inside tabPanel(). Instead I want each tabPanel created by clicking the button "Add L2" to have it's own set of numericInput, selectInput and uiOutput.
You create indeed different tabPanels but they are empty - both numericInput and selectInput are not inside of dynamic tabPanels. The solution is based on https://gist.github.com/wch/5436415/ and you can find there an extensive explanation why do you need a function local to render outputs with a for loop.
As said above, you created correctly dynamic tabPanels but they are empty. Within lapply you should specify unique widgets as arguments to tabPanel.
output$panelset <- renderUI({
n <- seq(max(input$moreL2 - input$lessL2 + 1, 1))
tabList <- lapply(paste("Pan", n), tabPanel)
do.call(tabsetPanel, tabList)
})
Here I coded an example of how you can do it the correct way. Each time you create a unique tabPanel with a unique set of widgets.
tabList <- lapply(n, function(i) {
tabPanel(
title = paste0('Pan', i),
numericInput(inputId = paste0('L2amount', i), 'Select L2 Amount', value = 0),
selectInput(inputId = paste0('L2type', i), 'Select L2 Type', c('Percent', 'Absolute')),
dateRangeInput(inputId = paste0('L2daterange',i),
label = 'Select Adjustment Period',
start = '01-01-2010', end = '01-12-2015'))
})
do.call(tabsetPanel, tabList)
})
Then for each tabPanel with unique set of widgets you have to create unique set of outputs and then you can render values of your widgets.
Full solution:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(width = 3, fixed=T,
h3("L2 Machine"),
actionButton('moreL2', tags$b('Add L2')),
actionButton('lessL2', tags$b('Remove L2')),
uiOutput('panelset')
),
mainPanel(
uiOutput("dynamic")
)
)
)
TMAX <- 10 # specify maximal number of dynamic panels
server <- function(input, output) {
output$panelset <- renderUI({
n <- seq(max(input$moreL2 - input$lessL2 + 1, 1))
# You have to create each time a new set of unique widgets
tabList <- lapply(n, function(i) {
tabPanel(
title = paste0('Pan', i),
numericInput(inputId = paste0('L2amount', i), 'Select L2 Amount', value = 0),
selectInput(inputId = paste0('L2type', i), 'Select L2 Type', c('Percent', 'Absolute')),
dateRangeInput(inputId = paste0('L2daterange',i),
label = 'Select Adjustment Period',
start = '01-01-2010', end = '01-12-2015'))
})
do.call(tabsetPanel, tabList)
})
output$dynamic <- renderUI({
n <- seq(max(input$moreL2 - input$lessL2 + 1, 1))
# You want to render n-times different outputs and each time you have
# k different outputs -- > need a list within a list.
lapply(n, function(i) {
list(
h5(paste0("Pan", i, " widgets")),
verbatimTextOutput(paste0('L2a', i)),
verbatimTextOutput(paste0('L2t', i)),
verbatimTextOutput(paste0('L2dat', i)),
br()
)
})
})
for (i in 1:TMAX) {
local({
my_i <- i
# Outputs
L2a <- paste0('L2a', my_i)
L2t <- paste0('L2t', my_i)
L2dat <- paste0('L2dat', my_i)
list(
output[[L2a]] <- renderPrint({ input[[paste0('L2amount', my_i)]] }),
output[[L2t]] <- renderPrint({ input[[paste0('L2type', my_i)]] }),
output[[L2dat]] <- renderPrint({ input[[paste0('L2daterange', my_i)]] })
)
})
}
}
shinyApp(ui, server)

How to run read.csv data in dynamic input of shiny app

Fowllowing the description of dynamic shiny app at topic [R Shiny Dynamic Input
, i want to get a data into shiny app. I wrote in ui.R
library(fPortfolio)
library(quantmod)
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Portfolio optimization"),
sidebarLayout(
sidebarPanel(
numericInput("numInputs", "A number of stocks", 2),
# place to hold dynamic inputs
uiOutput("inputGroup")
),
mainPanel(
tabPanel("Trading Statistics",
fixedRow(
column(8,
fixedRow(column(4,tableOutput("tablePerformance")),
column(4,tableOutput("tableRisk"))),
fixedRow(column(4,tableOutput("tableDaily")),
column(4,tableOutput("tableMonthly"))))
))
)
)
))
and in server.r
library(fPortfolio)
library(quantmod)
library(shiny)
server<-shinyServer(function(input, output){
observeEvent(input$numInputs, {
output$inputGroup = renderUI({
input_list <- lapply(1:input$numInputs, function(i) {
# for each dynamically generated input, give a different name
inputName <- paste("input", i, sep = "")
textInput(inputName, inputName, value = 1)
})
do.call(tagList, input_list)
})
})
data <- read.csv("E:/stock vn data/dulieuvietnam/metastock_all_data.txt", header = TRUE, stringsAsFactors = FALSE)
Tickers <- data[!duplicated(data$X.Ticker.),1]
Tickers <- subset(Tickers,substr(Tickers,1,1)!= "^")
PriceList <- list()
for (i in 1:length(Tickers)){
PriceList[[i]] <- subset(data[,c(2,6)],data$X.Ticker. == Tickers[i])
names(PriceList[[i]]) <- c("Date",Tickers[i])
PriceList[[i]][PriceList[[i]]==0]<-NA
PriceList[[i]] <- na.locf(PriceList[[i]])
}
PriceList[[(length(Tickers)+1)]]<-subset(data[,c(2,6)],data$X.Ticker. == "^VNINDEX")
names(PriceList[[(length(Tickers)+1)]]) <- c("Date","VNINDEX")
PriceList[[(length(Tickers)+1)]][PriceList[[(length(Tickers)+1)]]==0]<-NA
PriceList[[(length(Tickers)+1)]] <- na.locf(PriceList[[(length(Tickers)+1)]])
dataPrice <- PriceList[[1]]
for (k in 2:length(PriceList)){
dataPrice <-merge(dataPrice,PriceList[[k]],all=TRUE)
}
output$tablePerformance<-renderTable({
})
})
.
When i run runApp(), the app only shows input with label "A number of stocks" that has default value is 2. However, interface of app did not show two text input.
Please help me!

Resources