Hello,
I've been working on editing uploaded DT tables in my shiny app and it works great by using the following code.
However, I want to be able to pass the edited uploaded table that gets edited as a final dataframe to a reactivevalues or some object that I can then finally manipulate and use for next step calculation.
It works if the data in the module is dataframe (not reactive as an uploaded one). But I'm not sure how to pass the module with reactive value (upload file) to another reactive values for further calculation. Any help would be much appreciated.
#Load packages
library(shiny)
library(data.table)
library(dplyr, warn.conflicts = FALSE)
library(DT)
#>
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#>
#> dataTableOutput, renderDataTable
library(tidyr)
library(lubridate, warn.conflicts = FALSE)
### Rate ###
counter_rate_fcn <- function(data){
data<-df(data,RATE) %>%
select(provider, Network, BG_FLG, State, SvcType, dt_range,RATE) %>%
spread(key=dt_range,value=RATE, fill = FALSE) %>%
arrange(order(match(Network,gusrate$Network)))
return(data)
}
### Module for Uploaded Rate
modFunction_Upload_Rate <- function(input, output, session, data) {
v <- reactiveValues(data = NULL)
observeEvent(data(), {
v$data <- data()
})
proxy = dataTableProxy("Upload_Rate")
observeEvent(input$Upload_Rate_cell_edit, {
info = input$Upload_Rate_cell_edit
str(info)
i = info$row
j = info$col
k = info$value
str(info)
v$data[i, j] <<- DT::coerceValue(k, v$data[i, j])
replaceData(proxy, v$data, resetPaging = FALSE) # replaces data displayed by the updated table
})
output$Upload_Rate <- DT::renderDataTable({
req(v$data)
DT::datatable(v$data, editable = TRUE) %>%
formatPercentage(c(6:ncol(v$data)), 2)
})
}
modFunctionUI_Upload_Rate <- function(id) {
ns <- NS(id)
DT::dataTableOutput(ns("Upload_Rate"))
}
shinyApp(
ui = fluidPage(
fluidRow(
fileInput("file1", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))
),
fluidRow(
modFunctionUI_Upload_Rate("counter1_rate_table"),
DT::dataTableOutput("counter1_rate_cal_table")
)
),
server = function(input, output, session) {
counter1_rate<-reactive({
req(input$file1)
counter1<-fread(input$file1$datapath, header = TRUE, sep = ",", stringsAsFactors = FALSE)
counter_rate_fcn(counter1)
})
edited <- callModule(modFunction_Upload_Rate, "counter1_rate_table",counter1_rate)
#Problem starts here
cal_counter_rate <- reactiveValues(data=NULL)
# observe({
# cal_counter_rate$data <- edited$data()})
observeEvent(edited$data(),{
cal_counter_rate$data <- edited$data()
})
output$counter1_rate_cal_table <- DT::renderDataTable({
req(cal_counter_rate$data)
DT::datatable(cal_counter_rate$data) %>%
formatPercentage(c(6:ncol(cal_counter_rate$data)), 2)
})
}
)
Shiny applications not supported in static R Markdown documents
Created on 2020-11-18 by the reprex package (v0.3.0)