I'm hoping to insert an rclipboard::rclipButton() into a DataTable in RShiny and am having trouble figuring out how to do it. Have tried the following (based on: Using renderDataTable within renderUi in Shiny):
library(shiny); library(tidyverse); library(rclipboard)
ui <- fluidPage(
mainPanel(
rclipboardSetup(),
uiOutput('myTable')
)
)
server <- function(input, output) {
output$myTable <- renderUI({
output$myTable <- renderUI({
iris <- iris %>% filter(row_number()==1:2)
iris$button <- rclipButton(
inputId = "clipbtn",
label = "Copy",
clipText = "test",
icon = icon("clipboard")
)
output$aa <- renderDataTable(iris)
dataTableOutput("aa")
})
})
}
shinyApp(ui, server)
But looks like this:
"[object Object]"
Have also tried paste0()'ing the rclipButton() into the DataTable but that just renders as a long string of HTML.
Any suggestions much appreciated!
Well, rclipButton() call will generate shiny.tag objects, and you need to change it to string so DT can parse it. Then the key is to use escape = F in datatable.
I also rewrite the way to generate the DT table.
library(shiny); library(tidyverse); library(rclipboard)
ui <- fluidPage(
mainPanel(
rclipboardSetup(),
DT::dataTableOutput("aa")
)
)
server <- function(input, output) {
output$aa <- DT::renderDataTable({
iris2 <- iris %>% filter(row_number()==1:2)
iris2$button <- rclipButton(
inputId = "clipbtn",
label = "Copy",
clipText = "test",
icon = icon("clipboard")
) %>% as.character()
DT::datatable(iris2, escape = F)
})
}
shinyApp(ui, server)
Related
I am trying to colour specific cells in a data frame with RShiny, based on their values.
I have managed to highlight the cells successfully using the formatStyle function from the DT library, however the output format of formatStyle appears to be a list, which is a problem since I would now like to add formatting options to the renderDataTable function (such as the scroll bar using scrollX=TRUE).
Is there someway of transforming the output from formatStyle to a data frame?
So here is some reproducible example code which works:
library(shiny)
library(reticulate)
library(DT)
ui <- fluidPage(
mainPanel(
# first header title
h3("MTCars"),
# prepare the first output table
DT::dataTableOutput('table1'),
)
)
server <- function(input, output, session) {
myData <- mtcars
myData$wheelDiameter <- myData$wt
myData$windscreenHeight <- myData$mpg
myData$carTint <- myData$vs
myData$color <- rep(c("red","black","green","yellow"),4)
colourWeights <- reactive({
highlightData <- datatable(myData) %>% formatStyle(
'wt',
backgroundColor = styleInterval(c(1.5,3.0), c("red","yellow","green")),
fontWeight = 'bold'
)
return(highlightData)
})
# display the first output table
output$table1 <- DT::renderDataTable({
colourWeights()
})
}
shinyApp(ui, server)
And a screen shot of the output:
Shiny output table
And here is some example code which doesn't work since the formatStyle output is not a dataframe:
library(shiny)
library(reticulate)
library(DT)
ui <- fluidPage(
mainPanel(
# first header title
h3("MTCars"),
# prepare the first output table
DT::dataTableOutput('table1'),
)
)
server <- function(input, output, session) {
myData <- mtcars
myData$wheelDiameter <- myData$wt
myData$windscreenHeight <- myData$mpg
myData$carTint <- myData$vs
myData$color <- rep(c("red","black","green","yellow"),4)
colourWeights <- reactive({
highlightData <- datatable(myData) %>% formatStyle(
'wt',
backgroundColor = styleInterval(c(1.5,3.0), c("red","yellow","green")),
fontWeight = 'bold'
)
return(highlightData)
})
# display the first output table
output$table1 <- DT::renderDataTable({
datatable(colourWeights(),
options = list(
scrollX = TRUE,
autoWidth = FALSE,
dom = 'Blrtip'
)
)
})
}
shinyApp(ui, server)
This is the error I get:
Error: 'data' must be 2-dimensional (e.g. data frame or matrix
Thanks in advance
It shows that colourWeights is already a datatable. Thus, moving the options to the highlightData part will work.
library(shiny)
library(reticulate)
library(DT)
ui <- fluidPage(
mainPanel(
# first header title
h3("MTCars"),
# prepare the first output table
DT::dataTableOutput('table1'),
)
)
server <- function(input, output, session) {
myData <- mtcars
myData$wheelDiameter <- myData$wt
myData$windscreenHeight <- myData$mpg
myData$carTint <- myData$vs
myData$color <- rep(c("red","black","green","yellow"),4)
colourWeights <- reactive({
highlightData <- datatable(myData,
options = list(
scrollX = TRUE,
autoWidth = FALSE,
dom = 'Blrtip'
)) %>% formatStyle(
'wt',
backgroundColor = styleInterval(c(1.5,3.0), c("red","yellow","green")),
fontWeight = 'bold'
)
return(highlightData)
})
# display the first output table
output$table1 <- DT::renderDataTable({
colourWeights()
})
}
shinyApp(ui, server)
I have an R shiny app with a DT datatable that is rendered using the datatable function in order to set various options. I would like to use dataTableProxy and replaceData to update the data in the table, but all the examples I can find assume the DT is rendered directly from the data object, not using the datatable function. The reprex below shows what I would like to do, but replaceData doesn't work in this pattern. How do I do this? Thanks.
# based on
# https://community.rstudio.com/t/reorder-data-table-with-seleceted-rows-first/4254
library(shiny)
library(DT)
ui = fluidPage(
actionButton("button1", "Randomize"),
fluidRow(
column(6,
h4("Works"),
DT::dataTableOutput('table1', width="90%")),
column(6,
h4("Doesn't Work"),
DT::dataTableOutput('table2', width="90%"))
)
)
server = function(input, output, session) {
my <- reactiveValues(data = iris)
output$table1 <- DT::renderDataTable(isolate(my$data))
output$table2 <- DT::renderDataTable({
DT::datatable(isolate(my$data),
options = list(lengthChange=FALSE, ordering=FALSE, searching=FALSE,
columnDefs=list(list(className='dt-center', targets="_all")),
stateSave=TRUE, info=FALSE),
class = "nowrap cell-border hover stripe",
rownames = FALSE,
editable = FALSE
) %>%
DT::formatStyle('Sepal.Width', `text-align`="center")
})
observeEvent(input$button1, {
# calculate new row order
row_order <- sample(1:nrow(my$data))
my$data <- my$data[row_order, ]
proxy1 <- DT::dataTableProxy('table1')
DT::replaceData(proxy1, my$data)
proxy2 <- DT::dataTableProxy('table2')
DT::replaceData(proxy2, my$data)
})
}
shinyApp(ui, server)
Update: Very strangely, removing rownames = FALSE made it all possible. I'm not exactly sure why, but probably rownames might be essential for replacing Data.
# based on
# https://community.rstudio.com/t/reorder-data-table-with-seleceted-rows-first/4254
library(shiny)
library(DT)
ui = fluidPage(
actionButton("button1", "Randomize"),
fluidRow(
column(6,
h4("Works"),
DT::dataTableOutput('table1', width="90%")),
column(6,
h4("Doesn't Work"),
DT::dataTableOutput('table2', width="90%"))
)
)
server = function(input, output, session) {
my <- reactiveValues(data = iris)
output$table1 <- DT::renderDataTable(isolate(my$data))
output$table2 <- DT::renderDataTable({
DT::datatable(isolate(my$data),
options = list(lengthChange=FALSE, ordering=FALSE, searching=FALSE,
columnDefs=list(list(className='dt-center', targets="_all")),
stateSave=TRUE, info=FALSE),
class = "nowrap cell-border hover stripe",
# rownames = FALSE,
editable = FALSE
) %>%
DT::formatStyle('Sepal.Width', `text-align`="center")
})
observeEvent(input$button1, {
# calculate new row order
row_order <- sample(1:nrow(my$data))
my$data <- my$data[row_order, ]
proxy1 <- DT::dataTableProxy('table1')
DT::replaceData(proxy1, my$data)
proxy2 <- DT::dataTableProxy('table2')
DT::replaceData(proxy2, my$data)
})
}
shinyApp(ui, server)
When using filtering and the verbatimTextOutput function in R Shiny, rows go seemingly go missing when I select more than one of the input choices in my checkboxGroupInput.
Below is my code. Any advice?
Thanks in advance.
infantmort <- read.csv("infantmort.csv", header = TRUE)
ui <- fluidPage(
checkboxGroupInput("regioninputID",
"Select Region(s)",
choices = unique(infantmort$whoregion)
),
mainPanel(
verbatimTextOutput("regionoutputID"), width = "auto", height = "auto"
)
)
server <- function(input, output) {
dataset <- reactive({
as.data.frame(infantmort %>% select(whoregion, year, deathsinthousands) %>%
filter(whoregion == input$regioninputID) )
})
output$regionoutputID <- renderPrint({ dataset()
})
}
shinyApp(ui = ui, server = server)
You need to change your filter from == to %in%
The following should do the trick
server <- function(input, output) {
dataset <- reactive({
as.data.frame(infantmort %>% select(whoregion, year, deathsinthousands) %>%
filter(whoregion %in% input$regioninputID) )
})
I'm looking to customized the output of a data table in one of my shiny applications. I'd like to only keep the "next page" button on the bottom of the data table, but cannot figure out how to do so. I know you can customize the output using options = list(dom = ...) but cannot figure out how to produce the output I would like. Is this something that will only be able to be accomplished using java script? Example below, where Previous 1, 2, etc. is what I would like to keep. Thank you!
library(DT)
library(shiny)
ui <- fluidPage(
dataTableOutput(outputId = "dat")
)
server <- function(input, output, session) {
tb = iris
tb = datatable(tb, list(pageLength = 10))
output$dat = renderDataTable({
tb
})
}
shinyApp(ui, server)
To learn about all datatable options see this (datatables documentation). The option you are interested in is pagingType. So just do
library(DT)
library(shiny)
ui <- fluidPage(
DT::dataTableOutput(outputId = "dat")
)
server <- function(input, output, session) {
tb = iris
tb = DT::datatable(tb, list(pageLength = 10,pagingType = 'simple'))
output$dat = DT::renderDataTable(
{tb}
)
}
shinyApp(ui, server)
I am trying to figure out how i can change type of the column after getting the data from reactive sql query...
for example when i fetch the data from database, some of the columns are characters, and i wish them to be factors. And some of the columns are numeric (which is correct) but i need them to be displayed as factors in datatable (for use in the datatable, as the column named is Tolerance, and tolerance cannot be filtered by range, it should be one single number).
Simple code:
library(ROracle)
library(shiny)
library(DT)
server <- shinyServer(
function(input, output, session) {
con <- dbConnect(dbDriver("Oracle"),"xx/K",username="user",password="pwd")
tableList <- dbListTables(con,schema="K")
updateSelectizeInput(session, "tabnames", server = TRUE, choices = tableList)
sqlOutput <- reactive({
sqlInput <- paste("select rownum * from K.",input$tabnames)
dbGetQuery(con$cc, sqlInput, stringsAsFactors = T)#it hasnt worked neither
})
output$table <- DT::renderDataTable(sqlOutput(), server=TRUE, rownames=TRUE, filter="top", options=list(pageLength=10))
session$onSessionEnded(function() { dbDisconnect(con) })
})
ui_panel <-
tabPanel("Test",
sidebarLayout(
sidebarPanel(
),
mainPanel(
selectizeInput("tabnames",label = "server side", choices = NULL),
tableOutput("out"),
tableOutput("table")
)
)
)
ui <- shinyUI(navbarPage("Test",ui_panel))
runApp(list(ui=ui,server=server))
When i have tried simply:
output$table <- DT::renderDataTable({
sqlOutput()$HOEHE_TOLP <- as.factor(sqlOutput()$HOEHE_TOLP)
datatable(sqlOutput(), server=TRUE, rownames=TRUE, filter="top", options=list(pageLength=10))})
It didnt work, and gave me an error:
Error in sqlOutput()$HOEHE_TOLP <- as.factor(sqlOutput()$HOEHE_TOLP) :
ungültige (NULL) linke Seite in Zuweisung
*invalid (NULL) left side of assignment
Any ideas how i can convert some columns to factors for reactive data frame?
Cheers
EDIT:
Simply replace this expression:
output$table <- DT::renderDataTable(sqlOutput(), server=TRUE,
rownames=TRUE, filter="top", options=list(pageLength=10))
With:
output$table <- DT::renderDataTable({
intermed <- sqlOutput()
intermed$HOEHE_TOLP <- as.factor(intermed$HOEHE_TOLP)
datatable(intermed) %>% formatStyle("RUND2_MITT", color = 'red',
backgroundColor = 'lightyellow', fontWeight = 'bold')
}, server=TRUE, rownames=TRUE, filter="top", options=list(pageLength=10))
Here is a self contained example:
library(DT)
library(shiny)
ui <- fluidPage(
actionButton("inst", "Instigate Reactive"),
dataTableOutput("test")
)
server <- function(input, output){
data <- eventReactive(input$inst, {
iris
})
output$test <- renderDataTable({
set <- data()
set$Sepal.Length <- as.factor(set$Sepal.Length)
datatable(set) %>% formatStyle("Petal.Length", color = 'red',
backgroundColor = 'lightyellow',
fontWeight = 'bold')
})
}
shinyApp(ui, server)