shiny modules: Store parameters (additional argument) already when creating module-UI instead of passing it...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have created a module sliderCheckbox which bundles together a sliderInput and a checkBoxInput to disable the sliderInput - basically a possibility to state "I don't know", which is necessary for survey-like inputs. When the slider is disabled, I want it to return a default value - most often the initial value, but not necessarily.



Now my question is: Is there any possibility to pass this default value when initialising the UI, that is with sliderCheckboxInput()? As the default value is a property like minimum and maximum, that is where it logically belongs to, and it also fits better to the rest of my setup.



Example:



library(shiny)
library(shinyjs)

sliderCheckboxInput <- function(id,description="",
min = 0,
max = 100,
value = 30,
default= NULL ##HERE I would want the default value to be set
cb_title = "I don't know"){
ns <- NS(id)

fluidRow(
column(width=9,
sliderInput(ns("sl"),
paste0(description, collapse=""),
min = min,
max = max,
value = value)
),
column(width=2,
checkboxInput(ns("active"),
cb_title, value=FALSE )
)
)
}

sliderCheckbox<- function(input, output, session,
default=NA) { #Problem: set default when initialising module

oldvalue<- reactiveVal()

observeEvent(input$active, {
if (input$active){
oldvalue(input$sl)
disable("sl")
updateSliderInput(session, "sl", value=default)
}else {
updateSliderInput(session, "sl", value=oldvalue())
enable("sl")
}

toggleState("sl", !input$active)
})

onclick("sl",
if(input$active) updateCheckboxInput(session, "active", value=FALSE)
)

return ( reactive({
if (input$active){
default
}else {
input$sl
}
}))

}


ui <- fluidPage(

useShinyjs(),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderCheckboxInput("bins", "Number of bins:",
min = 1,
max = 50,
value = 30)
),

# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)

server <- function(input, output, session) {
bins_nr <- callModule(sliderCheckbox, "bins", default=44)

output$distPlot <- renderPlot({

# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = bins_nr() + 1)

# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')

})

}

shinyApp(ui, server)









share|improve this question

























  • Whenever I need to do something more complex, like passing values back and forth, I usually end up moving the whole thing to the server side and then passing the rendered input to the ui. When you are in the server you can do all kinds of things, like what you did with the plot. You will probably need to make renderSliderCheckbox().

    – Elin
    Nov 23 '18 at 15:35


















0















I have created a module sliderCheckbox which bundles together a sliderInput and a checkBoxInput to disable the sliderInput - basically a possibility to state "I don't know", which is necessary for survey-like inputs. When the slider is disabled, I want it to return a default value - most often the initial value, but not necessarily.



Now my question is: Is there any possibility to pass this default value when initialising the UI, that is with sliderCheckboxInput()? As the default value is a property like minimum and maximum, that is where it logically belongs to, and it also fits better to the rest of my setup.



Example:



library(shiny)
library(shinyjs)

sliderCheckboxInput <- function(id,description="",
min = 0,
max = 100,
value = 30,
default= NULL ##HERE I would want the default value to be set
cb_title = "I don't know"){
ns <- NS(id)

fluidRow(
column(width=9,
sliderInput(ns("sl"),
paste0(description, collapse=""),
min = min,
max = max,
value = value)
),
column(width=2,
checkboxInput(ns("active"),
cb_title, value=FALSE )
)
)
}

sliderCheckbox<- function(input, output, session,
default=NA) { #Problem: set default when initialising module

oldvalue<- reactiveVal()

observeEvent(input$active, {
if (input$active){
oldvalue(input$sl)
disable("sl")
updateSliderInput(session, "sl", value=default)
}else {
updateSliderInput(session, "sl", value=oldvalue())
enable("sl")
}

toggleState("sl", !input$active)
})

onclick("sl",
if(input$active) updateCheckboxInput(session, "active", value=FALSE)
)

return ( reactive({
if (input$active){
default
}else {
input$sl
}
}))

}


ui <- fluidPage(

useShinyjs(),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderCheckboxInput("bins", "Number of bins:",
min = 1,
max = 50,
value = 30)
),

# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)

server <- function(input, output, session) {
bins_nr <- callModule(sliderCheckbox, "bins", default=44)

output$distPlot <- renderPlot({

# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = bins_nr() + 1)

# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')

})

}

shinyApp(ui, server)









share|improve this question

























  • Whenever I need to do something more complex, like passing values back and forth, I usually end up moving the whole thing to the server side and then passing the rendered input to the ui. When you are in the server you can do all kinds of things, like what you did with the plot. You will probably need to make renderSliderCheckbox().

    – Elin
    Nov 23 '18 at 15:35














0












0








0


2






I have created a module sliderCheckbox which bundles together a sliderInput and a checkBoxInput to disable the sliderInput - basically a possibility to state "I don't know", which is necessary for survey-like inputs. When the slider is disabled, I want it to return a default value - most often the initial value, but not necessarily.



Now my question is: Is there any possibility to pass this default value when initialising the UI, that is with sliderCheckboxInput()? As the default value is a property like minimum and maximum, that is where it logically belongs to, and it also fits better to the rest of my setup.



Example:



library(shiny)
library(shinyjs)

sliderCheckboxInput <- function(id,description="",
min = 0,
max = 100,
value = 30,
default= NULL ##HERE I would want the default value to be set
cb_title = "I don't know"){
ns <- NS(id)

fluidRow(
column(width=9,
sliderInput(ns("sl"),
paste0(description, collapse=""),
min = min,
max = max,
value = value)
),
column(width=2,
checkboxInput(ns("active"),
cb_title, value=FALSE )
)
)
}

sliderCheckbox<- function(input, output, session,
default=NA) { #Problem: set default when initialising module

oldvalue<- reactiveVal()

observeEvent(input$active, {
if (input$active){
oldvalue(input$sl)
disable("sl")
updateSliderInput(session, "sl", value=default)
}else {
updateSliderInput(session, "sl", value=oldvalue())
enable("sl")
}

toggleState("sl", !input$active)
})

onclick("sl",
if(input$active) updateCheckboxInput(session, "active", value=FALSE)
)

return ( reactive({
if (input$active){
default
}else {
input$sl
}
}))

}


ui <- fluidPage(

useShinyjs(),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderCheckboxInput("bins", "Number of bins:",
min = 1,
max = 50,
value = 30)
),

# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)

server <- function(input, output, session) {
bins_nr <- callModule(sliderCheckbox, "bins", default=44)

output$distPlot <- renderPlot({

# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = bins_nr() + 1)

# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')

})

}

shinyApp(ui, server)









share|improve this question
















I have created a module sliderCheckbox which bundles together a sliderInput and a checkBoxInput to disable the sliderInput - basically a possibility to state "I don't know", which is necessary for survey-like inputs. When the slider is disabled, I want it to return a default value - most often the initial value, but not necessarily.



Now my question is: Is there any possibility to pass this default value when initialising the UI, that is with sliderCheckboxInput()? As the default value is a property like minimum and maximum, that is where it logically belongs to, and it also fits better to the rest of my setup.



Example:



library(shiny)
library(shinyjs)

sliderCheckboxInput <- function(id,description="",
min = 0,
max = 100,
value = 30,
default= NULL ##HERE I would want the default value to be set
cb_title = "I don't know"){
ns <- NS(id)

fluidRow(
column(width=9,
sliderInput(ns("sl"),
paste0(description, collapse=""),
min = min,
max = max,
value = value)
),
column(width=2,
checkboxInput(ns("active"),
cb_title, value=FALSE )
)
)
}

sliderCheckbox<- function(input, output, session,
default=NA) { #Problem: set default when initialising module

oldvalue<- reactiveVal()

observeEvent(input$active, {
if (input$active){
oldvalue(input$sl)
disable("sl")
updateSliderInput(session, "sl", value=default)
}else {
updateSliderInput(session, "sl", value=oldvalue())
enable("sl")
}

toggleState("sl", !input$active)
})

onclick("sl",
if(input$active) updateCheckboxInput(session, "active", value=FALSE)
)

return ( reactive({
if (input$active){
default
}else {
input$sl
}
}))

}


ui <- fluidPage(

useShinyjs(),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderCheckboxInput("bins", "Number of bins:",
min = 1,
max = 50,
value = 30)
),

# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)

server <- function(input, output, session) {
bins_nr <- callModule(sliderCheckbox, "bins", default=44)

output$distPlot <- renderPlot({

# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = bins_nr() + 1)

# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')

})

}

shinyApp(ui, server)






r module shiny






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 15:27







Julian

















asked Nov 23 '18 at 14:46









JulianJulian

338315




338315













  • Whenever I need to do something more complex, like passing values back and forth, I usually end up moving the whole thing to the server side and then passing the rendered input to the ui. When you are in the server you can do all kinds of things, like what you did with the plot. You will probably need to make renderSliderCheckbox().

    – Elin
    Nov 23 '18 at 15:35



















  • Whenever I need to do something more complex, like passing values back and forth, I usually end up moving the whole thing to the server side and then passing the rendered input to the ui. When you are in the server you can do all kinds of things, like what you did with the plot. You will probably need to make renderSliderCheckbox().

    – Elin
    Nov 23 '18 at 15:35

















Whenever I need to do something more complex, like passing values back and forth, I usually end up moving the whole thing to the server side and then passing the rendered input to the ui. When you are in the server you can do all kinds of things, like what you did with the plot. You will probably need to make renderSliderCheckbox().

– Elin
Nov 23 '18 at 15:35





Whenever I need to do something more complex, like passing values back and forth, I usually end up moving the whole thing to the server side and then passing the rendered input to the ui. When you are in the server you can do all kinds of things, like what you did with the plot. You will probably need to make renderSliderCheckbox().

– Elin
Nov 23 '18 at 15:35












1 Answer
1






active

oldest

votes


















1














You can send the value from the ui to the server using a hidden textInput



library(shiny)
library(shinyjs)

sendValueToServer <- function(id, value) {
hidden(textInput(
id, "If you can see this, you forgot useShinyjs()", value
))
}

myModuleUI <- function(id, param) {
ns <- NS(id)
tagList(
sendValueToServer(ns("param_id"), param),
textOutput(ns("text_out"))
)
}

myModule <- function(input, output, session) {
param <- isolate(input$param_id)

output$text_out <- renderText({
param
})
}

shinyApp(
ui = fluidPage(
useShinyjs(),
myModuleUI("id", "test")
),
server = function(input, output, session) {
callModule(myModule, "id")
}
)


There are probably more direct ways to do this using the JavaScript API of shiny but this is a "pure R" solution which should be enough for most usecases. Note that you can use the input value at initialization time with



isolate(input$text_in)


because the ui is always built before the server. Things get more complicated if everything is wrapped into renderUI but this does not seem to be the case for you.






share|improve this answer


























  • Thankyou for the answer. In my real application the next step is to wrap everything into renderUI. So I would be glad if if you can explain a little bit more your last sentence?

    – Julian
    Nov 24 '18 at 19:29








  • 1





    If myModuleUI is part of an uiOutput, input$text_in won't be available when the app starts up. Your server logic will have to wait until the renderUI components are rendered (displayed) before fetching input$text_in. This can be quite tricky in practice and therefore I personally try to avoid rednerUI at the "top level" of my applications.

    – Gregor de Cillia
    Nov 24 '18 at 20:11














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448767%2fshiny-modules-store-parameters-additional-argument-already-when-creating-modu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can send the value from the ui to the server using a hidden textInput



library(shiny)
library(shinyjs)

sendValueToServer <- function(id, value) {
hidden(textInput(
id, "If you can see this, you forgot useShinyjs()", value
))
}

myModuleUI <- function(id, param) {
ns <- NS(id)
tagList(
sendValueToServer(ns("param_id"), param),
textOutput(ns("text_out"))
)
}

myModule <- function(input, output, session) {
param <- isolate(input$param_id)

output$text_out <- renderText({
param
})
}

shinyApp(
ui = fluidPage(
useShinyjs(),
myModuleUI("id", "test")
),
server = function(input, output, session) {
callModule(myModule, "id")
}
)


There are probably more direct ways to do this using the JavaScript API of shiny but this is a "pure R" solution which should be enough for most usecases. Note that you can use the input value at initialization time with



isolate(input$text_in)


because the ui is always built before the server. Things get more complicated if everything is wrapped into renderUI but this does not seem to be the case for you.






share|improve this answer


























  • Thankyou for the answer. In my real application the next step is to wrap everything into renderUI. So I would be glad if if you can explain a little bit more your last sentence?

    – Julian
    Nov 24 '18 at 19:29








  • 1





    If myModuleUI is part of an uiOutput, input$text_in won't be available when the app starts up. Your server logic will have to wait until the renderUI components are rendered (displayed) before fetching input$text_in. This can be quite tricky in practice and therefore I personally try to avoid rednerUI at the "top level" of my applications.

    – Gregor de Cillia
    Nov 24 '18 at 20:11


















1














You can send the value from the ui to the server using a hidden textInput



library(shiny)
library(shinyjs)

sendValueToServer <- function(id, value) {
hidden(textInput(
id, "If you can see this, you forgot useShinyjs()", value
))
}

myModuleUI <- function(id, param) {
ns <- NS(id)
tagList(
sendValueToServer(ns("param_id"), param),
textOutput(ns("text_out"))
)
}

myModule <- function(input, output, session) {
param <- isolate(input$param_id)

output$text_out <- renderText({
param
})
}

shinyApp(
ui = fluidPage(
useShinyjs(),
myModuleUI("id", "test")
),
server = function(input, output, session) {
callModule(myModule, "id")
}
)


There are probably more direct ways to do this using the JavaScript API of shiny but this is a "pure R" solution which should be enough for most usecases. Note that you can use the input value at initialization time with



isolate(input$text_in)


because the ui is always built before the server. Things get more complicated if everything is wrapped into renderUI but this does not seem to be the case for you.






share|improve this answer


























  • Thankyou for the answer. In my real application the next step is to wrap everything into renderUI. So I would be glad if if you can explain a little bit more your last sentence?

    – Julian
    Nov 24 '18 at 19:29








  • 1





    If myModuleUI is part of an uiOutput, input$text_in won't be available when the app starts up. Your server logic will have to wait until the renderUI components are rendered (displayed) before fetching input$text_in. This can be quite tricky in practice and therefore I personally try to avoid rednerUI at the "top level" of my applications.

    – Gregor de Cillia
    Nov 24 '18 at 20:11
















1












1








1







You can send the value from the ui to the server using a hidden textInput



library(shiny)
library(shinyjs)

sendValueToServer <- function(id, value) {
hidden(textInput(
id, "If you can see this, you forgot useShinyjs()", value
))
}

myModuleUI <- function(id, param) {
ns <- NS(id)
tagList(
sendValueToServer(ns("param_id"), param),
textOutput(ns("text_out"))
)
}

myModule <- function(input, output, session) {
param <- isolate(input$param_id)

output$text_out <- renderText({
param
})
}

shinyApp(
ui = fluidPage(
useShinyjs(),
myModuleUI("id", "test")
),
server = function(input, output, session) {
callModule(myModule, "id")
}
)


There are probably more direct ways to do this using the JavaScript API of shiny but this is a "pure R" solution which should be enough for most usecases. Note that you can use the input value at initialization time with



isolate(input$text_in)


because the ui is always built before the server. Things get more complicated if everything is wrapped into renderUI but this does not seem to be the case for you.






share|improve this answer















You can send the value from the ui to the server using a hidden textInput



library(shiny)
library(shinyjs)

sendValueToServer <- function(id, value) {
hidden(textInput(
id, "If you can see this, you forgot useShinyjs()", value
))
}

myModuleUI <- function(id, param) {
ns <- NS(id)
tagList(
sendValueToServer(ns("param_id"), param),
textOutput(ns("text_out"))
)
}

myModule <- function(input, output, session) {
param <- isolate(input$param_id)

output$text_out <- renderText({
param
})
}

shinyApp(
ui = fluidPage(
useShinyjs(),
myModuleUI("id", "test")
),
server = function(input, output, session) {
callModule(myModule, "id")
}
)


There are probably more direct ways to do this using the JavaScript API of shiny but this is a "pure R" solution which should be enough for most usecases. Note that you can use the input value at initialization time with



isolate(input$text_in)


because the ui is always built before the server. Things get more complicated if everything is wrapped into renderUI but this does not seem to be the case for you.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 21:39

























answered Nov 23 '18 at 21:18









Gregor de CilliaGregor de Cillia

4,0791923




4,0791923













  • Thankyou for the answer. In my real application the next step is to wrap everything into renderUI. So I would be glad if if you can explain a little bit more your last sentence?

    – Julian
    Nov 24 '18 at 19:29








  • 1





    If myModuleUI is part of an uiOutput, input$text_in won't be available when the app starts up. Your server logic will have to wait until the renderUI components are rendered (displayed) before fetching input$text_in. This can be quite tricky in practice and therefore I personally try to avoid rednerUI at the "top level" of my applications.

    – Gregor de Cillia
    Nov 24 '18 at 20:11





















  • Thankyou for the answer. In my real application the next step is to wrap everything into renderUI. So I would be glad if if you can explain a little bit more your last sentence?

    – Julian
    Nov 24 '18 at 19:29








  • 1





    If myModuleUI is part of an uiOutput, input$text_in won't be available when the app starts up. Your server logic will have to wait until the renderUI components are rendered (displayed) before fetching input$text_in. This can be quite tricky in practice and therefore I personally try to avoid rednerUI at the "top level" of my applications.

    – Gregor de Cillia
    Nov 24 '18 at 20:11



















Thankyou for the answer. In my real application the next step is to wrap everything into renderUI. So I would be glad if if you can explain a little bit more your last sentence?

– Julian
Nov 24 '18 at 19:29







Thankyou for the answer. In my real application the next step is to wrap everything into renderUI. So I would be glad if if you can explain a little bit more your last sentence?

– Julian
Nov 24 '18 at 19:29






1




1





If myModuleUI is part of an uiOutput, input$text_in won't be available when the app starts up. Your server logic will have to wait until the renderUI components are rendered (displayed) before fetching input$text_in. This can be quite tricky in practice and therefore I personally try to avoid rednerUI at the "top level" of my applications.

– Gregor de Cillia
Nov 24 '18 at 20:11







If myModuleUI is part of an uiOutput, input$text_in won't be available when the app starts up. Your server logic will have to wait until the renderUI components are rendered (displayed) before fetching input$text_in. This can be quite tricky in practice and therefore I personally try to avoid rednerUI at the "top level" of my applications.

– Gregor de Cillia
Nov 24 '18 at 20:11






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448767%2fshiny-modules-store-parameters-additional-argument-already-when-creating-modu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()