problem with noUiSliderInput output having only 2 decimals










1














I'm trying to link a log scaled noUiSliderInput with a linear scaled numericInput without creating an eternal update loop.
Normally I would stop the update lines with this:



input$Histoslider != log10(input$Threshold_box)


This created some issues with decimals, which I can't seem to fix properly.
The main problem seems to be that noUiSliderInput always rounds its output to 2 decimals, causing the rounding problem for the conversion to 10^ and log10 back and forth



Detailed description:



I have an app where the user can set threshold filters in 2 ways:



1: type the number in a numericInput in un-transformed numbers



or



2: by changing the bar on a vertical noUiSliderInput.



The noUiSliderInput however is expressed as log10 numbers, as it is lined up with a plot of the data on log10 scale. so if the plot runs from 10^-1 to 10^4.5, the slider has values running from -1 to 4.5



The numericInput and noUiSliderInputare linked, so change one should update the other.
This created a lot of difficulty with decimals. The numericInput has to be restricted in the app to 2 decimals. So to do that I add some transformation, rounding and transformation back again to get matching numbers.



I can get it to work for a normal sliderInput, but somehow not for a noUiSliderInput, eventhough they should spit out the same format of data.



The reason I have to stick with a noUiSliderInput is because I need a slider on both the x and y axis of the plot.



Try to type 1256 in the numericInput to see an example of the problem



The app:



# install.packages("devtools")
devtools::install_github("dreamRs/shinyWidgets")
library(shiny)
library(shinyWidgets)

# function to see how many decimal places we need
decimalplaces <- function(x)
if ((x %% 1) != 0)
deci <- nchar(strsplit(sub('0+$', '', as.character(x)), ".", fixed=TRUE)[[1]][[2]])
if(deci >2) deci <- 2
return(deci)
else
return(0)



# starting values for the sliders
minval <- round(-1, digits = 6)
maxval <- round(4.5, digits = 6)

ui <- fluidPage(
tags$br(),
fluidRow(
column(3,
div(numericInput("Threshold_box", "Normal value: ", min = 0, max = 100, value = 1, step=0.01), style = "display:inline-block") ),
column(2,
# div(sliderInput( inputId = "Histoslider", label = NULL, min = minval, max = maxval, value = 0, step = 0.000001), style = 'display:inline-block; position:relative')
noUiSliderInput(inputId = "Histoslider", label = NULL, min = minval, max = maxval, tooltips = FALSE, value = 0, step = 0.000001, direction = "rtl", orientation = "vertical", width = "100px", height = "276px")
)))

server <- function(input, output, session)
#setting decimals to 6 as that seemed to work in the end for a normal sliderInput
values <- reactiveValues( transformDecimal = 2)

observeEvent(input$Threshold_box,
if(!is.na(input$Threshold_box)) values$transformDecimal <- decimalplaces(input$Threshold_box)
if(input$Histoslider != log10(input$Threshold_box))
newval <- log10(input$Threshold_box)
# updateSliderInput(session, 'Histoslider', value = newval)
updateNoUiSliderInput(session, 'Histoslider', value = newval)
, ignoreInit = T)


observeEvent(input$Histoslider,
## next three lines are to get matching set between a 2 decimal numer and the log value
sliderFull <- 10^input$Histoslider
sliderRound <- round(sliderFull, digits = values$transformDecimal)
sliderLog <- log10(sliderRound)

updateSliderInput(session, 'Histoslider', value = sliderLog)
if(sliderLog != log10(input$Threshold_box))
updateNumericInput(session, 'Threshold_box', value = round(10^sliderLog, digits = values$transformDecimal))

, ignoreInit = T)


shinyApp(ui, server)









share|improve this question




























    1














    I'm trying to link a log scaled noUiSliderInput with a linear scaled numericInput without creating an eternal update loop.
    Normally I would stop the update lines with this:



    input$Histoslider != log10(input$Threshold_box)


    This created some issues with decimals, which I can't seem to fix properly.
    The main problem seems to be that noUiSliderInput always rounds its output to 2 decimals, causing the rounding problem for the conversion to 10^ and log10 back and forth



    Detailed description:



    I have an app where the user can set threshold filters in 2 ways:



    1: type the number in a numericInput in un-transformed numbers



    or



    2: by changing the bar on a vertical noUiSliderInput.



    The noUiSliderInput however is expressed as log10 numbers, as it is lined up with a plot of the data on log10 scale. so if the plot runs from 10^-1 to 10^4.5, the slider has values running from -1 to 4.5



    The numericInput and noUiSliderInputare linked, so change one should update the other.
    This created a lot of difficulty with decimals. The numericInput has to be restricted in the app to 2 decimals. So to do that I add some transformation, rounding and transformation back again to get matching numbers.



    I can get it to work for a normal sliderInput, but somehow not for a noUiSliderInput, eventhough they should spit out the same format of data.



    The reason I have to stick with a noUiSliderInput is because I need a slider on both the x and y axis of the plot.



    Try to type 1256 in the numericInput to see an example of the problem



    The app:



    # install.packages("devtools")
    devtools::install_github("dreamRs/shinyWidgets")
    library(shiny)
    library(shinyWidgets)

    # function to see how many decimal places we need
    decimalplaces <- function(x)
    if ((x %% 1) != 0)
    deci <- nchar(strsplit(sub('0+$', '', as.character(x)), ".", fixed=TRUE)[[1]][[2]])
    if(deci >2) deci <- 2
    return(deci)
    else
    return(0)



    # starting values for the sliders
    minval <- round(-1, digits = 6)
    maxval <- round(4.5, digits = 6)

    ui <- fluidPage(
    tags$br(),
    fluidRow(
    column(3,
    div(numericInput("Threshold_box", "Normal value: ", min = 0, max = 100, value = 1, step=0.01), style = "display:inline-block") ),
    column(2,
    # div(sliderInput( inputId = "Histoslider", label = NULL, min = minval, max = maxval, value = 0, step = 0.000001), style = 'display:inline-block; position:relative')
    noUiSliderInput(inputId = "Histoslider", label = NULL, min = minval, max = maxval, tooltips = FALSE, value = 0, step = 0.000001, direction = "rtl", orientation = "vertical", width = "100px", height = "276px")
    )))

    server <- function(input, output, session)
    #setting decimals to 6 as that seemed to work in the end for a normal sliderInput
    values <- reactiveValues( transformDecimal = 2)

    observeEvent(input$Threshold_box,
    if(!is.na(input$Threshold_box)) values$transformDecimal <- decimalplaces(input$Threshold_box)
    if(input$Histoslider != log10(input$Threshold_box))
    newval <- log10(input$Threshold_box)
    # updateSliderInput(session, 'Histoslider', value = newval)
    updateNoUiSliderInput(session, 'Histoslider', value = newval)
    , ignoreInit = T)


    observeEvent(input$Histoslider,
    ## next three lines are to get matching set between a 2 decimal numer and the log value
    sliderFull <- 10^input$Histoslider
    sliderRound <- round(sliderFull, digits = values$transformDecimal)
    sliderLog <- log10(sliderRound)

    updateSliderInput(session, 'Histoslider', value = sliderLog)
    if(sliderLog != log10(input$Threshold_box))
    updateNumericInput(session, 'Threshold_box', value = round(10^sliderLog, digits = values$transformDecimal))

    , ignoreInit = T)


    shinyApp(ui, server)









    share|improve this question


























      1












      1








      1







      I'm trying to link a log scaled noUiSliderInput with a linear scaled numericInput without creating an eternal update loop.
      Normally I would stop the update lines with this:



      input$Histoslider != log10(input$Threshold_box)


      This created some issues with decimals, which I can't seem to fix properly.
      The main problem seems to be that noUiSliderInput always rounds its output to 2 decimals, causing the rounding problem for the conversion to 10^ and log10 back and forth



      Detailed description:



      I have an app where the user can set threshold filters in 2 ways:



      1: type the number in a numericInput in un-transformed numbers



      or



      2: by changing the bar on a vertical noUiSliderInput.



      The noUiSliderInput however is expressed as log10 numbers, as it is lined up with a plot of the data on log10 scale. so if the plot runs from 10^-1 to 10^4.5, the slider has values running from -1 to 4.5



      The numericInput and noUiSliderInputare linked, so change one should update the other.
      This created a lot of difficulty with decimals. The numericInput has to be restricted in the app to 2 decimals. So to do that I add some transformation, rounding and transformation back again to get matching numbers.



      I can get it to work for a normal sliderInput, but somehow not for a noUiSliderInput, eventhough they should spit out the same format of data.



      The reason I have to stick with a noUiSliderInput is because I need a slider on both the x and y axis of the plot.



      Try to type 1256 in the numericInput to see an example of the problem



      The app:



      # install.packages("devtools")
      devtools::install_github("dreamRs/shinyWidgets")
      library(shiny)
      library(shinyWidgets)

      # function to see how many decimal places we need
      decimalplaces <- function(x)
      if ((x %% 1) != 0)
      deci <- nchar(strsplit(sub('0+$', '', as.character(x)), ".", fixed=TRUE)[[1]][[2]])
      if(deci >2) deci <- 2
      return(deci)
      else
      return(0)



      # starting values for the sliders
      minval <- round(-1, digits = 6)
      maxval <- round(4.5, digits = 6)

      ui <- fluidPage(
      tags$br(),
      fluidRow(
      column(3,
      div(numericInput("Threshold_box", "Normal value: ", min = 0, max = 100, value = 1, step=0.01), style = "display:inline-block") ),
      column(2,
      # div(sliderInput( inputId = "Histoslider", label = NULL, min = minval, max = maxval, value = 0, step = 0.000001), style = 'display:inline-block; position:relative')
      noUiSliderInput(inputId = "Histoslider", label = NULL, min = minval, max = maxval, tooltips = FALSE, value = 0, step = 0.000001, direction = "rtl", orientation = "vertical", width = "100px", height = "276px")
      )))

      server <- function(input, output, session)
      #setting decimals to 6 as that seemed to work in the end for a normal sliderInput
      values <- reactiveValues( transformDecimal = 2)

      observeEvent(input$Threshold_box,
      if(!is.na(input$Threshold_box)) values$transformDecimal <- decimalplaces(input$Threshold_box)
      if(input$Histoslider != log10(input$Threshold_box))
      newval <- log10(input$Threshold_box)
      # updateSliderInput(session, 'Histoslider', value = newval)
      updateNoUiSliderInput(session, 'Histoslider', value = newval)
      , ignoreInit = T)


      observeEvent(input$Histoslider,
      ## next three lines are to get matching set between a 2 decimal numer and the log value
      sliderFull <- 10^input$Histoslider
      sliderRound <- round(sliderFull, digits = values$transformDecimal)
      sliderLog <- log10(sliderRound)

      updateSliderInput(session, 'Histoslider', value = sliderLog)
      if(sliderLog != log10(input$Threshold_box))
      updateNumericInput(session, 'Threshold_box', value = round(10^sliderLog, digits = values$transformDecimal))

      , ignoreInit = T)


      shinyApp(ui, server)









      share|improve this question















      I'm trying to link a log scaled noUiSliderInput with a linear scaled numericInput without creating an eternal update loop.
      Normally I would stop the update lines with this:



      input$Histoslider != log10(input$Threshold_box)


      This created some issues with decimals, which I can't seem to fix properly.
      The main problem seems to be that noUiSliderInput always rounds its output to 2 decimals, causing the rounding problem for the conversion to 10^ and log10 back and forth



      Detailed description:



      I have an app where the user can set threshold filters in 2 ways:



      1: type the number in a numericInput in un-transformed numbers



      or



      2: by changing the bar on a vertical noUiSliderInput.



      The noUiSliderInput however is expressed as log10 numbers, as it is lined up with a plot of the data on log10 scale. so if the plot runs from 10^-1 to 10^4.5, the slider has values running from -1 to 4.5



      The numericInput and noUiSliderInputare linked, so change one should update the other.
      This created a lot of difficulty with decimals. The numericInput has to be restricted in the app to 2 decimals. So to do that I add some transformation, rounding and transformation back again to get matching numbers.



      I can get it to work for a normal sliderInput, but somehow not for a noUiSliderInput, eventhough they should spit out the same format of data.



      The reason I have to stick with a noUiSliderInput is because I need a slider on both the x and y axis of the plot.



      Try to type 1256 in the numericInput to see an example of the problem



      The app:



      # install.packages("devtools")
      devtools::install_github("dreamRs/shinyWidgets")
      library(shiny)
      library(shinyWidgets)

      # function to see how many decimal places we need
      decimalplaces <- function(x)
      if ((x %% 1) != 0)
      deci <- nchar(strsplit(sub('0+$', '', as.character(x)), ".", fixed=TRUE)[[1]][[2]])
      if(deci >2) deci <- 2
      return(deci)
      else
      return(0)



      # starting values for the sliders
      minval <- round(-1, digits = 6)
      maxval <- round(4.5, digits = 6)

      ui <- fluidPage(
      tags$br(),
      fluidRow(
      column(3,
      div(numericInput("Threshold_box", "Normal value: ", min = 0, max = 100, value = 1, step=0.01), style = "display:inline-block") ),
      column(2,
      # div(sliderInput( inputId = "Histoslider", label = NULL, min = minval, max = maxval, value = 0, step = 0.000001), style = 'display:inline-block; position:relative')
      noUiSliderInput(inputId = "Histoslider", label = NULL, min = minval, max = maxval, tooltips = FALSE, value = 0, step = 0.000001, direction = "rtl", orientation = "vertical", width = "100px", height = "276px")
      )))

      server <- function(input, output, session)
      #setting decimals to 6 as that seemed to work in the end for a normal sliderInput
      values <- reactiveValues( transformDecimal = 2)

      observeEvent(input$Threshold_box,
      if(!is.na(input$Threshold_box)) values$transformDecimal <- decimalplaces(input$Threshold_box)
      if(input$Histoslider != log10(input$Threshold_box))
      newval <- log10(input$Threshold_box)
      # updateSliderInput(session, 'Histoslider', value = newval)
      updateNoUiSliderInput(session, 'Histoslider', value = newval)
      , ignoreInit = T)


      observeEvent(input$Histoslider,
      ## next three lines are to get matching set between a 2 decimal numer and the log value
      sliderFull <- 10^input$Histoslider
      sliderRound <- round(sliderFull, digits = values$transformDecimal)
      sliderLog <- log10(sliderRound)

      updateSliderInput(session, 'Histoslider', value = sliderLog)
      if(sliderLog != log10(input$Threshold_box))
      updateNumericInput(session, 'Threshold_box', value = round(10^sliderLog, digits = values$transformDecimal))

      , ignoreInit = T)


      shinyApp(ui, server)






      r shiny decimal






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 '18 at 16:13







      Mark

















      asked Nov 10 '18 at 15:12









      MarkMark

      529521




      529521






















          0






          active

          oldest

          votes











          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%2f53240290%2fproblem-with-nouisliderinput-output-having-only-2-decimals%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53240290%2fproblem-with-nouisliderinput-output-having-only-2-decimals%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







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)