0

So in one of my previous question I had an issue on how to restore bookmark and run the model. This is just a reproducible example but as an app, I would like to modularize it as the size of the app keeps on increasing. I have the code below. In module 1 I want to call to render the datatable and call module 2 when the user clicks on the bookmark.As of now I have the code in module 1 which isnt working . The code for module 2 is in the server section. How can I modularize this app.

"Use case for a shiny app where the users can enter some value and when clicked on run it would run a model and show the values in the table. Now when I click on bookmark it captures the input values. And when I click on the restore bookmark it does populate the input values. What I want to do is after it restores the input values it should also run the model again and populate the values in the table. In short restore bookmark should populate the values and click on the run button to run the model."

library(shiny)
library(RSQLite)
library(data.table)
library(DT)
library(dplyr)

#### Module 1 renders the first table
opFunc <- function(input, output, session, modelRun,modelData,budget){

  output$x1 <- DT::renderDataTable({
    modelRun()

      datatable(
        df %>% mutate(Current  = as.numeric(Current)*(budget())), selection = 'none', editable = TRUE
      )

  })
}
  tableUI <- function(id) {
    ns <- NS(id)
    dataTableOutput(ns("x1"))
  }

#### ideally the second module for bookmarks

opBookmark <- function(){}

ui <- function(request) {
  fluidPage(
    tableUI("opfun"),
    column(12,
      column(3,tags$div(title="forecast", numericInput("budget_input", label = ("Total Forecast"), value = 2))),
      column(2, textInput(inputId = "description", label = "Bookmark description", placeholder = "Data Summary")),
      column(2, bookmarkButton(id="bookmarkBtn"))),
      column(2, actionButton("opt_run", "Run")),
    tags$style(type='text/css', "#bookmarkBtn { width:100%; margin-top: 25px;}")
  )
}

server <- function(input, output, session) {

  callModule( opFunc,"opfun",modelRun = reactive(input$opt_run),modelData = df,budget = reactive(input$budget_input))

  observeEvent(input$opt_run, {
    cat('HJE')
  })

  observeEvent(input$bookmarkBtn, {
    session$doBookmark()
  })
}
enableBookmarking(store = "url")
shinyApp(ui, server)
2
  • 1
    Thanks for posting another question. To be honest, there's a bit too much going on in this code, it's hard to separate the meat from the rest. I would try to first cut down your code to only the bare essentials so that it's easier for other people to jump in and help. Right now it's not quite minimal and looks like a bit too much to tackle!
    – DeanAttali
    Feb 23, 2019 at 1:58
  • 1
    I have tried to cut down on the code from removing all the database components, the datatable where the bookmarks are saved to.Hope this qualifies as minimal example.Thank you.
    – SNT
    Feb 23, 2019 at 2:22

1 Answer 1

2

Unfortunately the code provided was not fully reproducible nor minimal, so I went ahead and tried stripping out what I thought was non essential and added the df from your other post. I also changed the module server name from opFunc to tableMod because it was confusing for me to try to work with a module that had different UI and server names :)

The following code works as expected.

library(shiny)
library(DT)
library(dplyr)

#### Module 1 renders the first table
tableMod <- function(input, output, session, modelRun,modelData,budget){

  output$x1 <- DT::renderDataTable({
    modelRun()
    isolate(
      datatable(
        modelData %>% 
          mutate(Current  = as.numeric(Current)*(budget())),
        selection = 'none', editable = TRUE
      )
    )
  })
}
tableUI <- function(id) {
  ns <- NS(id)
  dataTableOutput(ns("x1"))
}

ui <- function(request) {
  fluidPage(
    tableUI("opfun"),
    numericInput("budget_input", "Total Forecast", value = 2),
    textInput(inputId = "description", "Bookmark description"),
    bookmarkButton(id="bookmarkBtn"),
    actionButton("opt_run", "Run")
  )
}

server <- function(input, output, session) {

  df <- data.frame(Channel = c("A", "B","C"),
                   Current = c(2000, 3000, 4000),
                   Modified = c(2500, 3500,3000),
                   New_Membership = c(450, 650,700),
                   stringsAsFactors = FALSE)

  callModule( tableMod,"opfun",
              modelRun = reactive(input$opt_run),
              modelData = df,
              budget = reactive(input$budget_input))

  observeEvent(input$opt_run, {
    cat('HJE')
  })

  setBookmarkExclude("bookmarkBtn")
  observeEvent(input$bookmarkBtn, {
    session$doBookmark()
  })
}

shinyApp(ui, server, enableBookmarking = "url")
4
  • Thank you. I do notice that the value for opt_run = 1 in the current example. When I try to replicate this by writing the bookmark values to the database the bookmark URL has opt_run = 0. Any reasons why that might be happening.
    – SNT
    Feb 25, 2019 at 21:10
  • Without seeing code I really cannot know. My answer is a direct solution to the question in the post, so if it doesn't solve your actual usecase, you need to invest a bit of time in coming up with a reproducible example that shows the exact problem you're having in your app. It's impossible to debug an app that we cannot see
    – DeanAttali
    Feb 25, 2019 at 22:22
  • So I tried it with your example. If you don't click and run and save bookmark thenopt_run = 0. But if you click on run and save bookmark then 'opt_run = 1'
    – SNT
    Feb 26, 2019 at 2:27
  • That makes sense and sounds like correct behaviour. The value of an input button is the number of times it's pressed. If you click the button 3 times, it'll have the value 3
    – DeanAttali
    Feb 26, 2019 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.