i'm trying to get a subseted datatable form shiny DT output where i have edited some cells. dont need to edit the original datatable, just render the values edited. there is how my shiny UI looks :
the fist DT is source data the second is made with selected rows of the first and three lines under is weighted mean ; weighted standart deviation; and sum of the second data table.
i make the col "Poids" of the second DT editable and i would like extract a DT with edited(and others too) DT2 too make my 3 calculation on it.
there is part of my code :
x2<-reactive({
sel <- input$x1_rows_selected
if(length(valdureT())){
valdureT()[sel, ]
}
})
output$x2 = DT::renderDataTable(x2(), rownames = FALSE,editable = list(
target = 'cell', disable = list(columns = c(1:9))),
extensions = c ('RowGroup'),
options = list(rowGroup = list(dataSrc = 2), order = list(c(4 , 'asc'))),
selection = 'none'
)
x3<-reactive({
sel <- input$x2_rows_all
if(length(x2())){
x2()[sel, ]
}
})
M<-reactive({M <- x3()$"Dureté Moyenne"
M<-as.numeric(M)})
S<-reactive({S<- x3()$"Ecart Type Dureté"
S<-as.numeric(S)})
N<-reactive({N<- x3()$Poids
N<-as.numeric(N)
})
dureTmoymoy<- reactive({paste("Dureté Moyenne des batchs séléctionnés : ",{weighted.mean(M(), N())}," kg")})
sdmoy<- reactive({paste("Ecart Type des batchs selectionnés : ",{sqrt(weighted.mean(S()^2 + M()^2, N()) - weighted.mean(M(), N())^2)}," kg")})
poidsselect<- reactive({paste("Poids des batchs selectionnés :", {sum(N())}," kg")})
output$dureTmoymoy<-renderText({dureTmoymoy()})
output$sdmoy<-renderText({sdmoy()})
output$poidsselect<-renderText({poidsselect()})
as you can see i make the x3 object (expected DT2 (x2) with row edited) with the input$x2_rows_all but that dont work.
is that possible?
exemple with iris data####Ok sorry there is an exemple with iris data.
I made the 1st col (sepal length) editable ; the sepal length have action on my weighted mean as weight.
How to make my 3 bot lines reactive when I edit the sepal length col?
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
wellPanel(
fluidRow(
column(12,h2("iris head" , align = "center"), DT::dataTableOutput('x1')),
column(12,h2("row selected on iris head" , align = "center"), DT::dataTableOutput('x2'))
),
h2("3 calculation about 2nd DT with edited cells"),
h3(textOutput("dureTmoymoy", inline = TRUE)),
h3(textOutput("sdmoy", inline = TRUE)),
h3(textOutput("poidsselect", inline = TRUE)),
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
headiris<- reactive({
headiris<-head(iris)
headiris<-as.data.frame(headiris)
})
output$x1 = DT::renderDataTable(headiris())
x2<-reactive({
sel <- input$x1_rows_selected
if(length(headiris())){
headiris()[sel, ]
}
})
output$x2 = DT::renderDataTable(x2(), rownames = FALSE,editable = list(
target = 'cell', disable = list(columns = c(1:6))),
selection = 'none'
)
x3<-reactive({
sel <- input$x2_rows_all
if(length(x2())){
x2()[sel, ]
}
})
M<-reactive({M <- x3()$"Petal.Length"
M<-as.numeric(M)})
S<-reactive({S<- x3()$"Sepal.Width"
S<-as.numeric(S)})
N<-reactive({N<- x3()$"Sepal.Length"
N<-as.numeric(N)
})
dureTmoymoy<- reactive({paste("petal lenght weighted mean ",{weighted.mean(M(), N())}," kg")})
sdmoy<- reactive({paste("sepal width weighted mean (SD) ",{sqrt(weighted.mean(S()^2 + M()^2, N()) - weighted.mean(M(), N())^2)}," kg")})
poidsselect<- reactive({paste("Sepal lenght sum :", {sum(N())}," kg")})
output$dureTmoymoy<-renderText({dureTmoymoy()})
output$sdmoy<-renderText({sdmoy()})
output$poidsselect<-renderText({poidsselect()})
}
# Run the application
shinyApp(ui = ui, server = server)
valdureT
, etc). – Stéphane Laurent