I am trying to get the table row information (such as row name. number, or cell value) when I click the lineup table, but I don;t know how. Can someonle help with it? Any input will be greatly appreciated.
The following is my sample code to show the problem
library(shiny)
library(crosstalk)
library(lineupjs)
library(magrittr)
ui <- fluidPage(
splitLayout(
column(12,
lineupOutput("table1", height=300),
textInput("val1", "Value1", "Tobeupdated1")
),
column(12,
DT::dataTableOutput("table2"),
textInput("val2", "Value2", "Tobeupdated2")
)
)
)
server <- function(input, output,session) {
dfshow <- head(iris,n=5)
shared_iris <- SharedData$new(dfshow)
t<-1
output$table1 <- renderLineup({
lineup(shared_iris)
})
output$table2<- DT::renderDT(dfshow, selection = 'single', rownames = FALSE, editable = TRUE)
observeEvent(input$table1_rows_selected,{
rowIndex <- input$table1_rows_selected
updateTextInput(session, "val1", value = as.character(input$table1_rows_selected))
})
observeEvent(input$table2_rows_selected,{
rowIndex <- input$table2_rows_selected
updateTextInput(session, "val2", value = as.character(input$table2_rows_selected))
})
}
shinyApp(ui = ui, server = server)
What I want is when I click the lineup table1, I want to update the value below the table1, like as the one on the right. Thanks.
It's hidden in the shared_iris$selection() method.
Do something like this:
library(shiny)
library(crosstalk)
library(lineupjs)
library(magrittr)
ui <- fluidPage(
splitLayout(
column(12,
lineupOutput("table1", height=300),
textInput("val1", "Value1", "Tobeupdated1")
),
column(12,
DT::dataTableOutput("table2"),
textInput("val2", "Value2", "Tobeupdated2")
)
)
)
server <- function(input, output,session) {
dfshow <- head(iris,n=5)
shared_iris <- SharedData$new(dfshow)
t<-1
output$table1 <- renderLineup({
lineup(shared_iris)
})
output$table2<- DT::renderDT(dfshow, selection = 'single', rownames = FALSE, editable = TRUE)
observeEvent(shared_iris$selection(),{
rowIndex <- which(shared_iris$selection())
updateTextInput(session, "val1", value = as.character(rowIndex))
})
observeEvent(input$table2_rows_selected,{
rowIndex <- input$table2_rows_selected
updateTextInput(session, "val2", value = as.character(input$table2_rows_selected))
})
}
shinyApp(ui = ui, server = server)
Related
Hello and thanks for reading me. I am working on a small app that shows a table in shiny with the "reactable" library, but I would like to obtain a reactive value when I click on a certain cell, with which I can get a text output type "paste0("you chose" , value0)", but so far I haven't found a correct way to do it. Does anyone have any idea how to do that
The actual code im using is:
shinyApp(
ui = fluidPage(
reactableOutput("tabla")
),
server = function(input, output){
output$tabla <- renderReactable({
iris |>
reactable(
columns = list(
Species = colDef(cell = function(value) {
htmltools::tags$a(href = value, target = "_blank", value)
})
)
)
})
}
)
library(shiny)
library(reactable)
shinyApp(
ui = fluidPage(
reactableOutput("tabla"),
verbatimTextOutput("selected")
),
server = function(input, output){
output$tabla <- renderReactable({
iris |>
reactable(
columns = list(
Species = colDef(cell = function(value) {
htmltools::tags$a(href = value, target = "_blank", value)
})
),
selection = "single", onClick = "select"
)
})
value0 <- reactive({
getReactableState("tabla", "selected")
})
output$selected <- renderPrint({
req(value0())
print(paste("you chose" , value0()))
})
}
)
Read more here
Default ordering is: Item1, Item2, Item3. If I select a new order and click Update, my selection disappears, but the output table is correct.
How do I preserve my selection so that it shows up like so even after Update is clicked?
library(shiny)
shinyApp(
ui = shinyUI({
fluidPage(
sidebarLayout(
sidebarPanel(uiOutput("selection"),
actionButton('update',"Update")),
mainPanel(
tableOutput('ordered')
)
)
)
}),
server = function(input, output, session) {
values <- reactiveValues(x = c('Item1','Item2','Item3'))
output$selection <- renderUI({
selectizeInput('neworder',
'Select order:',
choices = values$x,
selected = preserve$selection,
multiple = TRUE,
options = list(plugins = list('remove_button', 'drag_drop')))
})
output$ordered <- renderTable(
values$x
)
preserve <- reactiveValues(selection = character())
observeEvent(input$neworder,{
if (!all(preserve$selection %in% input$neworder)) {
preserve$selection = input$neworder
}
})
observeEvent(input$update,{
id <- values$x %in% input$neworder
values$x <- c(input$neworder, values$x[!id])
})
}
)
You can use isolate() to avoid unwanted update:
selectizeInput('neworder',
'Select order:',
choices = isolate(values$x),
selected = preserve$selection,
multiple = TRUE,
options = list(plugins = list('remove_button', 'drag_drop')))
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)
I want users of my Shiny app to fill in the values of a 2x2 table with row and column names. Of course, I could do it with 4 input boxes, but I assume that it will be tricky to position everything neatly. Despite that, I would prefer a table layout such as the one provided by the DT package. Thus, my question is: Is it possible to have a datatable (or something similar) filled by the user?
You can use shinysky
devtools::install_github("AnalytixWare/ShinySky") package
or rhandsontable to do what you want:
rm(list = ls())
library(shiny)
library(shinysky)
server <- shinyServer(function(input, output, session) {
# Initiate your table
previous <- reactive({mtcars[1:10,]})
MyChanges <- reactive({
if(is.null(input$hotable1)){return(previous())}
else if(!identical(previous(),input$hotable1)){
# hot.to.df function will convert your updated table into the dataframe
as.data.frame(hot.to.df(input$hotable1))
}
})
output$hotable1 <- renderHotable({MyChanges()}, readOnly = F)
output$tbl = DT::renderDataTable(MyChanges())
})
ui <- basicPage(mainPanel(column(6,hotable("hotable1")),column(6,DT::dataTableOutput('tbl'))))
shinyApp(ui, server)
A solution with DT:
library(DT)
library(shiny)
dat <- data.frame(
V1 = c(as.character(numericInput("x11", "", 0)), as.character(numericInput("x21", "", 0))),
V2 = c(as.character(numericInput("x21", "", 0)), as.character(numericInput("x22", "", 0)))
)
ui <- fluidPage(
fluidRow(
column(5, DT::dataTableOutput('my_table')),
column(2),
column(5, verbatimTextOutput("test"))
)
)
server <- function(input, output, session) {
output$my_table <- DT::renderDataTable(
dat, selection = "none",
options = list(searching = FALSE, paging=FALSE, ordering=FALSE, dom="t"),
server = FALSE, escape = FALSE, rownames= FALSE, colnames=c("", ""),
callback = JS("table.rows().every(function(i, tab, row) {
var $this = $(this.node());
$this.attr('id', this.data()[0]);
$this.addClass('shiny-input-container');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());")
)
output$test <- renderText({
as.character(input$x11)
})
}
shinyApp(ui, server)
I am new to using DT in R shiny.Basically what i am trying to do here is to use the select value from the first table to filter the second table.
my Ui.r is
library(shiny)
library(shinydashboard)
ui <- dashboardPage(skin="green",
dashboardHeader(title="Inventory Management"),
dashboardSidebar(disable = TRUE),
dashboardBody(fluidRow(column(4,box(status="success",
uiOutput("Firstselection"),
br(),
uiOutput("Secondselection"))
),
column(4,infoBoxOutput("salesbox")),
column(4,infoBoxOutput("Runoutbox")),
column(4,infoBoxOutput("Excessbox"))),
actionButton("actionbtn","Run"),
fluidRow(tabBox(tabPanel(
DT::dataTableOutput(outputId="table"),title = "Stock Available for the category chosen",width = 12),
tabPanel(DT::dataTableOutput(outputId="asso"),title = "Associated products",width = 12)))
))
and my server is
server <-function(input, output, session) {
observeEvent(input$actionbtn, {source('global.r',local = TRUE)
#choose sub category based on category
output$Firstselection<-renderUI({selectInput("ray",
"Category:",
c("All",unique(as.character(bestpred$lib_ray))))})
output$Secondselection<-renderUI({selectInput("sray",
"Sub Category:",
c("All",unique(as.character(bestpred[bestpred$lib_ray==input$ray,"lib_sray"]))))})
# Filter data based on selections
output$table <- DT::renderDataTable({
data <- bestpred
if (input$ray != "All"){
data <- data[data$lib_ray == input$ray,]
}
if (input$sray != "All"){
data <- data[data$lib_sray == input$sray,]
}
data
},filter="top"
)
output$salesbox<-renderInfoBox({infoBox("Total Sales",sum(data()$Total_Sales),icon = icon("line-chart"))})
output$Runoutbox<-renderInfoBox({infoBox("Total Runout",sum(data()$status=="Runout"),icon = icon("battery-quarter"))})
output$Excessbox<-renderInfoBox({infoBox("Total excess",sum(data()$status=="Excess"),icon = icon("exclamation-triangle"))})
output$asso <- DT::renderDataTable({
asso <- test1
s=data[input$tablatable_rows_selected,1]
asso <- asso[asso$num_art == s,]
asso
},filter="top")
})}
So when i select a row in the output table i wanna use that as an filter for my asso table
this code dosent poup any error but the output table asso is always empty
Find a generalized solution in the following:
Adapted from here: https://yihui.shinyapps.io/DT-rows/
library(shiny)
library(DT)
server <- shinyServer(function(input, output, session) {
output$x1 = DT::renderDataTable(cars, server = FALSE)
output$x2 = DT::renderDataTable({
sel <- input$x1_rows_selected
if(length(cars)){
cars[sel, ]
}
}, server = FALSE)
})
ui <- fluidPage(
fluidRow(
column(6, DT::dataTableOutput('x1')),
column(6, DT::dataTableOutput('x2'))
)
)
shinyApp(ui, server)