如何在Shiny中的反应性上下文之外使用input$id?

huangapple go评论117阅读模式
英文:

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(&quot;shiny:sessioninitialized&quot;,function() {
  $.get(&quot;https://api.ipify.org&quot;, function(response)
    {Shiny.setInputValue(&quot;createshinyid&quot;, 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 &lt;- fluidPage(
  
  tags$script(
  &#39;$(document).on(&quot;shiny:sessioninitialized&quot;,function() {
    $.get(&quot;https://api.ipify.org&quot;, function(response)
      {Shiny.setInputValue(&quot;createshinyid&quot;, response);
      });
    });&#39;
  ),
  
  verbatimTextOutput(outputId = &quot;text1&quot;), 
  
  verbatimTextOutput(outputId = &quot;text2&quot;)
  
)

server &lt;- function(input, output, session) {
  
  df_1 &lt;- data.frame(
    ip = c(&quot;123.456.789&quot;, &quot;333.333.333&quot;),
    n = c(1:2)
  )
    
    if ( ( (&quot;333.333.333&quot;) %in% input$createshinyid ) == TRUE ) {
      
      output$texto1 &lt;- renderPrint({
        
        print(&quot;ok&quot;)
        
      })
      
    } else ({
      
      output$texto2 &lt;- renderPrint({
        
        print(&quot;Nok&quot;)
        
      })
      
    })
  
}

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 &lt;- function(input, output, session) {
  
  df_1 &lt;- data.frame(
    ip = c(&quot;123.456.789&quot;, &quot;333.333.333&quot;),
    n = c(1:2)
  )
  
  condition &lt;- reactive({
    &quot;333.333.333&quot; %in% input$createshinyid
  })
  
  output$text1 &lt;- renderPrint({
    req(condition())
    print(&quot;ok&quot;)
  })
  
  output$text2 &lt;- renderPrint({
    req(!condition())
    print(&quot;Nok&quot;)
  })
  
}
英文:

The answer to your question, literrally is: "this is not possible". But you can do that:

server &lt;- function(input, output, session) {
  
  df_1 &lt;- data.frame(
    ip = c(&quot;123.456.789&quot;, &quot;333.333.333&quot;),
    n = c(1:2)
  )
  
  condition &lt;- reactive({
    &quot;333.333.333&quot; %in% input$createshinyid
  })
  
  output$text1 &lt;- renderPrint({
    req(condition())
    print(&quot;ok&quot;)
  })
  
  
  output$text2 &lt;- renderPrint({
    req(!condition())
    print(&quot;Nok&quot;)
  })
  
}

huangapple
  • 本文由 发表于 2023年3月7日 08:08:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定