英文:
How to use input$id outside of reactive context in Shiny?
问题
I need to use a Shiny
input$id
outside the reactive context, inside an if statement. I can't use any reactivity inside this if, but I need to access the input$id
value inside it. As expected, when I do this I get the following response:
Can't access reactive value 'createshinyid' outside of reactive consumer.
ℹ Do you need to wrap inside reactive() or observe()?
I know very well that in this case you must use the input$id
inside a reactive function. This is not the issue here.
I need to use it outside the reactive context, inside an if statement because my application can refuse to start using it or accept it.
I cannot use observe
, observeEvent
, reactive
or any other reactive function. I need to access the input$id
outside of reactivities.
I will explain my code below:
I have this JS
function:
$(document).on("shiny:sessioninitialized", function() {
$.get("https://api.ipify.org", function(response) {
Shiny.setInputValue("createshinyid", response);
});
});
It creates input$createshinyid
.
This function accesses the user's IP of an application.
I need to put it inside an if without reactivity on it.
See part of my code (the application is very large, so I adapted it):
library(shiny)
ui <- fluidPage(
tags$script(
'$(document).on("shiny:sessioninitialized", function() {
$.get("https://api.ipify.org", function(response) {
Shiny.setInputValue("createshinyid", response);
});
});'
),
verbatimTextOutput(outputId = "text1"),
verbatimTextOutput(outputId = "text2")
)
server <- function(input, output, session) {
df_1 <- data.frame(
ip = c("123.456.789", "333.333.333"),
n = c(1:2)
)
if (("333.333.333") %in% input$createshinyid ) {
output$texto1 <- renderPrint({
print("ok")
})
} else {
output$texto2 <- renderPrint({
print("Nok")
})
}
}
shinyApp(ui, server)
The server side contains a lot of Javascript. Using reactive
, observe
, or observeEvent
or similar causes some interference in the code, for example, when I click once, it clicks twice...
So I need to use input$createshinyid
outside the reactive context, inside this if statement.
英文:
I need to use a Shiny
input$id
outside the reactive context, inside an if statement. I can't use any reactivity inside this if, but I need to access the input$id
value inside it. As expected, when I do this I get the following response:
> Can't access reactive value 'createshinyid' outside of reactive consumer.
ℹ Do you need to wrap inside reactive() or observe()?
I know very well that in this case you must use the input$id
inside a reactive function. This is not the issue here.
I need to use it outside the reactive context, inside an if statement because my application can refuse to start using it or accept it.
I cannot use observe
, observeEvent
, reactive
or any other reactive function. I need to access the input$id
outside of reactivities.
I will explain my code below:
I have this JS
function:
$(document).on("shiny:sessioninitialized",function() {
$.get("https://api.ipify.org", function(response)
{Shiny.setInputValue("createshinyid", response);
});
});
It creates input$createshinyid
.
This function accesses the user's IP of an application.
I need to put it inside an if without reactivity on it.
See part of my code (the application is very large, so I adapted it):
library(shiny)
ui <- fluidPage(
tags$script(
'$(document).on("shiny:sessioninitialized",function() {
$.get("https://api.ipify.org", function(response)
{Shiny.setInputValue("createshinyid", response);
});
});'
),
verbatimTextOutput(outputId = "text1"),
verbatimTextOutput(outputId = "text2")
)
server <- function(input, output, session) {
df_1 <- data.frame(
ip = c("123.456.789", "333.333.333"),
n = c(1:2)
)
if ( ( ("333.333.333") %in% input$createshinyid ) == TRUE ) {
output$texto1 <- renderPrint({
print("ok")
})
} else ({
output$texto2 <- renderPrint({
print("Nok")
})
})
}
shinyApp(ui, server)
The server side contains a lot of Javascript. Using reactive
, observe
, or observeEvent
or similar causes some interference in the code, for example, when I click once, it clicks twice...
So I need to use input$createshinyid
outside the reactive context, inside this if statement.
答案1
得分: 1
以下是您要翻译的代码部分:
server <- function(input, output, session) {
df_1 <- data.frame(
ip = c("123.456.789", "333.333.333"),
n = c(1:2)
)
condition <- reactive({
"333.333.333" %in% input$createshinyid
})
output$text1 <- renderPrint({
req(condition())
print("ok")
})
output$text2 <- renderPrint({
req(!condition())
print("Nok")
})
}
英文:
The answer to your question, literrally is: "this is not possible". But you can do that:
server <- function(input, output, session) {
df_1 <- data.frame(
ip = c("123.456.789", "333.333.333"),
n = c(1:2)
)
condition <- reactive({
"333.333.333" %in% input$createshinyid
})
output$text1 <- renderPrint({
req(condition())
print("ok")
})
output$text2 <- renderPrint({
req(!condition())
print("Nok")
})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论