Hi all,
I'm trying to create a shiny + shinymeta app, and I'm encountering an issue when substituting a metaReactive object for a new expansion context.
The first time I upload data to the app, the code output button correctly shows the code for uploading the data and for creating the DT. However, upon uploading another .csv file, the expansion context (i.e. the code showing how to read-in the data, in this example) disappears altogether. First two code chunks are an example of this in process, and the latter chunks are my code for a two-file Shiny app. Thanks for the help!
First upload:
# Uploaad data; make sure to add the local file path to read_csv()
data_upload <- readr::read_csv("your_file_here.csv")
# Create interactive data table
data_upload %>%
DT::datatable(options = list(scrollX = TRUE, fixedColumns = TRUE, colReorder = TRUE), extensions = c("FixedColumns", "ColReorder"))
Second upload:
# Create interactive data table
data_upload %>%
DT::datatable(options = list(scrollX = TRUE, fixedColumns = TRUE, colReorder = TRUE), extensions = c("FixedColumns", "ColReorder"))
Server
function(input,output,session) {
# UPLOAD DATA START =====================
# read in data
data_upload <- metaReactive2({
req(input$uploaded_data)
metaExpr( {
readr::read_csv(input$uploaded_data$datapath)
})
})
# create expansion context for data read-in
data_upload_ec <- newExpansionContext()
data_upload_ec$substituteMetaReactive(data_upload, function(){
metaExpr({ "# Uploaad data; make sure to add the local file path to read_csv()"
readr::read_csv("your_file_here.csv")
})
})
# UPLOAD DATA END ========================
# CREATE DT START ========================
# create DT
output$upload_header <- metaRender2(DT::renderDataTable,{
metaExpr({ "# Create interactive data table"
..(data_upload()) %>%
DT::datatable(options = list(scrollX = TRUE, fixedColumns = TRUE, colReorder = TRUE),
extensions = c("FixedColumns",'ColReorder'))
})
})
# observe DT event for code display
observeEvent(input$upload_header_output_code, {
code <- expandChain(.expansionContext = data_upload_ec,
output$upload_header())
displayCodeModal(code)
})
# CREATE DT END ===========================
}
UI
dashboardPage(
# PAGE 1 START =======================
dashboardHeader(title = "Test"),
dashboardSidebar(
fileInput(inputId = "uploaded_data", label = "Upload .csv File", accept = ".csv")
),
dashboardBody(
outputCodeButton(DT::dataTableOutput("upload_header"))
)
# PAGE 1 END ==========================
)
Hi @cpsievert, is this situation showing expected behavior? (I think this might have to do with how observeEvent() works, but I'm afraid I don't know that function well enough to really understand.)