Shiny datatable: popup data about selected row in a new window - r

I have a datatable in shiny. When a user selects a certain row, I want to display some other data based on the selected row in a new window. I tried to use shinyBS package but I could not use it without action button and I don't want to include action button. I want the pop up to display when a row is selected. Any ideas?
mymtcars = head(mtcars)
for_pop_up = 1:6
app <- shinyApp(
ui = fluidPage(
DT::dataTableOutput("mydatatable")
),
server = shinyServer(function(input, output, session) {
mycars = head(mtcars)
output$mydatatable = DT::renderDataTable(mycars, selection = 'single',
rownames = FALSE, options = list(dom = 't'))
output$popup = renderPrint({
for_pop_up[input$mydatatable_rows_selected]
})
})
)
runApp(app)

You could use an observeEvent and a modal dialog, like this:
mymtcars = head(mtcars)
for_pop_up = 1:6
app <- shinyApp(
ui = fluidPage(
DT::dataTableOutput("mydatatable")
),
server = shinyServer(function(input, output, session) {
mycars = head(mtcars)
output$mydatatable = DT::renderDataTable(mycars, selection = 'single',
rownames = FALSE, options = list(dom = 't'))
observeEvent(input$mydatatable_rows_selected,
{
showModal(modalDialog(
title = "You have selected a row!",
mycars[input$mydatatable_rows_selected,]
))
})
})
)
Hope this helps!

Related

Another datatable inside the main datatable

IS there a way to get datatable when the user clicks on the row. Basically when the user clicks on any row, the last 4 columns of that specific row should be displayed in a modal (in datatable format)
library(shiny)
ui <- fluidPage(
DT::dataTableOutput("mydatatable")
)
server <- function(input, output, session) {
mycars = head(mtcars)
output$mydatatable = DT::renderDataTable(mycars, selection = 'single',
rownames = FALSE, options = list(dom = 't'))
observeEvent(input$mydatatable_rows_selected,
{
showModal(modalDialog(
title = "You have selected a row!",
mycars[input$mydatatable_rows_selected,]
))
})
}
shinyApp(ui, server)
You can use renderDataTable inside modalDialog.
library(shiny)
library(DT)
ui <- fluidPage(
DT::dataTableOutput("mydatatable")
)
server <- function(input, output, session) {
mycars = head(mtcars)
output$mydatatable = DT::renderDataTable(mycars, selection = 'single',
rownames = FALSE, options = list(dom = 't'))
observeEvent(input$mydatatable_rows_selected,
{
showModal(modalDialog(
title = "You have selected a row!",
DT::renderDataTable({
DT::datatable(mycars[input$mydatatable_rows_selected, (ncol(mycars) - 3):ncol(mycars)])
})
))
})
}
shinyApp(ui, server)

Hide a tab in shiny app when the user is not in this tab. Or deactivate it

I have the shiny dashboard below and I have made the cells of the column Species interactive in a way that if the user clicks on a word of that column ,for example 'setosa', to move to the tab Species.This is the only way someone can move to this tab. The issue is that I do not want the tab Species to be displayed when the user is not in this tab. A secondary solution would be to deactivate Species 'click on' ability. So if the user would accidentaly press it nothing would happen.
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
shinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(),
sidebar = dashboardSidebar(),
body = dashboardBody(tabsetPanel(
id = "myTabsetPanel",
tabPanel("Documents",
DTOutput("dt1")),
tabPanel("Species",
DTOutput("dt2"))
)),
),
server = function(input, output, session) {
output$dt1 <- renderDT(
iris,
filter = "top",
options = list(pageLength = 5),
selection = list(mode = 'single', target = 'cell')
)
output$dt2 <- renderDT(
mtcars,
filter = "top",
options = list(pageLength = 5),
selection = list(mode = 'single', target = 'cell')
)
observeEvent(input$dt1_cell_clicked, {
# alternative: input$dt1_cells_selected
if (req(input$dt1_cell_clicked$value) == "setosa") {
updateTabsetPanel(session, inputId = "myTabsetPanel", selected = "Species")
}
})
}
)
UPDATE: observe the input$dt1_cells_selected and reset the value at the end of the observeEvent to allow same cell selection to re-trigger the tab open. You will need to use a dataTableProxy to do this.
You can use hideTab and showTab to reactively hide and show the tab, but still be able to navigate to it via a data table click. More info here. I added a table output to the "Species" tab so we can tell if it has switched properly. By adding an observeEvent around the input$myTabsetPanel we can have the "Species" tab hidden whenever input$myTabsetPanel == Documents:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
shinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(),
sidebar = dashboardSidebar(),
body = dashboardBody(tabsetPanel(
id = "myTabsetPanel",
tabPanel("Documents",
DTOutput("dt1")),
tabPanel("Species",
DTOutput("dt2"))
))
),
server = function(input, output, session) {
observeEvent(input$myTabsetPanel, {
if(input$myTabsetPanel == "Documents"){
hideTab("myTabsetPanel", "Species")
}
})
output$dt1 <- renderDT(
iris,
filter = "top",
options = list(pageLength = 5),
selection = list(mode = 'single', target = 'cell')
)
output$dt2 <- renderDT(
mtcars,
filter = "top",
options = list(pageLength = 5),
selection = list(mode = 'single', target = 'cell')
)
myProxy = DT::dataTableProxy('dt1')
observeEvent(input$dt1_cells_selected,{
# alternative: input$dt1_cells_selected
if (req(input$dt1_cell_clicked$value) == "setosa") {
showTab("myTabsetPanel", "Species")
updateTabsetPanel(session, inputId = "myTabsetPanel", selected = "Species")
DT::selectCells(myProxy, NULL)
}
})
}
)

Title export options of DT::datatable using shiny app

Given following shiny app:
library(shiny)
library(tidyverse)
library(DT)
ui <- fluidPage(
br(),
DTOutput("DT")
)
server <- function(input, output) {
output$DT <- renderDataTable({
mtcars %>%
datatable(.,extensions = 'Buttons',
options = list(dom = 'Bfrtip',
exportOptions = list(header = ""),
buttons = c('copy', 'csv', 'excel', 'pdf')))
})
}
shinyApp(ui = ui, server = server)
one can copy the full table in the clipboard using the buttons on the top left of the datatable.
But when pasting the content into excel, notepad, or whatever there is a header, which I want to remove.
I'm pretty sure that the header can be deleted or changed using sth. similar to exportOptions = list(header = ""),. But not suprisingly this is not working. Perhaps one can find or translate a solution from here to R/Shiny.
That's the title option of the button:
library(shiny)
library(DT)
ui <- fluidPage(
br(),
DTOutput("DT")
)
server <- function(input, output) {
output$DT <- renderDT({
mtcars %>%
datatable(., extensions = 'Buttons',
options = list(
dom = 'Bfrtip',
buttons = list(
list(
extend = "copy",
text = "COPY",
title = NULL
)
)
)
)
})
}
shinyApp(ui = ui, server = server)

data table disappear after editing in shiny app

I'm trying to make an editable and downloadable data table in shiny app. After I edit the table, the data table automatically disappear for some reason. This only happen when the data dat is reactive (which is necessary in my app).
Does anyone knows what is going on? Thanks a lot.
example code below:
library(shiny)
library(DT)
ui <- fluidPage(
selectInput("nrow",
"num of rows",
choices = 1:5,
selected = 3,
multiple = FALSE),
DTOutput("table")
)
server <- function(input, output){
dat = reactive({
iris[1:as.integer(input$nrow),]
})
output[["table"]] <- renderDT({
datatable(dat(), editable = "cell", extensions = "Buttons",
options = list(
dom = "Bfrtip",
buttons = list(
"csv"
)
))
})
observeEvent(input[["table_cell_edit"]], {
cellinfo <- input[["table_cell_edit"]]
dat() <<- editData(dat(), input[["table_cell_edit"]], "table")
})
}
shinyApp(ui, server)
Try this:
library(shiny)
library(DT)
ui <- fluidPage(
selectInput("nrow","num of rows",choices = 1:5,selected = 3,multiple = FALSE),
DTOutput("table")
)
server <- function(input, output){
v <- reactiveValues()
observeEvent(input$nrow,{
v$dat <- iris[1:as.integer(input$nrow),]
})
output[["table"]] <- renderDT({
datatable(v$dat, editable = "cell", extensions = "Buttons", options = list(dom = "Bfrtip",buttons = list("excel")))
})
observeEvent(input[["table_cell_edit"]], {
cellinfo <- input[["table_cell_edit"]]
v$dat <<- editData(v$dat, input[["table_cell_edit"]], "table")
})
}
shinyApp(ui, server)
Is it OK like this ? A possible unwanted behavior is that the table is reset after changing the number of rows. But I don't think we can avoid that... since these are two different tables.
library(shiny)
library(DT)
ui <- fluidPage(
selectInput("nrow",
"num of rows",
choices = 1:5,
selected = 3,
multiple = FALSE),
DTOutput("table")
)
server <- function(input, output){
dat0 <- iris
dat <- reactiveVal()
observe({
dat(dat0[1:as.integer(input$nrow),])
})
output[["table"]] <- renderDT({
datatable(dat(), editable = "cell", extensions = "Buttons",
options = list(
dom = "Bfrtip",
buttons = list(
"csv"
)
))
})
observeEvent(input[["table_cell_edit"]], {
cellinfo <- input[["table_cell_edit"]]
dat(editData(dat(), input[["table_cell_edit"]], "table"))
})
}
shinyApp(ui, server)

Unable to clear the displayed output in ShinyApp using actionButton

I'm building a shinyApp on mtcars data. I got 2 actionButtons (Go & Clear).
The Go button is for displaying the output on mainPanel whereas the Clear button is for clearing that output.
My Clear button isn't working due to some unforeseen reason. Can somebody please have a look at my codes. I shall be extremely grateful.
library(shiny)
library(DT)
library(dplyr)
library(shinythemes)
library(htmlwidgets)
library(shinyWidgets)
library(shinydashboard)
data_table<-mtcars
#ui
ui = fluidPage(
sidebarLayout(
sidebarPanel (
uiOutput("cyl_selector"),
uiOutput("disp_selector"),
actionButton(inputId = "go", label = "Go"),
actionButton(inputId = "reset", label = "Clear")),
mainPanel(
DT::dataTableOutput('mytable') )))
#server
server = function(input, output, session) {
output$cyl_selector <- renderUI({
selectInput(inputId = "cyl",
label = "cyl:", multiple = TRUE,
choices = c( unique(as.character(data_table$cyl))),
selected = c('4')) })
output$disp_selector <- renderUI({
available <- data_table[c(data_table$cyl %in% input$cyl ), "disp"]
selectInput(
inputId = "disp",
label = "disp:",
multiple = TRUE,
choices = c('All',as.character(unique(available))),
selected = 'All') })
thedata <- eventReactive(input$go,{
data_table<-data_table[data_table$cyl %in% input$cyl,]
if(input$disp != 'All'){
data_table<-data_table[data_table$disp %in% input$disp,]
}
data_table
})
# thedata <- eventReactive(input$reset,{
# data_table<-NULL
# })
output$mytable = DT::renderDataTable({
DT::datatable( filter = "top", rownames = FALSE, escape = FALSE,
options = list(pageLength = 50, autowidth=FALSE,
dom = 'Brtip' ),
{
thedata() # Call reactive thedata()
})
})}
shinyApp(ui = ui, server = server)
I didn't analyze your script completly, but i can see that it doesn't call the second button at all (Clear). You made an eventReactive() using input$go for the first button to make the plot, but you need to call input$reset too if you want to make it work.

Resources